fidl_next

Trait IntoBytes

pub unsafe trait IntoBytes {
    // Provided methods
    fn as_bytes(&self) -> &[u8] 
       where Self: Immutable { ... }
    fn as_mut_bytes(&mut self) -> &mut [u8] 
       where Self: FromBytes { ... }
    fn write_to(
        &self,
        dst: &mut [u8],
    ) -> Result<(), SizeError<&Self, &mut [u8]>>
       where Self: Immutable { ... }
    fn write_to_prefix(
        &self,
        dst: &mut [u8],
    ) -> Result<(), SizeError<&Self, &mut [u8]>>
       where Self: Immutable { ... }
    fn write_to_suffix(
        &self,
        dst: &mut [u8],
    ) -> Result<(), SizeError<&Self, &mut [u8]>>
       where Self: Immutable { ... }
}
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)]; e.g.:

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

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

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§

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

Gets the bytes of this value.

§Examples
use zerocopy::IntoBytes;

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

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

Gets the bytes of this value mutably.

§Examples
use zerocopy::IntoBytes;

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

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],
});

fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to dst.

If dst.len() != size_of_val(self), write_to returns Err.

§Examples
use zerocopy::IntoBytes;

#[derive(IntoBytes, Immutable)]
#[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 Err 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_err());
assert_eq!(excessive_bytes, [0u8; 128]);

fn write_to_prefix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the prefix of dst.

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

§Examples
use zerocopy::IntoBytes;

#[derive(IntoBytes, Immutable)]
#[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 Err 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_err());
assert_eq!(insufficent_bytes, [0, 0]);

fn write_to_suffix( &self, dst: &mut [u8], ) -> Result<(), SizeError<&Self, &mut [u8]>>
where Self: Immutable,

Writes a copy of self to the suffix of dst.

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

§Examples
use zerocopy::IntoBytes;

#[derive(IntoBytes, Immutable)]
#[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_err());
assert_eq!(insufficent_bytes, [0, 0]);

If insufficient target bytes are provided, write_to_suffix returns Err 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_err());
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§

§

impl IntoBytes for Option<NonZero<i8>>

§

impl IntoBytes for Option<NonZero<i16>>

§

impl IntoBytes for Option<NonZero<i32>>

§

impl IntoBytes for Option<NonZero<i64>>

§

impl IntoBytes for Option<NonZero<i128>>

§

impl IntoBytes for Option<NonZero<isize>>

§

impl IntoBytes for Option<NonZero<u8>>

§

impl IntoBytes for Option<NonZero<u16>>

§

impl IntoBytes for Option<NonZero<u32>>

§

impl IntoBytes for Option<NonZero<u64>>

§

impl IntoBytes for Option<NonZero<u128>>

§

impl IntoBytes for Option<NonZero<usize>>

§

impl IntoBytes for bool

§

impl IntoBytes for char

§

impl IntoBytes for f32

§

impl IntoBytes for f64

§

impl IntoBytes for i8

§

impl IntoBytes for i16

§

impl IntoBytes for i32

§

impl IntoBytes for i64

§

impl IntoBytes for i128

§

impl IntoBytes for isize

§

impl IntoBytes for str

§

impl IntoBytes for u8

§

impl IntoBytes for u16

§

impl IntoBytes for u32

§

impl IntoBytes for u64

§

impl IntoBytes for u128

§

impl IntoBytes for ()

§

impl IntoBytes for usize

Source§

impl IntoBytes for Name
where [u8; 32]: IntoBytes,

Source§

impl IntoBytes for PadByte
where u8: IntoBytes,

Source§

