Trait zerocopy::AsBytes [−][src]
pub unsafe trait AsBytes { fn as_bytes(&self) -> &[u8] { ... } fn as_bytes_mut(&mut self) -> &mut [u8]
where
Self: FromBytes, { ... } }
Types which are safe to treat as an immutable byte slice.
WARNING: Do not implement this trait yourself! Instead, use
#[derive(AsBytes)]
.
AsBytes
types can be safely viewed as a slice of bytes. In particular,
this means that, in any valid instance of the type, none of the bytes of the
instance are uninitialized. This precludes the following types:
- Structs with internal padding
- Unions in which not all variants have the same length
AsBytes
is ignorant of byte order. For byte order-aware types, see the
byteorder
module.
Custom Derive Errors
Due to the way that the custom derive for AsBytes
is implemented, you may
get an error like this:
error[E0080]: evaluation of constant value failed
--> lib.rs:1:10
|
1 | #[derive(AsBytes)]
| ^^^^^^^ attempt to divide by zero
This error means that the type being annotated has padding bytes, which is
illegal for AsBytes
types. Consider either adding explicit struct fields
where those padding bytes would be or using #[repr(packed)]
.
Safety
If T: AsBytes
, then unsafe code may assume that it is sound to treat any
instance of the type as an immutable [u8]
of length size_of::<T>()
. If a
type is marked as AsBytes
which violates this contract, it may cause
undefined behavior.
If a type has the following properties, then it is safe to implement
AsBytes
for that type
- If the type is a struct:
- It must have a defined representation (
repr(C)
,repr(transparent)
, orrepr(packed)
). - All of its fields must be
AsBytes
- Its layout must have no padding. This is always true for
repr(transparent)
andrepr(packed)
. Forrepr(C)
, see the layout algorithm described in the Rust Reference.
- It must have a defined representation (
- If the type is an enum:
- It must be a C-like enum (meaning that all variants have no fields)
- It must have a defined representation (
repr
sC
,u8
,u16
,u32
,u64
,usize
,i8
,i16
,i32
,i64
, orisize
).
Provided methods
fn as_bytes(&self) -> &[u8]
[src]
Get the bytes of this value.
as_bytes
provides access to the bytes of this value as an immutable
byte slice.
fn as_bytes_mut(&mut self) -> &mut [u8] where
Self: FromBytes,
[src]
Self: FromBytes,
Get the bytes of this value mutably.
as_bytes_mut
provides access to the bytes of this value as a mutable
byte slice.
Implementations on Foreign Types
impl<T> AsBytes for PhantomData<T>
[src]
impl<T> AsBytes for PhantomData<T>
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
Implementors
impl AsBytes for ()
[src]
impl AsBytes for ()
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for bool
[src]
impl AsBytes for bool
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for f32
[src]
impl AsBytes for f32
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for f64
[src]
impl AsBytes for f64
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for i8
[src]
impl AsBytes for i8
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for i16
[src]
impl AsBytes for i16
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for i32
[src]
impl AsBytes for i32
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for i64
[src]
impl AsBytes for i64
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for i128
[src]
impl AsBytes for i128
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for isize
[src]
impl AsBytes for isize
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for u8
[src]
impl AsBytes for u8
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for u16
[src]
impl AsBytes for u16
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for u32
[src]
impl AsBytes for u32
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for u64
[src]
impl AsBytes for u64
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for u128
[src]
impl AsBytes for u128
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl AsBytes for usize
[src]
impl AsBytes for usize
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl<O: ByteOrder> AsBytes for I16<O>
[src]
impl<O: ByteOrder> AsBytes for I16<O>
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl<O: ByteOrder> AsBytes for I32<O>
[src]
impl<O: ByteOrder> AsBytes for I32<O>
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl<O: ByteOrder> AsBytes for I64<O>
[src]
impl<O: ByteOrder> AsBytes for I64<O>
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl<O: ByteOrder> AsBytes for I128<O>
[src]
impl<O: ByteOrder> AsBytes for I128<O>
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl<O: ByteOrder> AsBytes for U16<O>
[src]
impl<O: ByteOrder> AsBytes for U16<O>
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl<O: ByteOrder> AsBytes for U32<O>
[src]
impl<O: ByteOrder> AsBytes for U32<O>
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl<O: ByteOrder> AsBytes for U64<O>
[src]
impl<O: ByteOrder> AsBytes for U64<O>
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,
impl<O: ByteOrder> AsBytes for U128<O>
[src]
impl<O: ByteOrder> AsBytes for U128<O>
[src]fn only_derive_is_allowed_to_implement_this_trait() where
Self: Sized,
[src]
Self: Sized,