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: AsFd, Buf: Buffer<u8>>(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<T> { /// 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); /// Assert that `len` elements were written to, and provide a return value. unsafe fn assume_init(self, len: usize) -> Self::Output; } (And thanks to Yoshua Wuyts for feedback on this trait and encouragement for the overall idea!) (Rustix's own Buffer trait is sealed and its functions are private, but that's just rustix choosing for now to reserve the ability to evolve the trait without breaking compatibility, at the expense of not allowing users to use Buffer for defining their own I/O functions, for now.) Buffer is implemented for &mut [T], so users can pass read a &mut [u8] buffer to write into, and it'll return a Result<usize>, where the usize indicates how many bytes were actually read, on success. This matches how read in rustix used to work. Using this looks like: let mut buf = [0_u8; 16]; let num_read = read(fd, &mut buf)?; use(&buf[..num_read]); Buffer is also implemented for &mut [MaybeUninit<T>], so users can pass read a &mut [MaybeUninit<u8>], and in that case, they'll get back a Result<(&mut [u8], &mut ...
First seen: 2025-05-21 03:17
Last seen: 2025-05-21 09:18