packet::serialize

Trait BufferProvider

Source
pub trait BufferProvider<Input, Output> {
    type Error;

    // Required methods
    fn alloc_no_reuse(
        self,
        prefix: usize,
        body: usize,
        suffix: usize,
    ) -> Result<Output, Self::Error>;
    fn reuse_or_realloc(
        self,
        buffer: Input,
        prefix: usize,
        suffix: usize,
    ) -> Result<Output, (Self::Error, Input)>;
}
Expand description

An object capable of providing buffers which satisfy certain constraints.

A BufferProvider<Input, Output> is an object which is capable of consuming a buffer of type Input and, either by reusing it or by allocating a new one and copying the input buffer’s body into it, producing a buffer of type Output which meets certain prefix and suffix length constraints.

A BufferProvider must always be provided when serializing a Serializer.

Implementors may find the helper function try_reuse_buffer useful.

For clients who don’t need the full expressive power of this trait, the simpler BufferAlloc trait is provided. It only defines how to allocate new buffers, and two blanket impls of BufferProvider are provided for all BufferAlloc types.

Required Associated Types§

Source

type Error

The type of errors returned from reuse_or_realloc.

Required Methods§

Source

fn alloc_no_reuse( self, prefix: usize, body: usize, suffix: usize, ) -> Result<Output, Self::Error>

Attempts to produce an output buffer with the given constraints by allocating a new one.

alloc_no_reuse produces a new buffer with the following invariants:

  • The output buffer must have at least prefix bytes of prefix
  • The output buffer must have at least suffix bytes of suffix
  • The output buffer must have a body of length body bytes.

If these requirements cannot be met, then an error is returned.

Source

fn reuse_or_realloc( self, buffer: Input, prefix: usize, suffix: usize, ) -> Result<Output, (Self::Error, Input)>

Consumes an input buffer and attempts to produce an output buffer with the given constraints, either by reusing the input buffer or by allocating a new one and copying the body into it.

reuse_or_realloc consumes a buffer by value, and produces a new buffer with the following invariants:

  • The output buffer must have at least prefix bytes of prefix
  • The output buffer must have at least suffix bytes of suffix
  • The output buffer must have the same body as the input buffer

If these requirements cannot be met, then an error is returned along with the input buffer, which is unmodified.

Implementors§