zerocopy

Trait AsBytes

Source
pub unsafe trait AsBytes {
    // Provided methods
    fn as_bytes(&self) -> &[u8]  { ... }
    fn as_bytes_mut(&mut self) -> &mut [u8] 
       where Self: FromBytes { ... }
    fn write_to(&self, bytes: &mut [u8]) -> Option<()> { ... }
    fn write_to_prefix(&self, bytes: &mut [u8]) -> Option<()> { ... }
    fn write_to_suffix(&self, bytes: &mut [u8]) -> Option<()> { ... }
}
Expand description

Types that can be viewed as an immutable slice of initialized bytes.

Any AsBytes type can be viewed as a slice of initialized bytes of the same size. This is useful for efficiently serializing structured data as raw bytes.

§Implementation

Do not implement this trait yourself! Instead, use #[derive(AsBytes)] (requires the derive Cargo feature); e.g.:

#[derive(AsBytes)]
#[repr(C)]
struct MyStruct {
    ...
}

#[derive(AsBytes)]
#[repr(u8)]
enum MyEnum {
    ...
}

#[derive(AsBytes)]
#[repr(C)]
union MyUnion {
    ...
}

This derive performs a sophisticated, compile-time safety analysis to determine whether a type is AsBytes. See the derive documentation for guidance on how to interpret error messages produced by the derive’s analysis.

§Safety

This section describes what is required in order for T: AsBytes, and what unsafe code may assume of such types. If you don’t plan on implementing AsBytes manually, and you don’t plan on writing unsafe code that operates on AsBytes types, then you don’t need to read this section.

If T: AsBytes, then unsafe code may assume that:

  • It is sound to treat any t: T as an immutable [u8] of length size_of_val(t).
  • Given t: &T, it is sound to construct a b: &[u8] where b.len() == size_of_val(t) at the same address as t, and it is sound for both b and t to be live at the same time.

If a type is marked as AsBytes which violates this contract, it may cause undefined behavior.

#[derive(AsBytes)] only permits types which satisfy these requirements.

Provided Methods§

Source

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

Gets the bytes of this value.

as_bytes provides access to the bytes of this value as an immutable byte slice.

§Examples
use zerocopy::AsBytes;

#[derive(AsBytes)]
#[repr(C)]
struct PacketHeader {
    src_port: [u8; 2],
    dst_port: [u8; 2],
    length: [u8; 2],
    checksum: [u8; 2],
}

let header = PacketHeader {
    src_port: [0, 1],
    dst_port: [2, 3],
    length: [4, 5],
    checksum: [6, 7],
};

let bytes = header.as_bytes();

assert_eq!(bytes, [0, 1, 2, 3, 4, 5, 6, 7]);
Source

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

Gets the bytes of this value mutably.

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

§Examples
use zerocopy::AsBytes;

#[derive(AsBytes, FromZeroes, FromBytes)]
#[repr(C)]
struct PacketHeader {
    src_port: [u8; 2],
    dst_port: [u8; 2],
    length: [u8; 2],
    checksum: [u8; 2],
}

let mut header = PacketHeader {
    src_port: [0, 1],
    dst_port: [2, 3],
    length: [4, 5],
    checksum: [6, 7],
};

let bytes = header.as_bytes_mut();

assert_eq!(bytes, [0, 1, 2, 3, 4, 5, 6, 7]);

bytes.reverse();

assert_eq!(header, PacketHeader {
    src_port: [7, 6],
    dst_port: [5, 4],
    length: [3, 2],
    checksum: [1, 0],
});
Source

fn write_to(&self, bytes: &mut [u8]) -> Option<()>

Writes a copy of self to bytes.

If bytes.len() != size_of_val(self), write_to returns None.

§Examples
use zerocopy::AsBytes;

#[derive(AsBytes)]
#[repr(C)]
struct PacketHeader {
    src_port: [u8; 2],
    dst_port: [u8; 2],
    length: [u8; 2],
    checksum: [u8; 2],
}

let header = PacketHeader {
    src_port: [0, 1],
    dst_port: [2, 3],
    length: [4, 5],
    checksum: [6, 7],
};

let mut bytes = [0, 0, 0, 0, 0, 0, 0, 0];

header.write_to(&mut bytes[..]);

assert_eq!(bytes, [0, 1, 2, 3, 4, 5, 6, 7]);

If too many or too few target bytes are provided, write_to returns None and leaves the target bytes unmodified:

