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.