pub struct ReadBufCursor<'a> {
buf: &'a mut ReadBuf<'a>,
}Expand description
The cursor part of a ReadBuf, representing the unfilled portion.
This is created by calling ReadBuf::unfilled().
ReadBufCursor provides safe and unsafe methods for writing data into the
buffer:
-
Safe approach: Use
put_sliceto copy data from a slice. This handles initialization tracking and cursor advancement automatically. -
Unsafe approach: For zero-copy scenarios or when interfacing with low-level APIs, use
as_mutto get a mutable slice ofMaybeUninit<u8>, then calladvanceafter writing. This is more efficient but requires careful attention to safety invariants.
§Example using safe methods
use hyper::rt::ReadBuf;
let mut backing = [0u8; 64];
let mut read_buf = ReadBuf::new(&mut backing);
{
let mut cursor = read_buf.unfilled();
// put_slice handles everything safely
cursor.put_slice(b"hello");
}
assert_eq!(read_buf.filled(), b"hello");§Example using unsafe methods
use hyper::rt::ReadBuf;
let mut backing = [0u8; 64];
let mut read_buf = ReadBuf::new(&mut backing);
{
let mut cursor = read_buf.unfilled();
// SAFETY: we will initialize exactly 5 bytes
let slice = unsafe { cursor.as_mut() };
slice[0].write(b'h');
slice[1].write(b'e');
slice[2].write(b'l');
slice[3].write(b'l');
slice[4].write(b'o');
// SAFETY: we have initialized 5 bytes
unsafe { cursor.advance(5) };
}
assert_eq!(read_buf.filled(), b"hello");Fields§
§buf: &'a mut ReadBuf<'a>Implementations§
Source§impl ReadBufCursor<'_>
impl ReadBufCursor<'_>
Sourcepub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<u8>]
pub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<u8>]
Access the unfilled part of the buffer.
§Safety
The caller must not uninitialize any bytes that may have been initialized before.
Sourcepub unsafe fn advance(&mut self, n: usize)
pub unsafe fn advance(&mut self, n: usize)
Advance the filled cursor by n bytes.
§Safety
The caller must take care that n more bytes have been initialized.
Sourcepub fn remaining(&self) -> usize
pub fn remaining(&self) -> usize
Returns the number of bytes that can be written from the current position until the end of the buffer is reached.
This value is equal to the length of the slice returned by as_mut().
Sourcepub fn put_slice(&mut self, src: &[u8])
pub fn put_slice(&mut self, src: &[u8])
Transfer bytes into self from src and advance the cursor
by the number of bytes written.
§Panics
self must have enough remaining capacity to contain all of src.
Sourcepub fn initialize_unfilled(&mut self) -> &mut [u8] ⓘ
pub fn initialize_unfilled(&mut self) -> &mut [u8] ⓘ
Returns a mutable reference to the unfilled part of the buffer, ensuring it is fully initialized.