let mut excessive_bytes = &mut [0u8; 128][..];

let write_result = header.write_to(excessive_bytes);

assert!(write_result.is_none());
assert_eq!(excessive_bytes, [0u8; 128]);
Source

fn write_to_prefix(&self, bytes: &mut [u8]) -> Option<()>

Writes a copy of self to the prefix of bytes.

write_to_prefix writes self to the first size_of_val(self) bytes of bytes. If bytes.len() < size_of_val(self), it returns None.

§Examples
use zerocopy::AsBytes;

#[derive(AsBytes)]
#[repr(C)]
struct PacketHeader {
    src_port: [u8; 2],
    dst_port: [u8; 2],
    length: [u8; 2],
    checksum: [u8; 2],
}

let header = PacketHeader {
    src_port: [0, 1],
    dst_port: [2, 3],
    length: [4, 5],
    checksum: [6, 7],
};

let mut bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

header.write_to_prefix(&mut bytes[..]);

assert_eq!(bytes, [0, 1, 2, 3, 4, 5, 6, 7, 0, 0]);

If insufficient target bytes are provided, write_to_prefix returns None and leaves the target bytes unmodified:

let mut insufficent_bytes = &mut [0, 0][..];

let write_result = header.write_to_suffix(insufficent_bytes);

assert!(write_result.is_none());
assert_eq!(insufficent_bytes, [0, 0]);
Source

fn write_to_suffix(&self, bytes: &mut [u8]) -> Option<()>

Writes a copy of self to the suffix of bytes.

write_to_suffix writes self to the last size_of_val(self) bytes of bytes. If bytes.len() < size_of_val(self), it returns None.

§Examples
use zerocopy::AsBytes;

#[derive(AsBytes)]
#[repr(C)]
struct PacketHeader {
    src_port: [u8; 2],
    dst_port: [u8; 2],
    length: [u8; 2],
    checksum: [u8; 2],
}

let header = PacketHeader {
    src_port: [0, 1],
    dst_port: [2, 3],
    length: [4, 5],
    checksum: [6, 7],
};

let mut bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

header.write_to_suffix(&mut bytes[..]);

assert_eq!(bytes, [0, 0, 0, 1, 2, 3, 4, 5, 6, 7]);

let mut insufficent_bytes = &mut [0, 0][..];

let write_result = header.write_to_suffix(insufficent_bytes);

assert!(write_result.is_none());
assert_eq!(insufficent_bytes, [0, 0]);

If insufficient target bytes are provided, write_to_suffix returns None and leaves the target bytes unmodified:

let mut insufficent_bytes = &mut [0, 0][..];

let write_result = header.write_to_suffix(insufficent_bytes);

