pub trait OctetsBuilder: Sized {
    type Octets;

    // Required methods
    fn append_slice(&mut self, slice: &[u8]) -> Result<(), ShortBuf>;
    fn truncate(&mut self, len: usize);
    fn freeze(self) -> Self::Octets;
    fn len(&self) -> usize;
    fn is_empty(&self) -> bool;

    // Provided methods
    fn append_all<F>(&mut self, op: F) -> Result<(), ShortBuf>
       where F: FnOnce(&mut Self) -> Result<(), ShortBuf> { ... }
    fn append_compressed_dname<N: ToDname>(
        &mut self,
        name: &N
    ) -> Result<(), ShortBuf> { ... }
    fn u16_len_prefixed<F>(&mut self, op: F) -> Result<(), ShortBuf>
       where Self: AsMut<[u8]>,
             F: FnOnce(&mut Self) -> Result<(), ShortBuf> { ... }
}
Expand description

A buffer to construct an octet sequence.

Octet builders represent a buffer of space available for building an octets sequence by appending the contents of octet slices. The buffers may consist of a predefined amount of space or grow as needed.

The trait does not require octet builder to provide access to the already assembled data. However, implementations are likely to do so, anyway, via implementations of AsRef<[u8]> and AsMut<[u8]>. If access becomes necessary when using an octets builder, simply add these as extra trait bounds.

Required Associated Types§

source

type Octets

The type of the octets the builder can be converted into.

If Octets implements IntoBuilder, the Builder associated type of that trait must be Self.

Required Methods§

source

fn append_slice(&mut self, slice: &[u8]) -> Result<(), ShortBuf>

Appends the content of a slice to the builder.

If there isn’t enough space available for appending the slice, returns an error and leaves the builder alone.

source

fn truncate(&mut self, len: usize)

Truncates the builder back to a length of len octets.

source

fn freeze(self) -> Self::Octets

Converts the builder into immutable octets.

source

fn len(&self) -> usize

Returns the length of the already assembled data.

source

fn is_empty(&self) -> bool

Returns whether the builder is currently empty.

Provided Methods§

source

fn append_all<F>(&mut self, op: F) -> Result<(), ShortBuf>where F: FnOnce(&mut Self) -> Result<(), ShortBuf>,

Appends all data or nothing.

The method executes the provided closure that presumably will try to append data to the builder and propagates an error from the builder. If the closure returns with an error, the builder is truncated back to the length from before the closure was executed.

Note that upon an error the builder is only truncated. If the closure modified any already present data via AsMut<[u8]>, these modification will survive.

source

fn append_compressed_dname<N: ToDname>( &mut self, name: &N ) -> Result<(), ShortBuf>

Appends a domain name using name compression if supported.

Domain name compression attempts to lower the size of a DNS message by avoiding to include repeated domain name suffixes. Instead of adding the full suffix, a pointer to the location of the previous occurence is added. Since that occurence may itself contain a compressed suffix, doing name compression isn’t cheap and therefore optional. However, in order to be able to opt in, we need to know if we are dealing with a domain name that ought to be compressed.

The trait provides a default implementation which simply appends the name uncompressed.

source

fn u16_len_prefixed<F>(&mut self, op: F) -> Result<(), ShortBuf>where Self: AsMut<[u8]>, F: FnOnce(&mut Self) -> Result<(), ShortBuf>,

Prepends some appended data with its length as a u16.

The method will append the data being added via the closure op to the builder prepended with a 16 bit unsigned value of its length.

The implementation will prepend a 0u16 before executing the closure and update it to the number of octets added afterwards. If the closure adds more than 65535 octets or if any appending fails, the builder will be truncated to its previous length.

Implementations on Foreign Types§

source§

impl OctetsBuilder for Vec<u8>

§

type Octets = Vec<u8, Global>

source§

fn append_slice(&mut self, slice: &[u8]) -> Result<(), ShortBuf>

source§

fn truncate(&mut self, len: usize)

source§

fn freeze(self) -> Self::Octets

source§

fn len(&self) -> usize

source§

fn is_empty(&self) -> bool

source§

impl<'a, T: OctetsBuilder<Octets = T>> OctetsBuilder for &'a mut T

§

type Octets = &'a mut T

source§

fn append_slice(&mut self, slice: &[u8]) -> Result<(), ShortBuf>

source§

fn truncate(&mut self, len: usize)

source§

fn freeze(self) -> Self::Octets

source§

fn len(&self) -> usize

source§

fn is_empty(&self) -> bool

source§

impl<'a> OctetsBuilder for Cow<'a, [u8]>

§

type Octets = Cow<'a, [u8]>

source§

fn append_slice(&mut self, slice: &[u8]) -> Result<(), ShortBuf>

source§

fn truncate(&mut self, len: usize)

source§

fn freeze(self) -> Self::Octets

source§

fn len(&self) -> usize

source§

fn is_empty(&self) -> bool

source§

impl<A: Array<Item = u8>> OctetsBuilder for SmallVec<A>

§

type Octets = SmallVec<A>

source§

fn append_slice(&mut self, slice: &[u8]) -> Result<(), ShortBuf>

source§

fn truncate(&mut self, len: usize)

source§

fn freeze(self) -> Self::Octets

source§

fn len(&self) -> usize

source§

fn is_empty(&self) -> bool

source§

impl OctetsBuilder for BytesMut

§

type Octets = Bytes

source§

fn append_slice(&mut self, slice: &[u8]) -> Result<(), ShortBuf>

source§

fn truncate(&mut self, len: usize)

source§

fn freeze(self) -> Self::Octets

source§

fn len(&self) -> usize

source§

fn is_empty(&self) -> bool

Implementors§