Struct ReadBufCursor

pub struct ReadBufCursor<'a> { /* private fields */ }

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:

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");

Implementations

impl ReadBufCursor<'_>

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.

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.

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().

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.

fn initialize_unfilled(&mut self) -> &mut [u8]

Returns a mutable reference to the unfilled part of the buffer, ensuring it is fully initialized.

fn initialize_unfilled_to(&mut self, n: usize) -> &mut [u8]

Returns a mutable reference to the first n bytes of the unfilled part of the buffer, ensuring it is fully initialized.

Panics

Panics if self.remaining() is less than n.

Trait Implementations

impl<'a> Debug for ReadBufCursor<'a>

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Auto Trait Implementations

impl<'a> !UnwindSafe for ReadBufCursor<'a>

impl<'a> Freeze for ReadBufCursor<'a>

impl<'a> RefUnwindSafe for ReadBufCursor<'a>

impl<'a> Send for ReadBufCursor<'a>

impl<'a> Sync for ReadBufCursor<'a>

impl<'a> Unpin for ReadBufCursor<'a>

impl<'a> UnsafeUnpin for ReadBufCursor<'a>

Blanket Implementations

impl<T> Any for ReadBufCursor<'a> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for ReadBufCursor<'a> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for ReadBufCursor<'a> where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> From<T> for ReadBufCursor<'a>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Instrument for ReadBufCursor<'a>

impl<T> WithSubscriber for ReadBufCursor<'a>

impl<T, U> Into<U> for ReadBufCursor<'a> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for ReadBufCursor<'a> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for ReadBufCursor<'a> where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>