Trait zerocopy::IntoBytes

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

Types that can be converted to an immutable slice of initialized bytes.

Any IntoBytes type can be converted to 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(IntoBytes)] (requires the derive Cargo feature); e.g.:

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

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

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

This derive performs a sophisticated, compile-time safety analysis to determine whether a type is IntoBytes. 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: IntoBytes, and what unsafe code may assume of such types. If you don’t plan on implementing IntoBytes manually, and you don’t plan on writing unsafe code that operates on IntoBytes types, then you don’t need to read this section.

If T: IntoBytes, then unsafe code may assume that it is sound to treat any t: T as an immutable [u8] of length size_of_val(t). If a type is marked as IntoBytes which violates this contract, it may cause undefined behavior.

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

Provided Methods§

source

fn as_bytes(&self) -> &[u8]
where Self: NoCell,

Gets the bytes of this value.

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

§Examples
use zerocopy::IntoBytes;

#[derive(IntoBytes, NoCell)]
#[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 + NoCell,

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::IntoBytes;

#[derive(FromZeros, FromBytes, IntoBytes, NoCell)]
#[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<()>
where Self: NoCell,

Writes a copy of self to bytes.

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

§Examples
use zerocopy::IntoBytes;

#[derive(IntoBytes, NoCell)]
#[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<()>
where Self: NoCell,

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::IntoBytes;

#[derive(IntoBytes, NoCell)]
#[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<()>
where Self: NoCell,

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::IntoBytes;

#[derive(IntoBytes, NoCell)]
#[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]);

Implementations on Foreign Types§

source§

impl IntoBytes for Option<NonZeroI8>

source§

impl IntoBytes for Option<NonZeroI16>

source§

impl IntoBytes for Option<NonZeroI32>

source§

impl IntoBytes for Option<NonZeroI64>

source§

impl IntoBytes for Option<NonZeroI128>

source§

impl IntoBytes for Option<NonZeroIsize>

source§

impl IntoBytes for Option<NonZeroU8>

source§

impl IntoBytes for Option<NonZeroU16>

source§

impl IntoBytes for Option<NonZeroU32>

source§

impl IntoBytes for Option<NonZeroU64>

source§

impl IntoBytes for Option<NonZeroU128>

source§

impl IntoBytes for Option<NonZeroUsize>

source§

impl IntoBytes for bool

source§

impl IntoBytes for char

source§

impl IntoBytes for f32

source§

impl IntoBytes for f64

source§

impl IntoBytes for i8

source§

impl IntoBytes for i16

source§

impl IntoBytes for i32

source§

impl IntoBytes for i64

source§

impl IntoBytes for i128

source§

impl IntoBytes for isize

source§

impl IntoBytes for str

source§

impl IntoBytes for u8

source§

impl IntoBytes for u16

source§

impl IntoBytes for u32

source§

impl IntoBytes for u64

source§

impl IntoBytes for u128

source§

impl IntoBytes for ()

source§

impl IntoBytes for usize

source§

impl IntoBytes for __m128

source§

impl IntoBytes for __m128d

source§

impl IntoBytes for __m128i

source§

impl IntoBytes for __m256

source§

impl IntoBytes for __m256d

source§

impl IntoBytes for __m256i

source§

impl IntoBytes for NonZeroI8

source§

impl IntoBytes for NonZeroI16

source§

impl IntoBytes for NonZeroI32

source§

impl IntoBytes for NonZeroI64

source§

impl IntoBytes for NonZeroI128

source§

impl IntoBytes for NonZeroIsize

source§

impl IntoBytes for NonZeroU8

source§

impl IntoBytes for NonZeroU16

source§

impl IntoBytes for NonZeroU32

source§

impl IntoBytes for NonZeroU64

source§

impl IntoBytes for NonZeroU128

source§

impl IntoBytes for NonZeroUsize

source§

impl<T: IntoBytes> IntoBytes for [T]

source§

impl<T: IntoBytes> IntoBytes for Wrapping<T>

source§

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

source§

impl<T: ?Sized + IntoBytes> IntoBytes for UnsafeCell<T>

source§

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

source§

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

Implementors§

source§

impl<O> IntoBytes for F32<O>

source§

impl<O> IntoBytes for F64<O>

source§

impl<O> IntoBytes for I16<O>

source§

impl<O> IntoBytes for I32<O>

source§

impl<O> IntoBytes for I64<O>

source§

impl<O> IntoBytes for I128<O>

source§

impl<O> IntoBytes for Isize<O>

source§

impl<O> IntoBytes for U16<O>

source§

impl<O> IntoBytes for U32<O>

source§

impl<O> IntoBytes for U64<O>

source§

impl<O> IntoBytes for U128<O>

source§

impl<O> IntoBytes for Usize<O>

source§

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