Trait mesh_protobuf::buffer::Buffer

source ·
pub trait Buffer {
    // Required methods
    unsafe fn unwritten(&mut self) -> &mut [MaybeUninit<u8>];
    unsafe fn extend_written(&mut self, len: usize);
}
Expand description

Models a partially written, contiguous byte buffer.

Required Methods§

source

unsafe fn unwritten(&mut self) -> &mut [MaybeUninit<u8>]

Returns the unwritten portion of the buffer. The returned data may or may not be initialized.

§Safety

The caller must ensure that no uninitialized bytes are written to the slice.

An astute reader might note that the Vec<u8> implementation does not require the unsafe bound on this function, as those bytes returned by are truly MaybeUninit. However, based on the backing storage of Buffer this is not always the case.

For example, a Buffer implementation on a Cursor<&[u8]> could be used to uninitialize a portion of the slice, by doing the following:

// some_cursor contains a Cursor based implementation of Buffer which is
// backed by storage that is always initialized.
let foo = some_cursor.unwritten();
foo[0].write(MaybeUninit::uninit()) // This is UB!! ⚠️

Thus the caller must ensure that uninitialize bytes are never written to the returned slice, and why this function is unsafe.

source

unsafe fn extend_written(&mut self, len: usize)

Extends the initialized region of the buffer.

§Safety

The caller must ensure that the next len bytes have been initialized.

Implementations on Foreign Types§

source§

impl Buffer for Vec<u8>

source§

unsafe fn unwritten(&mut self) -> &mut [MaybeUninit<u8>]

source§

unsafe fn extend_written(&mut self, len: usize)

source§

impl Buffer for Cursor<&mut [u8]>

source§

unsafe fn unwritten(&mut self) -> &mut [MaybeUninit<u8>]

source§

unsafe fn extend_written(&mut self, len: usize)

Implementors§

source§

impl Buffer for Buf<'_>