Skip to main content

Buf

Trait Buf 

Source
pub trait Buf: Buf {
    // Provided methods
    fn take_owned<T: OwnedBytes>(&mut self, len: usize) -> T { ... }
    fn try_take_owned<T: OwnedBytes>(
        &mut self,
        len: usize,
    ) -> Result<T, TryGetError> { ... }
}
Expand description

A buffer accepted by codec readers.

Use this bound for generic readers of serialized values and their decoding helpers, including fixed-size reads and length or padding validation.

Implementations backed by shared storage must preserve that ownership in bytes::Buf::copy_to_bytes whenever the requested range can be represented without copying. This lets decoded byte fields retain views of the input allocation. Implement this trait for custom buffers that meet this contract. Implementations whose storage is an OwnedBytes type must also offer it through Buf::take_owned, passing the view to OwnedBytes::claim and building T from its bytes when it comes back, so fields of that type decode as views of the input. Adapters over another Buf forward Buf::take_owned so a view is not lost behind them.

Borrowed slices require an explicit Copying adapter. Ordinary scalar and byte-array reads remain allocation-free, while retained byte fields are copied. Borrow Bytes through Viewing to share storage for retained fields.

Provided Methods§

Source

fn take_owned<T: OwnedBytes>(&mut self, len: usize) -> T

Takes the next len bytes as T.

A buffer whose storage is an OwnedBytes type returns a view of that storage when T is that type. Any other T is built from the bytes.

§Panics

Panics if fewer than len bytes remain.

Source

fn try_take_owned<T: OwnedBytes>( &mut self, len: usize, ) -> Result<T, TryGetError>

Takes the next len bytes as T, as Buf::take_owned does.

§Errors

Returns the shortfall and leaves the buffer unchanged if fewer than len bytes remain.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Buf for Bytes

Source§

impl Buf for BytesMut

Source§

impl<A: Buf, B: Buf> Buf for Chain<A, B>

Source§

fn take_owned<T: OwnedBytes>(&mut self, len: usize) -> T

Returns what the buffer holding the whole range returns, and builds T from the bytes when the range spans both.

Source§

impl<B: Buf + ?Sized> Buf for &mut B

Source§

fn take_owned<T: OwnedBytes>(&mut self, len: usize) -> T

Source§

impl<B: Buf> Buf for Take<B>

Source§

fn take_owned<T: OwnedBytes>(&mut self, len: usize) -> T

Returns what the limited buffer returns, and shortens the limit by len.

Implementors§

Source§

impl Buf for Copying<'_>

Source§

impl Buf for Viewing<'_>