Trait heph::net::Bytes[][src]

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

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.

Returns the length of the buffer as returned by as_bytes.

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

Returns true if the buffer has spare capacity.

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

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::net::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`.
}

Implementors