[−][src]Struct zerocopy::LayoutVerified
A length- and alignment-checked reference to a byte slice which can safely be reinterpreted as another type.
LayoutVerified
is a byte slice reference (&[u8]
, &mut [u8]
,
Ref<[u8]>
, RefMut<[u8]>
, etc) with the invaraint that the slice's length
and alignment are each greater than or equal to the length and alignment of
T
. Using this invariant, it implements Deref
for T
so long as T: FromBytes
and DerefMut
so long as T: FromBytes + AsBytes
.
Examples
LayoutVerified
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::{AsBytes, ByteSlice, ByteSliceMut, FromBytes, LayoutVerified, Unaligned}; #[derive(FromBytes, AsBytes, Unaligned)] #[repr(C)] struct UdpHeader { src_port: [u8; 2], dst_port: [u8; 2], length: [u8; 2], checksum: [u8; 2], } struct UdpPacket<B> { header: LayoutVerified<B, UdpHeader>, body: B, } impl<B: ByteSlice> UdpPacket<B> { pub fn parse(bytes: B) -> Option<UdpPacket<B>> { let (header, body) = LayoutVerified::new_unaligned_from_prefix(bytes)?; Some(UdpPacket { header, body }) } pub fn get_src_port(&self) -> [u8; 2] { self.header.src_port } } impl<B: ByteSliceMut> UdpPacket<B> { pub fn set_src_port(&mut self, src_port: [u8; 2]) { self.header.src_port = src_port; } }
Methods
impl<B, T> LayoutVerified<B, T> where
B: ByteSlice,
[src]
B: ByteSlice,
pub fn new(bytes: B) -> Option<LayoutVerified<B, T>>
[src]
Construct a new LayoutVerified
.
new
verifies that bytes.len() == size_of::<T>()
and that bytes
is
aligned to align_of::<T>()
, and constructs a new LayoutVerified
. If
either of these checks fail, it returns None
.
pub fn new_from_prefix(bytes: B) -> Option<(LayoutVerified<B, T>, B)>
[src]
Construct a new LayoutVerified
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 LayoutVerified
, and
returns the remaining bytes to the caller. If either the length or
alignment checks fail, it returns None
.
pub fn new_from_suffix(bytes: B) -> Option<(B, LayoutVerified<B, T>)>
[src]
Construct a new LayoutVerified
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 LayoutVerified
, and returns the preceding bytes
to the caller. If either the length or alignment checks fail, it returns
None
.
impl<B, T> LayoutVerified<B, T> where
B: ByteSlice,
T: ?Sized,
[src]
B: ByteSlice,
T: ?Sized,
impl<B, T> LayoutVerified<B, [T]> where
B: ByteSlice,
[src]
B: ByteSlice,
pub fn new_slice(bytes: B) -> Option<LayoutVerified<B, [T]>>
[src]
Construct a new LayoutVerified
of a slice type.
new_slice
verifies that bytes.len()
is a multiple of
size_of::<T>()
and that bytes
is aligned to align_of::<T>()
, and
constructs a new LayoutVerified
. If either of these checks fail, it
returns None
.
Panics
new_slice
panics if T
is a zero-sized type.
impl<B, T> LayoutVerified<B, T> where
B: ByteSliceMut,
[src]
B: ByteSliceMut,
pub fn new_zeroed(bytes: B) -> Option<LayoutVerified<B, T>>
[src]
Construct a new LayoutVerified
after zeroing the bytes.
new_zeroed
verifies that bytes.len() == size_of::<T>()
and that
bytes
is aligned to align_of::<T>()
, and constructs a new
LayoutVerified
. If either of these checks fail, it returns None
.
If the checks succeed, then bytes
will be initialized to zero. This
can be useful when re-using buffers to ensure that sensitive data
previously stored in the buffer is not leaked.
pub fn new_from_prefix_zeroed(bytes: B) -> Option<(LayoutVerified<B, T>, B)>
[src]
Construct a new LayoutVerified
from the prefix of a byte slice,
zeroing the prefix.
new_from_prefix_zeroed
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 LayoutVerified
, and
returns the remaining bytes to the caller. If either the length or
alignment checks fail, it returns None
.
If the checks succeed, then the prefix which is consumed will be initialized to zero. This can be useful when re-using buffers to ensure that sensitive data previously stored in the buffer is not leaked.
pub fn new_from_suffix_zeroed(bytes: B) -> Option<(B, LayoutVerified<B, T>)>
[src]
Construct a new LayoutVerified
from the suffix of a byte slice,
zeroing the suffix.
new_from_suffix_zeroed
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 LayoutVerified
, and returns the preceding bytes
to the caller. If either the length or alignment checks fail, it returns
None
.
If the checks succeed, then the suffix which is consumed will be initialized to zero. This can be useful when re-using buffers to ensure that sensitive data previously stored in the buffer is not leaked.
impl<B, T> LayoutVerified<B, [T]> where
B: ByteSliceMut,
[src]
B: ByteSliceMut,
pub fn new_slice_zeroed(bytes: B) -> Option<LayoutVerified<B, [T]>>
[src]
Construct a new LayoutVerified
of a slice type after zeroing the
bytes.
new_slice_zeroed
verifies that bytes.len()
is a multiple of
size_of::<T>()
and that bytes
is aligned to align_of::<T>()
, and
constructs a new LayoutVerified
. If either of these checks fail, it
returns None
.
If the checks succeed, then bytes
will be initialized to zero. This
can be useful when re-using buffers to ensure that sensitive data
previously stored in the buffer is not leaked.
Panics
new_slice
panics if T
is a zero-sized type.
impl<B, T> LayoutVerified<B, T> where
B: ByteSlice,
T: Unaligned,
[src]
B: ByteSlice,
T: Unaligned,
pub fn new_unaligned(bytes: B) -> Option<LayoutVerified<B, T>>
[src]
Construct a new LayoutVerified
for a type with no alignment
requirement.
new_unaligned
verifies that bytes.len() == size_of::<T>()
and
constructs a new LayoutVerified
. If the check fails, it returns
None
.
pub fn new_unaligned_from_prefix(bytes: B) -> Option<(LayoutVerified<B, T>, B)>
[src]
Construct a new LayoutVerified
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 LayoutVerified
, and returns the remaining bytes
to the caller. If the length check fails, it returns None
.
pub fn new_unaligned_from_suffix(bytes: B) -> Option<(B, LayoutVerified<B, T>)>
[src]
Construct a new LayoutVerified
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 LayoutVerified
, and returns the preceding bytes
to the caller. If the length check fails, it returns None
.
impl<B, T> LayoutVerified<B, [T]> where
B: ByteSlice,
T: Unaligned,
[src]
B: ByteSlice,
T: Unaligned,
pub fn new_slice_unaligned(bytes: B) -> Option<LayoutVerified<B, [T]>>
[src]
Construct a new LayoutVerified
of a slice type with no alignment
requirement.
new_slice_unaligned
verifies that bytes.len()
is a multiple of
size_of::<T>()
and constructs a new LayoutVerified
. If the check
fails, it returns None
.
Panics
new_slice
panics if T
is a zero-sized type.
impl<B, T> LayoutVerified<B, T> where
B: ByteSliceMut,
T: Unaligned,
[src]
B: ByteSliceMut,
T: Unaligned,
pub fn new_unaligned_zeroed(bytes: B) -> Option<LayoutVerified<B, T>>
[src]
Construct a new LayoutVerified
for a type with no alignment
requirement, zeroing the bytes.
new_unaligned_zeroed
verifies that bytes.len() == size_of::<T>()
and
constructs a new LayoutVerified
. If the check fails, it returns
None
.
If the check succeeds, then bytes
will be initialized to zero. This
can be useful when re-using buffers to ensure that sensitive data
previously stored in the buffer is not leaked.
pub fn new_unaligned_from_prefix_zeroed(
bytes: B
) -> Option<(LayoutVerified<B, T>, B)>
[src]
bytes: B
) -> Option<(LayoutVerified<B, T>, B)>
Construct a new LayoutVerified
from the prefix of a byte slice for a
type with no alignment requirement, zeroing the prefix.
new_unaligned_from_prefix_zeroed
verifies that bytes.len() >= size_of::<T>()
. It consumes the first size_of::<T>()
bytes from
bytes
to construct a LayoutVerified
, and returns the remaining bytes
to the caller. If the length check fails, it returns None
.
If the check succeeds, then the prefix which is consumed will be initialized to zero. This can be useful when re-using buffers to ensure that sensitive data previously stored in the buffer is not leaked.
pub fn new_unaligned_from_suffix_zeroed(
bytes: B
) -> Option<(B, LayoutVerified<B, T>)>
[src]
bytes: B
) -> Option<(B, LayoutVerified<B, T>)>
Construct a new LayoutVerified
from the suffix of a byte slice for a
type with no alignment requirement, zeroing the suffix.
new_unaligned_from_suffix_zeroed
verifies that bytes.len() >= size_of::<T>()
. It consumes the last size_of::<T>()
bytes from
bytes
to construct a LayoutVerified
, and returns the preceding bytes
to the caller. If the length check fails, it returns None
.
If the check succeeds, then the suffix which is consumed will be initialized to zero. This can be useful when re-using buffers to ensure that sensitive data previously stored in the buffer is not leaked.
impl<B, T> LayoutVerified<B, [T]> where
B: ByteSliceMut,
T: Unaligned,
[src]
B: ByteSliceMut,
T: Unaligned,
pub fn new_slice_unaligned_zeroed(bytes: B) -> Option<LayoutVerified<B, [T]>>
[src]
Construct a new LayoutVerified
for a slice type with no alignment
requirement, zeroing the bytes.
new_slice_unaligned_zeroed
verifies that bytes.len()
is a multiple
of size_of::<T>()
and constructs a new LayoutVerified
. If the check
fails, it returns None
.
If the check succeeds, then bytes
will be initialized to zero. This
can be useful when re-using buffers to ensure that sensitive data
previously stored in the buffer is not leaked.
Panics
new_slice
panics if T
is a zero-sized type.
impl<'a, B, T> LayoutVerified<B, T> where
B: 'a + ByteSlice,
T: FromBytes,
[src]
B: 'a + ByteSlice,
T: FromBytes,
pub fn into_ref(self) -> &'a T
[src]
Convert this LayoutVerified
into a reference.
into_ref
consumes the LayoutVerified
, and returns a reference to
T
.
impl<'a, B, T> LayoutVerified<B, T> where
B: 'a + ByteSliceMut,
T: FromBytes + AsBytes,
[src]
B: 'a + ByteSliceMut,
T: FromBytes + AsBytes,
pub fn into_mut(self) -> &'a mut T
[src]
Convert this LayoutVerified
into a mutable reference.
into_mut
consumes the LayoutVerified
, and returns a mutable
reference to T
.
impl<'a, B, T> LayoutVerified<B, [T]> where
B: 'a + ByteSlice,
T: FromBytes,
[src]
B: 'a + ByteSlice,
T: FromBytes,
pub fn into_slice(self) -> &'a [T]
[src]
Convert this LayoutVerified
into a slice reference.
into_slice
consumes the LayoutVerified
, and returns a reference to
[T]
.
impl<'a, B, T> LayoutVerified<B, [T]> where
B: 'a + ByteSliceMut,
T: FromBytes + AsBytes,
[src]
B: 'a + ByteSliceMut,
T: FromBytes + AsBytes,
pub fn into_mut_slice(self) -> &'a mut [T]
[src]
Convert this LayoutVerified
into a mutable slice reference.
into_mut_slice
consumes the LayoutVerified
, and returns a mutable reference to
[T]
.
impl<B, T> LayoutVerified<B, T> where
B: ByteSliceMut,
T: ?Sized,
[src]
B: ByteSliceMut,
T: ?Sized,
Trait Implementations
impl<B, T> DerefMut for LayoutVerified<B, T> where
B: ByteSliceMut,
T: FromBytes + AsBytes,
[src]
B: ByteSliceMut,
T: FromBytes + AsBytes,
impl<B, T> DerefMut for LayoutVerified<B, [T]> where
B: ByteSliceMut,
T: FromBytes + AsBytes,
[src]
B: ByteSliceMut,
T: FromBytes + AsBytes,
impl<B, T> Deref for LayoutVerified<B, T> where
B: ByteSlice,
T: FromBytes,
[src]
B: ByteSlice,
T: FromBytes,
impl<B, T> Deref for LayoutVerified<B, [T]> where
B: ByteSlice,
T: FromBytes,
[src]
B: ByteSlice,
T: FromBytes,
impl<T, B> Display for LayoutVerified<B, T> where
B: ByteSlice,
T: FromBytes + Display,
[src]
B: ByteSlice,
T: FromBytes + Display,
impl<T, B> Display for LayoutVerified<B, [T]> where
B: ByteSlice,
T: FromBytes,
[T]: Display,
[src]
B: ByteSlice,
T: FromBytes,
[T]: Display,
impl<T, B> Debug for LayoutVerified<B, T> where
B: ByteSlice,
T: FromBytes + Debug,
[src]
B: ByteSlice,
T: FromBytes + Debug,
impl<T, B> Debug for LayoutVerified<B, [T]> where
B: ByteSlice,
T: FromBytes + Debug,
[src]
B: ByteSlice,
T: FromBytes + Debug,
Auto Trait Implementations
impl<B, T: ?Sized> Unpin for LayoutVerified<B, T> where
B: Unpin,
T: Unpin,
B: Unpin,
T: Unpin,
impl<B, T: ?Sized> Send for LayoutVerified<B, T> where
B: Send,
T: Send,
B: Send,
T: Send,
impl<B, T: ?Sized> Sync for LayoutVerified<B, T> where
B: Sync,
T: Sync,
B: Sync,
T: Sync,
impl<B, T: ?Sized> UnwindSafe for LayoutVerified<B, T> where
B: UnwindSafe,
T: UnwindSafe,
B: UnwindSafe,
T: UnwindSafe,
impl<B, T: ?Sized> RefUnwindSafe for LayoutVerified<B, T> where
B: RefUnwindSafe,
T: RefUnwindSafe,
B: RefUnwindSafe,
T: RefUnwindSafe,
Blanket Implementations
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> ToString for T where
T: Display + ?Sized,
[src]
T: Display + ?Sized,