Trait IntoBytes
unsafe trait IntoBytes
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.:
# use IntoBytes;
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: &Self) -> &[u8] where Self: ImmutableGets the bytes of this value.
Examples
use IntoBytes; # use *; let header = PacketHeader ; let bytes = header.as_bytes; assert_eq!;fn as_mut_bytes(self: &mut Self) -> &mut [u8] where Self: FromBytesGets the bytes of this value mutably.
Examples
use IntoBytes; # use *; # let mut header = PacketHeader ; let bytes = header.as_mut_bytes; assert_eq!; bytes.reverse; assert_eq!;fn write_to(self: &Self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>> where Self: ImmutableWrites a copy of
selftodst.If
dst.len() != size_of_val(self),write_toreturnsErr.Examples
use IntoBytes; # use *; let header = PacketHeader ; let mut bytes = ; header.write_to; assert_eq!;If too many or too few target bytes are provided,
write_toreturnsErrand leaves the target bytes unmodified:# use IntoBytes; # let header = MAX; let mut excessive_bytes = &mut ; let write_result = header.write_to; assert!; assert_eq!;fn write_to_prefix(self: &Self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>> where Self: ImmutableWrites a copy of
selfto the prefix ofdst.write_to_prefixwritesselfto the firstsize_of_val(self)bytes ofdst. Ifdst.len() < size_of_val(self), it returnsErr.Examples
use IntoBytes; # use *; let header = PacketHeader ; let mut bytes = ; header.write_to_prefix; assert_eq!;If insufficient target bytes are provided,
write_to_prefixreturnsErrand leaves the target bytes unmodified:# use IntoBytes; # let header = MAX; let mut insufficient_bytes = &mut ; let write_result = header.write_to_suffix; assert!; assert_eq!;fn write_to_suffix(self: &Self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>> where Self: ImmutableWrites a copy of
selfto the suffix ofdst.write_to_suffixwritesselfto the lastsize_of_val(self)bytes ofdst. Ifdst.len() < size_of_val(self), it returnsErr.Examples
use IntoBytes; # use *; let header = PacketHeader ; let mut bytes = ; header.write_to_suffix; assert_eq!; let mut insufficient_bytes = &mut ; let write_result = header.write_to_suffix; assert!; assert_eq!;If insufficient target bytes are provided,
write_to_suffixreturnsErrand leaves the target bytes unmodified:# use IntoBytes; # let header = MAX; let mut insufficient_bytes = &mut ; let write_result = header.write_to_suffix; assert!; assert_eq!;
Implementors
impl IntoBytes for AtomicI16impl IntoBytes for u16impl IntoBytes for AtomicUsizeimpl<O> IntoBytes for F32<O>impl<O> IntoBytes for I64<O>impl IntoBytes for i64impl<T: ?Sized + IntoBytes> IntoBytes for Cell<T>impl IntoBytes for f32impl IntoBytes for __m128dimpl IntoBytes for NonZeroU8impl IntoBytes for __m512bhimpl IntoBytes for NonZeroI32impl IntoBytes for NonZeroUsizeimpl IntoBytes for Option<NonZeroI16>impl IntoBytes for Option<NonZeroU128>impl IntoBytes for AtomicI8impl IntoBytes for ()impl<O> IntoBytes for U16<O>impl IntoBytes for AtomicI32impl IntoBytes for u32impl<T: ?Sized> IntoBytes for PhantomData<T>impl IntoBytes for i128impl<O> IntoBytes for I16<O>impl<T: IntoBytes, N: usize> IntoBytes for [T; N]impl IntoBytes for boolimpl IntoBytes for __m256impl<T> IntoBytes for Unalign<T>impl IntoBytes for NonZeroU16impl<O> IntoBytes for I32<O>impl IntoBytes for __m512dimpl IntoBytes for NonZeroI64impl IntoBytes for Option<NonZeroU8>impl IntoBytes for Option<NonZeroI32>impl IntoBytes for Option<NonZeroUsize>impl<O> IntoBytes for Isize<O>impl IntoBytes for AtomicU16impl IntoBytes for i8impl<O> IntoBytes for Usize<O>impl IntoBytes for AtomicI64impl IntoBytes for u64impl<T: ?Sized + IntoBytes> IntoBytes for ManuallyDrop<T>impl IntoBytes for isizeimpl IntoBytes for __m128impl IntoBytes for strimpl IntoBytes for __m256iimpl<O> IntoBytes for U64<O>impl IntoBytes for NonZeroU32impl IntoBytes for NonZeroI128impl IntoBytes for Option<NonZeroU16>impl IntoBytes for Option<NonZeroI64>impl IntoBytes for AtomicU8impl IntoBytes for AtomicU32impl IntoBytes for i16impl IntoBytes for AtomicIsizeimpl IntoBytes for u128impl<T: ?Sized + IntoBytes> IntoBytes for UnsafeCell<T>impl IntoBytes for f64impl IntoBytes for __m128iimpl IntoBytes for NonZeroI8impl IntoBytes for __m512impl<O> IntoBytes for F64<O>impl IntoBytes for NonZeroU64impl IntoBytes for NonZeroIsizeimpl<O> IntoBytes for I128<O>impl IntoBytes for Option<NonZeroU32>impl IntoBytes for Option<NonZeroI128>impl IntoBytes for AtomicBoolimpl IntoBytes for u8impl IntoBytes for AtomicU64impl IntoBytes for i32impl<T: IntoBytes> IntoBytes for Wrapping<T>impl IntoBytes for usizeimpl<T: IntoBytes> IntoBytes for [T]impl IntoBytes for charimpl IntoBytes for __m256dimpl IntoBytes for NonZeroI16impl IntoBytes for __m512iimpl IntoBytes for NonZeroU128impl IntoBytes for Option<NonZeroI8>impl<O> IntoBytes for U32<O>impl IntoBytes for Option<NonZeroU64>impl<O> IntoBytes for U128<O>impl IntoBytes for Option<NonZeroIsize>