pub type UnalignedView<B, T> = Ref<B, Unalign<T>>;
Aliased Type§
struct UnalignedView<B, T>(/* private fields */);
Implementations
§impl<'a, B, T> Ref<B, T>where
B: 'a + IntoByteSlice<'a>,
T: FromBytes + KnownLayout + Immutable + ?Sized,
impl<'a, B, T> Ref<B, T>where
B: 'a + IntoByteSlice<'a>,
T: FromBytes + KnownLayout + Immutable + ?Sized,
pub fn into_ref(r: Ref<B, T>) -> &'a T
pub fn into_ref(r: Ref<B, T>) -> &'a T
Converts this Ref
into a reference.
into_ref
consumes the Ref
, and returns a reference to T
.
Note: this is an associated function, which means that you have to call
it as Ref::into_ref(r)
instead of r.into_ref()
. This is so that
there is no conflict with a method on the inner type.
§impl<'a, B, T> Ref<B, T>where
B: 'a + IntoByteSliceMut<'a>,
T: FromBytes + IntoBytes + KnownLayout + ?Sized,
impl<'a, B, T> Ref<B, T>where
B: 'a + IntoByteSliceMut<'a>,
T: FromBytes + IntoBytes + KnownLayout + ?Sized,
pub fn into_mut(r: Ref<B, T>) -> &'a mut T
pub fn into_mut(r: Ref<B, T>) -> &'a mut T
Converts this Ref
into a mutable reference.
into_mut
consumes the Ref
, and returns a mutable reference to T
.
Note: this is an associated function, which means that you have to call
it as Ref::into_mut(r)
instead of r.into_mut()
. This is so that
there is no conflict with a method on the inner type.
§impl<B, T> Ref<B, T>where
B: ByteSlice,
T: FromBytes,
impl<B, T> Ref<B, T>where
B: ByteSlice,
T: FromBytes,
pub fn read(r: &Ref<B, T>) -> T
pub fn read(r: &Ref<B, T>) -> T
Reads a copy of T
.
Note: this is an associated function, which means that you have to call
it as Ref::read(r)
instead of r.read()
. This is so that there is no
conflict with a method on the inner type.
§impl<B, T> Ref<B, T>where
B: ByteSlice,
T: KnownLayout + Immutable + ?Sized,
impl<B, T> Ref<B, T>where
B: ByteSlice,
T: KnownLayout + Immutable + ?Sized,
pub fn from_bytes(
source: B,
) -> Result<Ref<B, T>, ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
pub fn from_bytes( source: B, ) -> Result<Ref<B, T>, ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
Constructs a Ref
from a byte slice.
If the length of source
is not a valid size of T
, or
if source
is not appropriately aligned for T
, this returns Err
. If
T: Unaligned
, you can infallibly discard the alignment
error.
T
may be a sized type, a slice, or a slice DST.
§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>::from_bytes(&b"UU"[..]); // ⚠ Compile Error!
§impl<B, T> Ref<B, T>
impl<B, T> Ref<B, T>
pub fn from_bytes_with_elems(
source: B,
count: usize,
) -> Result<Ref<B, T>, ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
pub fn from_bytes_with_elems( source: B, count: usize, ) -> Result<Ref<B, T>, ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
Constructs a Ref
from the given bytes with DST length equal to count
without copying.
This method attempts to return a Ref
to the prefix of source
interpreted as a T
with count
trailing elements, and a reference to
the remaining bytes. If the length of source
is not equal to the size
of Self
with count
elements, or if source
is not appropriately
aligned, this returns Err
. If T: Unaligned
, you can
infallibly discard the alignment error.
§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>::from_bytes_with_elems(&b"UU"[..], 42); // ⚠ Compile Error!
§impl<B, T> Ref<B, T>where
B: ByteSliceMut,
T: IntoBytes,
impl<B, T> Ref<B, T>where
B: ByteSliceMut,
T: IntoBytes,
pub fn write(r: &mut Ref<B, T>, t: T)
pub fn write(r: &mut Ref<B, T>, t: T)
Writes the bytes of t
and then forgets t
.
Note: this is an associated function, which means that you have to call
it as Ref::write(r, t)
instead of r.write(t)
. This is so that there
is no conflict with a method on the inner type.
§impl<B, T> Ref<B, T>where
B: SplitByteSlice,
T: KnownLayout + Immutable + ?Sized,
impl<B, T> Ref<B, T>where
B: SplitByteSlice,
T: KnownLayout + Immutable + ?Sized,
pub fn from_prefix(
source: B,
) -> Result<(Ref<B, T>, B), ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
pub fn from_prefix( source: B, ) -> Result<(Ref<B, T>, B), ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
Constructs a Ref
from the prefix of a byte slice.
This method computes the largest possible size of T
that
can fit in the leading bytes of source
, then attempts to return both a
Ref
to those bytes, and a reference to the remaining bytes. If there
are insufficient bytes, or if source
is not appropriately aligned,
this returns Err
. If T: Unaligned
, you can
infallibly discard the alignment error.
T
may be a sized type, a slice, or a slice DST.
§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>::from_prefix(&b"UU"[..]); // ⚠ Compile Error!
pub fn from_suffix(
source: B,
) -> Result<(B, Ref<B, T>), ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
pub fn from_suffix( source: B, ) -> Result<(B, Ref<B, T>), ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
Constructs a Ref
from the suffix of a byte slice.
This method computes the largest possible size of T
that
can fit in the trailing bytes of source
, then attempts to return both
a Ref
to those bytes, and a reference to the preceding bytes. If there
are insufficient bytes, or if that suffix of source
is not
appropriately aligned, this returns Err
. If T: Unaligned
, you can infallibly discard the alignment
error.
T
may be a sized type, a slice, or a slice DST.
§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>::from_suffix(&b"UU"[..]); // ⚠ Compile Error!
§impl<B, T> Ref<B, T>
impl<B, T> Ref<B, T>
pub fn from_prefix_with_elems(
source: B,
count: usize,
) -> Result<(Ref<B, T>, B), ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
pub fn from_prefix_with_elems( source: B, count: usize, ) -> Result<(Ref<B, T>, B), ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
Constructs a Ref
from the prefix of the given bytes with DST
length equal to count
without copying.
This method attempts to return a Ref
to the prefix of source
interpreted as a T
with count
trailing elements, and a reference to
the remaining bytes. If there are insufficient bytes, or if source
is
not appropriately aligned, this returns Err
. If T: Unaligned
, you can infallibly discard the alignment
error.
§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>::from_prefix_with_elems(&b"UU"[..], 42); // ⚠ Compile Error!
pub fn from_suffix_with_elems(
source: B,
count: usize,
) -> Result<(B, Ref<B, T>), ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
pub fn from_suffix_with_elems( source: B, count: usize, ) -> Result<(B, Ref<B, T>), ConvertError<AlignmentError<B, T>, SizeError<B, T>, Infallible>>
Constructs a Ref
from the suffix of the given bytes with DST length
equal to count
without copying.
This method attempts to return a Ref
to the suffix of source
interpreted as a T
with count
trailing elements, and a reference to
the preceding bytes. If there are insufficient bytes, or if that suffix
of source
is not appropriately aligned, this returns Err
. If T: Unaligned
, you can infallibly discard the alignment
error.
§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>::from_suffix_with_elems(&b"UU"[..], 42); // ⚠ Compile Error!