assert!(write_result.is_none());
assert_eq!(insufficent_bytes, [0, 0]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl AsBytes for Option<NonZeroI8>

Source§

impl AsBytes for Option<NonZeroI16>

Source§

impl AsBytes for Option<NonZeroI32>

Source§

impl AsBytes for Option<NonZeroI64>

Source§

impl AsBytes for Option<NonZeroI128>

Source§

impl AsBytes for Option<NonZeroIsize>

Source§

impl AsBytes for Option<NonZeroU8>

Source§

impl AsBytes for Option<NonZeroU16>

Source§

impl AsBytes for Option<NonZeroU32>

Source§

impl AsBytes for Option<NonZeroU64>

Source§

impl AsBytes for Option<NonZeroU128>

Source§

impl AsBytes for Option<NonZeroUsize>

Source§

impl AsBytes for bool

Source§

impl AsBytes for char

Source§

impl AsBytes for f32

Source§

impl AsBytes for f64

Source§

impl AsBytes for i8

Source§

impl AsBytes for i16

Source§

impl AsBytes for i32

Source§

impl AsBytes for i64

Source§

impl AsBytes for i128

Source§

impl AsBytes for isize

Source§

impl AsBytes for str

Source§

impl AsBytes for u8

Source§

impl AsBytes for u16

Source§

impl AsBytes for u32

Source§

impl AsBytes for u64

Source§

impl AsBytes for u128

Source§

impl AsBytes for ()

Source§

impl AsBytes for usize

Source§

impl AsBytes for float64x1_t

Source§

impl AsBytes for float64x2_t

Source§

impl AsBytes for float32x2_t

Source§

impl AsBytes for float32x4_t

Source§

impl AsBytes for int8x8_t

Source§

impl AsBytes for int8x8x2_t

Source§

impl AsBytes for int8x8x3_t

Source§

impl AsBytes for int8x8x4_t

Source§

impl AsBytes for int8x16_t

Source§

impl AsBytes for int8x16x2_t

Source§

impl AsBytes for int8x16x3_t

Source§

impl AsBytes for int8x16x4_t

Source§

impl AsBytes for int16x4_t

Source§

impl AsBytes for int16x8_t

Source§

impl AsBytes for int32x2_t

Source§

impl AsBytes for int32x4_t

Source§

impl AsBytes for int64x1_t

Source§

impl AsBytes for int64x2_t

Source§

impl AsBytes for poly8x8_t

Source§

impl AsBytes for poly8x8x2_t

Source§

impl AsBytes for poly8x8x3_t

Source§

impl AsBytes for poly8x8x4_t

Source§

impl AsBytes for poly8x16_t

Source§

impl AsBytes for poly8x16x2_t

Source§

impl AsBytes for poly8x16x3_t

Source§

impl AsBytes for poly8x16x4_t

Source§

impl AsBytes for poly16x4_t

Source§

impl AsBytes for poly16x8_t

Source§

impl AsBytes for poly64x1_t

Source§

impl AsBytes for poly64x2_t

Source§

impl AsBytes for uint8x8_t

Source§

impl AsBytes for uint8x8x2_t

Source§

impl AsBytes for uint8x8x3_t

Source§

impl AsBytes for uint8x8x4_t

Source§

impl AsBytes for uint8x16_t

Source§

impl AsBytes for uint8x16x2_t

Source§

impl AsBytes for uint8x16x3_t

Source§

impl AsBytes for uint8x16x4_t

Source§

impl AsBytes for uint16x4_t

Source§

impl AsBytes for uint16x8_t

Source§

impl AsBytes for uint32x2_t

Source§

impl AsBytes for uint32x4_t

Source§

impl AsBytes for uint64x1_t

Source§

impl AsBytes for uint64x2_t

Source§

impl AsBytes for NonZeroI8

Source§

impl AsBytes for NonZeroI16

Source§

impl AsBytes for NonZeroI32

Source§

impl AsBytes for NonZeroI64

Source§

impl AsBytes for NonZeroI128

Source§

impl AsBytes for NonZeroIsize

Source§

impl AsBytes for NonZeroU8

Source§

impl AsBytes for NonZeroU16

Source§

impl AsBytes for NonZeroU32

Source§

impl AsBytes for NonZeroU64

Source§

impl AsBytes for NonZeroU128

Source§

impl AsBytes for NonZeroUsize

Source§

impl<T: AsBytes> AsBytes for [T]

Source§

impl<T: AsBytes> AsBytes for Wrapping<T>

Source§

impl<T: ?Sized + AsBytes> AsBytes for ManuallyDrop<T>

Source§

impl<T: ?Sized> AsBytes for PhantomData<T>

Source§

impl<const N: usize, T: AsBytes> AsBytes for [T; N]

Implementors§

Source§

impl<O> AsBytes for F32<O>
where [u8; 4]: AsBytes, PhantomData<O>: AsBytes,

Source§

impl<O> AsBytes for F64<O>
where [u8; 8]: AsBytes, PhantomData<O>: AsBytes,

Source§

impl<O> AsBytes for I16<O>
where [u8; 2]: AsBytes, PhantomData<O>: AsBytes,

Source§

impl<O> AsBytes for I32<O>
where [u8; 4]: AsBytes, PhantomData<O>: AsBytes,

Source§

impl<O> AsBytes for I64<O>
where [u8; 8]: AsBytes, PhantomData<O>: AsBytes,

Source§

impl<O> AsBytes for I128<O>
where [u8; 16]: AsBytes, PhantomData<O>: AsBytes,

Source§

impl<O> AsBytes for U16<O>
where [u8; 2]: AsBytes, PhantomData<O>: AsBytes,

Source§

impl<O> AsBytes for U32<O>
where [u8; 4]: AsBytes, PhantomData<O>: AsBytes,

Source§

impl<O> AsBytes for U64<O>
where [u8; 8]: AsBytes, PhantomData<O>: AsBytes,

Source§

impl<O> AsBytes for U128<O>
where [u8; 16]: AsBytes, PhantomData<O>: AsBytes,

Source§

impl<T> AsBytes for Unalign<T>
where T: AsBytes,