Writing into Uninitialized Buffers in Rust
Published on: 2025-06-28 17:56:10
Writing into uninitialized buffers in Rust
Posted on March 11, 2025
Uninitialized buffers in Rust are a long-standing question, for example:
Recently, John Nunley and Alex Saveau came up with an idea for a new approach, using a Buffer trait, which is now in rustix 1.0, which I'll describe in this post.
Update: This idea is now available in a standalone published library: buffer-trait.
Introducing the Buffer trait
The POSIX read function reads bytes from a file descriptor into a buffer, and it can read fewer bytes than requested. Using Buffer , read in rustix looks like this:
pub fn read >( fd : Fd, buf : Buf) -> Result< Buf:: Output>
This uses the Buffer trait to describe the buffer argument. The Buffer trait looks like this:
pub trait Buffer { /// The type of the value returned by functions with `Buffer` arguments. type Output; /// Return a raw pointer and length to the underlying buffer. fn parts_mut (& mut self ) -> ( *mut T, usize ); /// Asse
... Read full article.