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§
Required Methods§
Provided Methods§
sourcefn range_from(self, start: usize) -> Self::Range
fn range_from(self, start: usize) -> Self::Range
Returns a range starting at index start
and going to the end.