impl IntoBytes for zx_exception_info_t
where u64: IntoBytes, u32: IntoBytes, [PadByte; 4]: IntoBytes, (): PaddingFree<zx_exception_info_t, zx_types::::{impl#239}::{constant#1}>,

Source§

impl IntoBytes for zx_info_maps_mapping_t
where u32: IntoBytes, [PadByte; 4]: IntoBytes, u64: IntoBytes, usize: IntoBytes, (): PaddingFree<zx_info_maps_mapping_t, zx_types::::{impl#565}::{constant#1}>,

Source§

impl IntoBytes for zx_info_vmo_t
where u64: IntoBytes, [u8; 32]: IntoBytes, usize: IntoBytes, u32: IntoBytes, [PadByte; 4]: IntoBytes, (): PaddingFree<zx_info_vmo_t, zx_types::::{impl#443}::{constant#1}>,

§

impl IntoBytes for __m128

§

impl IntoBytes for __m128d

§

impl IntoBytes for __m128i

§

impl IntoBytes for __m256

§

impl IntoBytes for __m256d

§

impl IntoBytes for __m256i

§

impl IntoBytes for NonZero<i8>

§

impl IntoBytes for NonZero<i16>

§

impl IntoBytes for NonZero<i32>

§

impl IntoBytes for NonZero<i64>

§

impl IntoBytes for NonZero<i128>

§

impl IntoBytes for NonZero<isize>

§

impl IntoBytes for NonZero<u8>

§

impl IntoBytes for NonZero<u16>

§

impl IntoBytes for NonZero<u32>

§

impl IntoBytes for NonZero<u64>

§

impl IntoBytes for NonZero<u128>

§

impl IntoBytes for NonZero<usize>

§

impl IntoBytes for AtomicBool

§

impl IntoBytes for AtomicI8

§

impl IntoBytes for AtomicI16

§

impl IntoBytes for AtomicI32

§

impl IntoBytes for AtomicI64

§

impl IntoBytes for AtomicIsize

§

impl IntoBytes for AtomicU8

§

impl IntoBytes for AtomicU16

§

impl IntoBytes for AtomicU32

§

impl IntoBytes for AtomicU64

§

impl IntoBytes for AtomicUsize

§

impl IntoBytes for AtomicI16_be
where AtomicI16: IntoBytes, (): PaddingFree<AtomicI16_be, rend::::{impl#4176}::{constant#0}>,

§

impl IntoBytes for AtomicI16_le
where AtomicI16: IntoBytes, (): PaddingFree<AtomicI16_le, rend::::{impl#4166}::{constant#0}>,

§

impl IntoBytes for AtomicI32_be
where AtomicI32: IntoBytes, (): PaddingFree<AtomicI32_be, rend::::{impl#4216}::{constant#0}>,

§

impl IntoBytes for AtomicI32_le
where AtomicI32: IntoBytes, (): PaddingFree<AtomicI32_le, rend::::{impl#4206}::{constant#0}>,

§

impl IntoBytes for AtomicI64_be
where AtomicI64: IntoBytes, (): PaddingFree<AtomicI64_be, rend::::{impl#4256}::{constant#0}>,

§

impl IntoBytes for AtomicI64_le
where AtomicI64: IntoBytes, (): PaddingFree<AtomicI64_le, rend::::{impl#4246}::{constant#0}>,

§

impl IntoBytes for AtomicU16_be
where AtomicU16: IntoBytes, (): PaddingFree<AtomicU16_be, rend::::{impl#4196}::{constant#0}>,

§

impl IntoBytes for AtomicU16_le
where AtomicU16: IntoBytes, (): PaddingFree<AtomicU16_le, rend::::{impl#4186}::{constant#0}>,

§

impl IntoBytes for AtomicU32_be
where AtomicU32: IntoBytes, (): PaddingFree<AtomicU32_be, rend::::{impl#4236}::{constant#0}>,

§

impl IntoBytes for AtomicU32_le
where AtomicU32: IntoBytes, (): PaddingFree<AtomicU32_le, rend::::{impl#4226}::{constant#0}>,

§

impl IntoBytes for AtomicU64_be
where AtomicU64: IntoBytes, (): PaddingFree<AtomicU64_be, rend::::{impl#4276}::{constant#0}>,

§

impl IntoBytes for AtomicU64_le
where AtomicU64: IntoBytes, (): PaddingFree<AtomicU64_le, rend::::{impl#4266}::{constant#0}>,

§

impl IntoBytes for NonZeroI16_be
where NonZero<i16>: IntoBytes, (): PaddingFree<NonZeroI16_be, rend::::{impl#3634}::{constant#0}>,

§

impl IntoBytes for NonZeroI16_le
where NonZero<i16>: IntoBytes, (): PaddingFree<NonZeroI16_le, rend::::{impl#3599}::{constant#0}>,

§

impl IntoBytes for NonZeroI32_be
where NonZero<i32>: IntoBytes, (): PaddingFree<NonZeroI32_be, rend::::{impl#3704}::{constant#0}>,

§

impl IntoBytes for NonZeroI32_le
where NonZero<i32>: IntoBytes, (): PaddingFree<NonZeroI32_le, rend::::{impl#3669}::{constant#0}>,

§

impl IntoBytes for NonZeroI64_be
where NonZero<i64>: IntoBytes, (): PaddingFree<NonZeroI64_be, rend::::{impl#3774}::{constant#0}>,

§

impl IntoBytes for NonZeroI64_le
where NonZero<i64>: IntoBytes, (): PaddingFree<NonZeroI64_le, rend::::{impl#3739}::{constant#0}>,

§

impl IntoBytes for NonZeroI128_be
where NonZero<i128>: IntoBytes, (): PaddingFree<NonZeroI128_be, rend::::{impl#3844}::{constant#0}>,

§

impl IntoBytes for NonZeroI128_le
where NonZero<i128>: IntoBytes, (): PaddingFree<NonZeroI128_le, rend::::{impl#3809}::{constant#0}>,

§

impl IntoBytes for NonZeroU16_be
where NonZero<u16>: IntoBytes, (): PaddingFree<NonZeroU16_be, rend::::{impl#3914}::{constant#0}>,

§

impl IntoBytes for NonZeroU16_le
where NonZero<u16>: IntoBytes, (): PaddingFree<NonZeroU16_le, rend::::{impl#3879}::{constant#0}>,

§

impl IntoBytes for NonZeroU32_be
where NonZero<u32>: IntoBytes, (): PaddingFree<NonZeroU32_be, rend::::{impl#3984}::{constant#0}>,

§

impl IntoBytes for NonZeroU32_le
where NonZero<u32>: IntoBytes, (): PaddingFree<NonZeroU32_le, rend::::{impl#3949}::{constant#0}>,

§

impl IntoBytes for NonZeroU64_be
where NonZero<u64>: IntoBytes, (): PaddingFree<NonZeroU64_be, rend::::{impl#4054}::{constant#0}>,

§

impl IntoBytes for NonZeroU64_le
where NonZero<u64>: IntoBytes, (): PaddingFree<NonZeroU64_le, rend::::{impl#4019}::{constant#0}>,

§

impl IntoBytes for NonZeroU128_be
where NonZero<u128>: IntoBytes, (): PaddingFree<NonZeroU128_be, rend::::{impl#4124}::{constant#0}>,

§

impl IntoBytes for NonZeroU128_le
where NonZero<u128>: IntoBytes, (): PaddingFree<NonZeroU128_le, rend::::{impl#4089}::{constant#0}>,

§

impl IntoBytes for char_be
where u32: IntoBytes, (): PaddingFree<char_be, rend::::{impl#3577}::{constant#0}>,

§

impl IntoBytes for char_le
where u32: IntoBytes, (): PaddingFree<char_le, rend::::{impl#3556}::{constant#0}>,

§

impl IntoBytes for char_ube
where u32: IntoBytes,

§

impl IntoBytes for char_ule
where u32: IntoBytes,

§

impl IntoBytes for f32_be
where f32: IntoBytes, (): PaddingFree<f32_be, rend::::{impl#3238}::{constant#0}>,

§

impl IntoBytes for f32_ube
where f32: IntoBytes,

§

impl IntoBytes for f32_ule
where f32: IntoBytes,

§

impl IntoBytes for f64_be
where f64: IntoBytes, (): PaddingFree<f64_be, rend::::{impl#3452}::{constant#0}>,

§

impl IntoBytes for f64_ube
where f64: IntoBytes,

§

impl IntoBytes for f64_ule
where f64: IntoBytes,

§

impl IntoBytes for i16_be
where i16: IntoBytes, (): PaddingFree<i16_be, rend::::{impl#199}::{constant#0}>,

§

impl IntoBytes for i16_ube
where i16: IntoBytes,

§

impl IntoBytes for i16_ule
where i16: IntoBytes,

§

impl IntoBytes for i32_be
where i32: IntoBytes, (): PaddingFree<i32_be, rend::::{impl#591}::{constant#0}>,

§

impl IntoBytes for i32_ube
where i32: IntoBytes,

§

impl IntoBytes for i32_ule
where i32: IntoBytes,

§

impl IntoBytes for i64_be
where i64: IntoBytes, (): PaddingFree<i64_be, rend::::{impl#983}::{constant#0}>,

§

impl IntoBytes for i64_ube
where i64: IntoBytes,

§

impl IntoBytes for i64_ule
where i64: IntoBytes,

§

impl IntoBytes for i128_be
where i128: IntoBytes, (): PaddingFree<i128_be, rend::::{impl#1375}::{constant#0}>,

§

impl IntoBytes for i128_le
where i128: IntoBytes, (): PaddingFree<i128_le, rend::::{impl#1179}::{constant#0}>,

§

impl IntoBytes for i128_ube
where i128: IntoBytes,

§

impl IntoBytes for i128_ule
where i128: IntoBytes,

§

impl IntoBytes for u16_be
where u16: IntoBytes, (): PaddingFree<u16_be, rend::::{impl#1766}::{constant#0}>,

§

impl IntoBytes for u16_ube
where u16: IntoBytes,

§

impl IntoBytes for u16_ule
where u16: IntoBytes,

§

impl IntoBytes for u32_be
where u32: IntoBytes, (): PaddingFree<u32_be, rend::::{impl#2156}::{constant#0}>,

§

impl IntoBytes for u32_ube
where u32: IntoBytes,

§

impl IntoBytes for u32_ule
where u32: IntoBytes,

§

impl IntoBytes for u64_be
where u64: IntoBytes, (): PaddingFree<u64_be, rend::::{impl#2546}::{constant#0}>,

§

impl IntoBytes for u64_ube
where u64: IntoBytes,

§

impl IntoBytes for u64_ule
where u64: IntoBytes,

§

impl IntoBytes for u128_be
where u128: IntoBytes, (): PaddingFree<u128_be, rend::::{impl#2936}::{constant#0}>,

§

impl IntoBytes for u128_le
where u128: IntoBytes, (): PaddingFree<u128_le, rend::::{impl#2741}::{constant#0}>,

§

impl IntoBytes for u128_ube
where u128: IntoBytes,

§

impl IntoBytes for u128_ule
where u128: IntoBytes,

§

impl<T> IntoBytes for [T]
where T: IntoBytes,

§

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

§

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

§

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

§

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

§

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

Implementors§

Source§

impl IntoBytes for WireMessageHeader
where u32_le: IntoBytes, [u8; 3]: IntoBytes, u8: IntoBytes, u64_le: IntoBytes, (): PaddingFree<WireMessageHeader, { _ }>,

§

impl IntoBytes for f32_le
where f32: IntoBytes, (): PaddingFree<f32_le, rend::::{impl#3131}::{constant#0}>,

§

impl IntoBytes for f64_le
where f64: IntoBytes, (): PaddingFree<f64_le, rend::::{impl#3345}::{constant#0}>,

§

impl IntoBytes for i16_le
where i16: IntoBytes, (): PaddingFree<i16_le, rend::::{impl#3}::{constant#0}>,

§

impl IntoBytes for i32_le
where i32: IntoBytes, (): PaddingFree<i32_le, rend::::{impl#395}::{constant#0}>,

§

impl IntoBytes for i64_le
where i64: IntoBytes, (): PaddingFree<i64_le, rend::::{impl#787}::{constant#0}>,

§

impl IntoBytes for u16_le
where u16: IntoBytes, (): PaddingFree<u16_le, rend::::{impl#1571}::{constant#0}>,

§

impl IntoBytes for u32_le
where u32: IntoBytes, (): PaddingFree<u32_le, rend::::{impl#1961}::{constant#0}>,

§

impl IntoBytes for u64_le
where u64: IntoBytes, (): PaddingFree<u64_le, rend::::{impl#2351}::{constant#0}>,

§

impl<O> IntoBytes for F32<O>

§

impl<O> IntoBytes for F64<O>

§

impl<O> IntoBytes for I16<O>

§

impl<O> IntoBytes for I32<O>

§

impl<O> IntoBytes for I64<O>

§

impl<O> IntoBytes for I128<O>

§

impl<O> IntoBytes for Isize<O>

§

impl<O> IntoBytes for U16<O>

§

impl<O> IntoBytes for U32<O>

§

impl<O> IntoBytes for U64<O>

§

impl<O> IntoBytes for U128<O>

§

impl<O> IntoBytes for Usize<O>

§

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