pub struct Ref<B, T: ?Sized>(/* private fields */);Expand description
A typed reference derived from a byte slice.
A Ref<B, T> is a reference to a T which is stored in a byte slice, B.
Unlike a native reference (&T or &mut T), Ref<B, T> has the same
mutability as the byte slice it was constructed from (B).
§Examples
Ref can be used to treat a sequence of bytes as a structured type, and to
read and write the fields of that type as if the byte slice reference were
simply a reference to that type.
use zerocopy::{IntoBytes, ByteSliceMut, FromBytes, FromZeros, KnownLayout, Immutable, Ref, SplitByteSlice, Unaligned};
#[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)]
#[repr(C)]
struct UdpHeader {
src_port: [u8; 2],
dst_port: [u8; 2],
length: [u8; 2],
checksum: [u8; 2],
}
struct UdpPacket<B> {
header: Ref<B, UdpHeader>,
body: B,
}
impl<B: SplitByteSlice> UdpPacket<B> {
pub fn parse(bytes: B) -> Option<UdpPacket<B>> {
let (header, body) = Ref::new_unaligned_from_prefix(bytes).ok()?;
Some(UdpPacket { header, body })
}
pub fn get_src_port(&self) -> [u8; 2] {
self.header.src_port
}
}
impl<B: ByteSliceMut> UdpPacket<B> {
pub fn with_src_port(&mut self, src_port: [u8; 2]) {
self.header.src_port = src_port;
}
}Implementations§
source§impl<B, T> Ref<B, T>
impl<B, T> Ref<B, T>
sourcepub fn new(bytes: B) -> Result<Ref<B, T>, CastError<B, T>>
pub fn new(bytes: B) -> Result<Ref<B, T>, CastError<B, T>>
Constructs a new Ref.
new verifies that bytes.len() == size_of::<T>() and that bytes is
aligned to align_of::<T>(), and constructs a new Ref. If either of
these checks fail, it returns None.
§Compile-Time Assertions
This method cannot yet be used on unsized types whose dynamically-sized component is zero-sized. Attempting to use this method on such types results in a compile-time assertion error; e.g.:
use zerocopy::*;
#[derive(Immutable, KnownLayout)]
#[repr(C)]
struct ZSTy {
leading_sized: u16,
trailing_dst: [()],
}
let _ = Ref::<_, ZSTy>::new(&b"UU"[..]); // ⚠ Compile Error!source§impl<B, T> Ref<B, T>
impl<B, T> Ref<B, T>
sourcepub fn new_from_prefix(bytes: B) -> Result<(Ref<B, T>, B), CastError<B, T>>
pub fn new_from_prefix(bytes: B) -> Result<(Ref<B, T>, B), CastError<B, T>>
Constructs a new Ref from the prefix of a byte slice.
new_from_prefix verifies that bytes.len() >= size_of::<T>() and that
bytes is aligned to align_of::<T>(). It consumes the first
size_of::<T>() bytes from bytes to construct a Ref, and returns
the remaining bytes to the caller. If either the length or alignment
checks fail, it returns None.
§Compile-Time Assertions
This method cannot yet be used on unsized types whose dynamically-sized component is zero-sized. Attempting to use this method on such types results in a compile-time assertion error; e.g.:
use zerocopy::*;
#[derive(Immutable, KnownLayout)]
#[repr(C)]
struct ZSTy {
leading_sized: u16,
trailing_dst: [()],
}
let _ = Ref::<_, ZSTy>::new_from_prefix(&b"UU"[..]); // ⚠ Compile Error!sourcepub fn new_from_suffix(bytes: B) -> Result<(B, Ref<B, T>), CastError<B, T>>
pub fn new_from_suffix(bytes: B) -> Result<(B, Ref<B, T>), CastError<B, T>>
Constructs a new Ref from the suffix of a byte slice.
new_from_suffix verifies that bytes.len() >= size_of::<T>() and that
the last size_of::<T>() bytes of bytes are aligned to
align_of::<T>(). It consumes the last size_of::<T>() bytes from
bytes to construct a Ref, and returns the preceding bytes to the
caller. If either the length or alignment checks fail, it returns
None.
§Compile-Time Assertions
This method cannot yet be used on unsized types whose dynamically-sized component is zero-sized. Attempting to use this method on such types results in a compile-time assertion error; e.g.:
use zerocopy::*;
#[derive(Immutable, KnownLayout)]
#[repr(C)]
struct ZSTy {
leading_sized: u16,
trailing_dst: [()],
}
let _ = Ref::<_, ZSTy>::new_from_suffix(&b"UU"[..]); // ⚠ Compile Error!source§impl<B, T> Ref<B, T>
impl<B, T> Ref<B, T>
sourcepub fn new_unaligned(bytes: B) -> Result<Ref<B, T>, SizeError<B, T>>
pub fn new_unaligned(bytes: B) -> Result<Ref<B, T>, SizeError<B, T>>
Constructs a new Ref for a type with no alignment requirement.
new_unaligned verifies that bytes.len() == size_of::<T>() and
constructs a new Ref. If the check fails, it returns None.
§Compile-Time Assertions
This method cannot yet be used on unsized types whose dynamically-sized component is zero-sized. Attempting to use this method on such types results in a compile-time assertion error; e.g.:
use zerocopy::*;
#[derive(Immutable, KnownLayout, Unaligned)]
#[repr(C, packed)]
struct ZSTy {
leading_sized: u16,
trailing_dst: [()],
}
let f = Ref::<&[u8], ZSTy>::new_unaligned(&b"UU"[..]); // ⚠ Compile Error!source§impl<B, T> Ref<B, T>
impl<B, T> Ref<B, T>
sourcepub fn new_unaligned_from_prefix(
bytes: B
) -> Result<(Ref<B, T>, B), SizeError<B, T>>
pub fn new_unaligned_from_prefix( bytes: B ) -> Result<(Ref<B, T>, B), SizeError<B, T>>
Constructs a new Ref from the prefix of a byte slice for a type with
no alignment requirement.
new_unaligned_from_prefix verifies that bytes.len() >= size_of::<T>(). It consumes the first size_of::<T>() bytes from
bytes to construct a Ref, and returns the remaining bytes to the
caller. If the length check fails, it returns None.
§Compile-Time Assertions
This method cannot yet be used on unsized types whose dynamically-sized component is zero-sized. Attempting to use this method on such types results in a compile-time assertion error; e.g.:
use zerocopy::*;
#[derive(Immutable, KnownLayout, Unaligned)]
#[repr(C, packed)]
struct ZSTy {
leading_sized: u16,
trailing_dst: [()],
}
let _ = Ref::<_, ZSTy>::new_unaligned_from_prefix(&b"UU"[..]); // ⚠ Compile Error!sourcepub fn new_unaligned_from_suffix(
bytes: B
) -> Result<(B, Ref<B, T>), SizeError<B, T>>
pub fn new_unaligned_from_suffix( bytes: B ) -> Result<(B, Ref<B, T>), SizeError<B, T>>
Constructs a new Ref from the suffix of a byte slice for a type with
no alignment requirement.
new_unaligned_from_suffix verifies that bytes.len() >= size_of::<T>(). It consumes the last size_of::<T>() bytes from
bytes to construct a Ref, and returns the preceding bytes to the
caller. If the length check fails, it returns None.
§Compile-Time Assertions
This method cannot yet be used on unsized types whose dynamically-sized component is zero-sized. Attempting to use this method on such types results in a compile-time assertion error; e.g.:
use zerocopy::*;
#[derive(Immutable, KnownLayout, Unaligned)]
#[repr(C, packed)]
struct ZSTy {
leading_sized: u16,
trailing_dst: [()],
}
let _ = Ref::<_, ZSTy>::new_unaligned_from_suffix(&b"UU"[..]); // ⚠ Compile Error!source§impl<B, T> Ref<B, T>where
B: ByteSliceMut,
T: ?Sized,
impl<B, T> Ref<B, T>where
B: ByteSliceMut,
T: ?Sized,
Trait Implementations§
source§impl<T, B> Ord for Ref<B, T>
impl<T, B> Ord for Ref<B, T>
source§impl<T, B> PartialEq for Ref<B, T>
impl<T, B> PartialEq for Ref<B, T>
source§impl<T, B> PartialOrd for Ref<B, T>
impl<T, B> PartialOrd for Ref<B, T>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read more