[][src]Trait zerocopy::AsBytes

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

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), or repr(packed)).
    • All of its fields must be AsBytes
    • Its layout must have no inter-field padding. This is always true for repr(transparent) and repr(packed). For repr(C), see the layout algorithm described in the Rust Reference.
  • 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 (reprs C, u8, u16, u32, u64, usize, i8, i16, i32, i64, or isize).

Provided methods

fn as_bytes(&self) -> &[u8]

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

Get the bytes of this value mutably.

as_bytes_mut provides access to the bytes of this value as a mutable byte slice.

Loading content...

Implementations on Foreign Types

impl<T> AsBytes for PhantomData<T>[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

Loading content...

Implementors

impl AsBytes for ()[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for i8[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for i16[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for i32[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for i64[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for i128[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for isize[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for u8[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for u16[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for u32[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for u64[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for u128[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl AsBytes for usize[src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 0][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 1][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 2][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 3][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 4][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 5][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 6][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 7][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 8][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 9][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 10][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 11][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 12][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 13][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 14][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 15][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 16][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 17][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 18][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 19][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 20][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 21][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 22][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 23][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 24][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 25][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 26][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 27][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 28][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 29][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 30][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 31][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T; 32][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

impl<T: AsBytes> AsBytes for [T][src]

fn as_bytes(&self) -> &[u8][src]

fn as_bytes_mut(&mut self) -> &mut [u8] where
    Self: FromBytes
[src]

Loading content...