pub trait Bytes {
fn as_bytes(&mut self) -> &mut [MaybeUninit<u8>];
fn spare_capacity(&self) -> usize;
unsafe fn update_length(&mut self, n: usize);
fn has_spare_capacity(&self) -> bool { ... }
fn limit(self, limit: usize) -> LimitedBytes<Self>
where
Self: Sized,
{ ... }
}Expand description
Trait to make easier to work with uninitialised buffers.
This is implemented for common types such as Vec<u8>, see below.
Required Methods
sourcefn as_bytes(&mut self) -> &mut [MaybeUninit<u8>]
fn as_bytes(&mut self) -> &mut [MaybeUninit<u8>]
Returns itself as a slice of bytes that may or may not be initialised.
Notes
The implementation must guarantee that two calls (without a call to
update_length in between) returns the same slice of bytes.
sourcefn spare_capacity(&self) -> usize
fn spare_capacity(&self) -> usize
Returns the length of the buffer as returned by as_bytes.
sourceunsafe fn update_length(&mut self, n: usize)
unsafe fn update_length(&mut self, n: usize)
Update the length of the byte slice, marking n bytes as initialised.
Safety
The caller must ensure that at least the first n bytes returned by
as_bytes are initialised.
Notes
If this method is not implemented correctly methods such as
TcpStream::recv_n will not work correctly (as the buffer will
overwrite itself on successive reads).
Provided Methods
sourcefn has_spare_capacity(&self) -> bool
fn has_spare_capacity(&self) -> bool
Returns true if the buffer has spare capacity.
sourcefn limit(self, limit: usize) -> LimitedBytes<Self>where
Self: Sized,
fn limit(self, limit: usize) -> LimitedBytes<Self>where
Self: Sized,
Wrap the buffer in LimitedBytes, which limits the amount of bytes used
to limit.
LimitedBytes::into_inner can be used to retrieve the buffer again,
or a mutable reference to the buffer can be used and the limited buffer
be dropped after usage.
Implementations on Foreign Types
sourceimpl<B> Bytes for &mut Bwhere
B: Bytes + ?Sized,
impl<B> Bytes for &mut Bwhere
B: Bytes + ?Sized,
fn as_bytes(&mut self) -> &mut [MaybeUninit<u8>]
fn spare_capacity(&self) -> usize
fn has_spare_capacity(&self) -> bool
unsafe fn update_length(&mut self, n: usize)
sourceimpl Bytes for Vec<u8>
impl Bytes for Vec<u8>
The implementation for Vec<u8> only uses the uninitialised capacity of the
vector. In other words the bytes currently in the vector remain untouched.
Examples
The following example shows that the bytes already in the vector remain untouched.
use heph_rt::bytes::Bytes;
let mut buf = Vec::with_capacity(100);
buf.extend(b"Hello world!");
write_bytes(b" Hello mars!", &mut buf);
assert_eq!(&*buf, b"Hello world! Hello mars!");
fn write_bytes<B>(src: &[u8], mut buf: B) where B: Bytes {
// Writes `src` to `buf`.
}