Trait domain::base::octets::OctetsRef

source ·
pub trait OctetsRef: AsRef<[u8]> + Copy + Sized {
    type Range: AsRef<[u8]>;

    // Required method
    fn range(self, start: usize, end: usize) -> Self::Range;

    // Provided methods
    fn range_from(self, start: usize) -> Self::Range { ... }
    fn range_to(self, end: usize) -> Self::Range { ... }
    fn range_all(self) -> Self::Range { ... }
}
Expand description

A reference to an octets sequence.

This trait is to be implemented for a (imutable) reference to a type of an octets sequence. I.e., it T is an octets sequence, OctetsRef needs to be implemented for &T.

The primary purpose of the trait is to allow access to a sub-sequence, called a ‘range.’ The type of this range is given via the Range associated type. For most types it will be a &[u8] with a lifetime equal to that of the reference itself. Only if an owned range can be created cheaply, it should be that type.

There is two basic ways of using the trait for a trait bound. You can either limit the octets sequence type itself by bounding references to it via a where clause. I.e., for an octets sequence type argument Octets you can specify where &'a Octets: OctetsRef or, if you don’t have a lifetime argument available where for<'a> &'a Octets: OctetsRef. For this option, you’d typically refer to values as references to the octets type, i.e., &Octets.

Alternatively, you can refer to the reference itself as a owned value. This works out fine since all octets references are required to be Copy. For instance, a function can take a value of generic type Oref and that type can then be directly bounded via Oref: OctetsRef.

Required Associated Types§

source

type Range: AsRef<[u8]>

The type of a range of the sequence.

Required Methods§

source

fn range(self, start: usize, end: usize) -> Self::Range

Returns a sub-sequence or ‘range’ of the sequence.

Provided Methods§

source

fn range_from(self, start: usize) -> Self::Range

Returns a range starting at index start and going to the end.

source

fn range_to(self, end: usize) -> Self::Range

Returns a range from the start to before index end.

source

fn range_all(self) -> Self::Range

Returns a range that covers the entire sequence.

Implementations on Foreign Types§

source§

impl<'a, T: OctetsRef> OctetsRef for &'a T

§

type Range = <T as OctetsRef>::Range

source§

fn range(self, start: usize, end: usize) -> Self::Range

source§

impl<'a> OctetsRef for &'a Bytes

§

type Range = Bytes

source§

fn range(self, start: usize, end: usize) -> Self::Range

source§

impl<'a> OctetsRef for &'a Vec<u8>

§

type Range = &'a [u8]

source§

fn range(self, start: usize, end: usize) -> Self::Range

source§

impl<'a, A: Array<Item = u8>> OctetsRef for &'a SmallVec<A>

§

type Range = &'a [u8]

source§

fn range(self, start: usize, end: usize) -> Self::Range

source§

impl<'a> OctetsRef for &'a [u8]

§

type Range = &'a [u8]

source§

fn range(self, start: usize, end: usize) -> Self::Range

source§

impl<'a, 's> OctetsRef for &'a Cow<'s, [u8]>

§

type Range = &'a [u8]

source§

fn range(self, start: usize, end: usize) -> Self::Range

Implementors§