// Copyright 2019 The Fuchsia Authors // // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0 // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option. // This file may not be copied, modified, or distributed except according to // those terms.
//! Byte order-aware numeric primitives. //! //! This module contains equivalents of the native multi-byte integer types with //! no alignment requirement and supporting byte order conversions. //! //! For each native multi-byte integer type - `u16`, `i16`, `u32`, etc - and //! floating point type - `f32` and `f64` - an equivalent type is defined by //! this module - [`U16`], [`I16`], [`U32`], [`F32`], [`F64`], etc. Unlike their //! native counterparts, these types have alignment 1, and take a type parameter //! specifying the byte order in which the bytes are stored in memory. Each type //! implements this crate's relevant conversion and marker traits. //! //! These two properties, taken together, make these types useful for defining //! data structures whose memory layout matches a wire format such as that of a //! network protocol or a file format. Such formats often have multi-byte values //! at offsets that do not respect the alignment requirements of the equivalent //! native types, and stored in a byte order not necessarily the same as that of //! the target platform. //! //! Type aliases are provided for common byte orders in the [`big_endian`], //! [`little_endian`], [`network_endian`], and [`native_endian`] submodules. //! //! # Example //! //! One use of these types is for representing network packet formats, such as //! UDP: //! //! ```rust //! use zerocopy::{*, byteorder::network_endian::U16}; //! # use zerocopy_derive::*; //! //! #[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)] //! #[repr(C)] //! struct UdpHeader { //! src_port: U16, //! dst_port: U16, //! length: U16, //! checksum: U16, //! } //! //! #[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)] //! #[repr(C, packed)] //! struct UdpPacket { //! header: UdpHeader, //! body: [u8], //! } //! //! impl UdpPacket { //! fn parse(bytes: &[u8]) -> Option<&UdpPacket> { //! UdpPacket::ref_from_bytes(bytes).ok() //! } //! } //! ```
/// A type-level representation of byte order. /// /// This type is implemented by [`BigEndian`] and [`LittleEndian`], which /// represent big-endian and little-endian byte order respectively. This module /// also provides a number of useful aliases for those types: [`NativeEndian`], /// [`NetworkEndian`], [`BE`], and [`LE`]. /// /// `ByteOrder` types can be used to specify the byte order of the types in this /// module - for example, [`U32<BigEndian>`] is a 32-bit integer stored in /// big-endian byte order. /// /// [`U32<BigEndian>`]: U32 pubtrait ByteOrder:
Copy + Clone + Debug + Display + Eq + PartialEq + Ord + PartialOrd + Hash + private::Sealed
{ #[doc(hidden)] const ORDER: Order;
}
#[allow(missing_copy_implementations, missing_debug_implementations)] #[doc(hidden)] pubenum Order {
BigEndian,
LittleEndian,
}
/// Big-endian byte order. /// /// See [`ByteOrder`] for more details. #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] pubenum BigEndian {}
impl ByteOrder for BigEndian { const ORDER: Order = Order::BigEndian;
}
impl Display for BigEndian { #[inline] fn fmt(&self, _: &mut Formatter<'_>) -> fmt::Result { match *self {}
}
}
/// Little-endian byte order. /// /// See [`ByteOrder`] for more details. #[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] pubenum LittleEndian {}
impl ByteOrder for LittleEndian { const ORDER: Order = Order::LittleEndian;
}
impl Display for LittleEndian { #[inline] fn fmt(&self, _: &mut Formatter<'_>) -> fmt::Result { match *self {}
}
}
/// The endianness used by this platform. /// /// This is a type alias for [`BigEndian`] or [`LittleEndian`] depending on the /// endianness of the target platform. #[cfg(target_endian = "big")] pubtype NativeEndian = BigEndian;
/// The endianness used by this platform. /// /// This is a type alias for [`BigEndian`] or [`LittleEndian`] depending on the /// endianness of the target platform. #[cfg(target_endian = "little")] pubtype NativeEndian = LittleEndian;
/// The endianness used in many network protocols. /// /// This is a type alias for [`BigEndian`]. pubtype NetworkEndian = BigEndian;
/// A type alias for [`BigEndian`]. pubtype BE = BigEndian;
/// A type alias for [`LittleEndian`]. pubtype LE = LittleEndian;
impl<O: ByteOrder> core::ops::$trait_assign<$native> for $name<O> { #[inline(always)] fn $method_assign(&mutself, rhs: $native) {
*self = core::ops::$trait::$method(*self, rhs);
}
}
}; // Implement traits in terms of the same trait on the native type, but // without performing a byte order swap when both operands are byteorder // types. This only works for bitwise operations like `&`, `|`, etc. // // When only one operand is a byteorder type, we still need to perform a // byteorder swap.
(@without_byteorder_swap $name:ident, $native:ident, $trait:ident, $method:ident, $trait_assign:ident, $method_assign:ident) => { impl<O: ByteOrder> core::ops::$trait<$name<O>> for $name<O> { type Output = $name<O>;
#[inline(always)] fn $method(self, rhs: $name<O>) -> $name<O> { let self_native = $native::from_ne_bytes(self.0); let rhs_native = $native::from_ne_bytes(rhs.0); let result_native = core::ops::$trait::$method(self_native, rhs_native);
$name(result_native.to_ne_bytes(), PhantomData)
}
}
impl<O: ByteOrder> core::ops::$trait<$name<O>> for $native { type Output = $name<O>;
#[inline(always)] fn $method(self, rhs: $name<O>) -> $name<O> { // No runtime cost - just byte packing let rhs_native = $native::from_ne_bytes(rhs.0); // (Maybe) runtime cost - byte order swap let slf_byteorder = $name::<O>::new(self); // No runtime cost - just byte packing let slf_native = $native::from_ne_bytes(slf_byteorder.0); // Runtime cost - perform the operation let result_native = core::ops::$trait::$method(slf_native, rhs_native); // No runtime cost - just byte unpacking
$name(result_native.to_ne_bytes(), PhantomData)
}
}
impl<O: ByteOrder> core::ops::$trait<$native> for $name<O> { type Output = $name<O>;
#[inline(always)] fn $method(self, rhs: $native) -> $name<O> { // (Maybe) runtime cost - byte order swap let rhs_byteorder = $name::<O>::new(rhs); // No runtime cost - just byte packing let rhs_native = $native::from_ne_bytes(rhs_byteorder.0); // No runtime cost - just byte packing let slf_native = $native::from_ne_bytes(self.0); // Runtime cost - perform the operation let result_native = core::ops::$trait::$method(slf_native, rhs_native); // No runtime cost - just byte unpacking
$name(result_native.to_ne_bytes(), PhantomData)
}
}
macro_rules! define_max_value_constant {
($name:ident, $bytes:expr, "unsigned integer") => { /// The maximum value. /// /// This constant should be preferred to constructing a new value using /// `new`, as `new` may perform an endianness swap depending on the /// endianness `O` and the endianness of the platform. pubconst MAX_VALUE: $name<O> = $name([0xFFu8; $bytes], PhantomData);
}; // We don't provide maximum and minimum value constants for signed values // and floats because there's no way to do it generically - it would require // a different value depending on the value of the `ByteOrder` type // parameter. Currently, one workaround would be to provide implementations // for concrete implementations of that trait. In the long term, if we are // ever able to make the `new` constructor a const fn, we could use that // instead.
($name:ident, $bytes:expr, "signed integer") => {};
($name:ident, $bytes:expr, "floating point number") => {};
}
`", stringify!($name), "` is like the native `", stringify!($native), "` type with
two major differences: First, it has no alignment requirement (its alignment is 1).
Second, the endianness of its memory layout is given by the type parameter `O`,
which can be any type which implements [`ByteOrder`]. In particular, this refers
to [`BigEndian`], [`LittleEndian`], [`NativeEndian`], and [`NetworkEndian`].
", stringify!($article), " `", stringify!($name), "` can be constructed using
the [`new`] method, and its contained value can be obtained as a native
`",stringify!($native), "` using the [`get`] method, or updated in place with
the [`set`] method. In all cases, if the endianness `O` is not the same as the
endianness of the current platform, an endianness swap will be performed in
order to uphold the invariants that a) the layout of `", stringify!($name), "`
has endianness `O` and that, b) the layout of `", stringify!($native), "` has
the platform's native endianness.
`", stringify!($name), "` implements [`FromBytes`], [`IntoBytes`], and [`Unaligned`],
making it useful for parsing and serialization. See the module documentation for an
example of how it can be used for parsing UDP packets.
#[allow(unused_unsafe)] // Unused when `feature = "derive"`. // SAFETY: `$name<O>` is `repr(transparent)`, and so it has the same // layout as its only non-zero field, which is a `u8` array. `u8` arrays // are `Immutable`, `TryFromBytes`, `FromZeros`, `FromBytes`, // `IntoBytes`, and `Unaligned`. const _: () = unsafe {
impl_or_verify!(O => Immutable for $name<O>);
impl_or_verify!(O => TryFromBytes for $name<O>);
impl_or_verify!(O => FromZeros for $name<O>);
impl_or_verify!(O => FromBytes for $name<O>);
impl_or_verify!(O => IntoBytes for $name<O>);
impl_or_verify!(O => Unaligned for $name<O>);
};
impl<O> $name<O> { /// The value zero. /// /// This constant should be preferred to constructing a new value /// using `new`, as `new` may perform an endianness swap depending /// on the endianness and platform. pubconst ZERO: $name<O> = $name([0u8; $bytes], PhantomData);
/// Constructs a new value from bytes which are already in `O` byte /// order. #[must_use = "has no side effects"] #[inline(always)] pubconstfn from_bytes(bytes: [u8; $bytes]) -> $name<O> {
$name(bytes, PhantomData)
}
/// Extracts the bytes of `self` without swapping the byte order. /// /// The returned bytes will be in `O` byte order. #[must_use = "has no side effects"] #[inline(always)] pubconstfn to_bytes(self) -> [u8; $bytes] { self.0
}
}
impl<O: ByteOrder> $name<O> {
maybe_const_trait_bounded_fn! { /// Constructs a new value, possibly performing an endianness /// swap to guarantee that the returned value has endianness /// `O`. #[must_use = "has no side effects"] #[inline(always)] pubconstfn new(n: $native) -> $name<O> { let bytes = match O::ORDER {
Order::BigEndian => $to_be_fn(n),
Order::LittleEndian => $to_le_fn(n),
};
$name(bytes, PhantomData)
}
}
maybe_const_trait_bounded_fn! { /// Returns the value as a primitive type, possibly performing /// an endianness swap to guarantee that the return value has /// the endianness of the native platform. #[must_use = "has no side effects"] #[inline(always)] pubconstfn get(self) -> $native { match O::ORDER {
Order::BigEndian => $from_be_fn(self.0),
Order::LittleEndian => $from_le_fn(self.0),
}
}
}
/// Updates the value in place as a primitive type, possibly /// performing an endianness swap to guarantee that the stored value /// has the endianness `O`. #[inline(always)] pubfn set(&mutself, n: $native) {
*self = Self::new(n);
}
}
// The reasoning behind which traits to implement here is to only // implement traits which won't cause inference issues. Notably, // comparison traits like PartialEq and PartialOrd tend to cause // inference issues.
impl<O: ByteOrder> Debug for $name<O> { #[inline] fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { // This results in a format like "U16(42)".
f.debug_tuple(stringify!($name)).field(&self.get()).finish()
}
}
};
}
define_type!(
A, "A 16-bit unsigned integer",
U16,
u16, 16, 2,
u16::from_be_bytes,
u16::to_be_bytes,
u16::from_le_bytes,
u16::to_le_bytes, "unsigned integer",
[u32, u64, u128, usize],
[u32, u64, u128, usize],
[U32, U64, U128, Usize],
[U32, U64, U128, Usize]
);
define_type!(
A, "A 32-bit unsigned integer",
U32,
u32, 32, 4,
u32::from_be_bytes,
u32::to_be_bytes,
u32::from_le_bytes,
u32::to_le_bytes, "unsigned integer",
[u64, u128],
[u64, u128],
[U64, U128],
[U64, U128]
);
define_type!(
A, "A 64-bit unsigned integer",
U64,
u64, 64, 8,
u64::from_be_bytes,
u64::to_be_bytes,
u64::from_le_bytes,
u64::to_le_bytes, "unsigned integer",
[u128],
[u128],
[U128],
[U128]
);
define_type!(
A, "A 128-bit unsigned integer",
U128,
u128, 128, 16,
u128::from_be_bytes,
u128::to_be_bytes,
u128::from_le_bytes,
u128::to_le_bytes, "unsigned integer",
[],
[],
[],
[]
);
define_type!(
A, "A word-sized unsigned integer",
Usize,
usize,
mem::size_of::<usize>() * 8,
mem::size_of::<usize>(),
usize::from_be_bytes,
usize::to_be_bytes,
usize::from_le_bytes,
usize::to_le_bytes, "unsigned integer",
[],
[],
[],
[]
);
define_type!(
An, "A 16-bit signed integer",
I16,
i16, 16, 2,
i16::from_be_bytes,
i16::to_be_bytes,
i16::from_le_bytes,
i16::to_le_bytes, "signed integer",
[i32, i64, i128, isize],
[i32, i64, i128, isize],
[I32, I64, I128, Isize],
[I32, I64, I128, Isize]
);
define_type!(
An, "A 32-bit signed integer",
I32,
i32, 32, 4,
i32::from_be_bytes,
i32::to_be_bytes,
i32::from_le_bytes,
i32::to_le_bytes, "signed integer",
[i64, i128],
[i64, i128],
[I64, I128],
[I64, I128]
);
define_type!(
An, "A 64-bit signed integer",
I64,
i64, 64, 8,
i64::from_be_bytes,
i64::to_be_bytes,
i64::from_le_bytes,
i64::to_le_bytes, "signed integer",
[i128],
[i128],
[I128],
[I128]
);
define_type!(
An, "A 128-bit signed integer",
I128,
i128, 128, 16,
i128::from_be_bytes,
i128::to_be_bytes,
i128::from_le_bytes,
i128::to_le_bytes, "signed integer",
[],
[],
[],
[]
);
define_type!(
An, "A word-sized signed integer",
Isize,
isize,
mem::size_of::<isize>() * 8,
mem::size_of::<isize>(),
isize::from_be_bytes,
isize::to_be_bytes,
isize::from_le_bytes,
isize::to_le_bytes, "signed integer",
[],
[],
[],
[]
);
// FIXME(https://github.com/rust-lang/rust/issues/72447): Use the endianness // conversion methods directly once those are const-stable.
macro_rules! define_float_conversion {
($ty:ty, $bits:ident, $bytes:expr, $mod:ident) => { mod $mod { usesuper::*;
define_float_conversion!($ty, $bits, $bytes, from_be_bytes, to_be_bytes);
define_float_conversion!($ty, $bits, $bytes, from_le_bytes, to_le_bytes);
}
};
($ty:ty, $bits:ident, $bytes:expr, $from:ident, $to:ident) => { // Clippy: The suggestion of using `from_bits()` instead doesn't work // because `from_bits` is not const-stable on our MSRV. #[allow(clippy::unnecessary_transmutes)] pub(crate) constfn $from(bytes: [u8; $bytes]) -> $ty {
transmute!($bits::$from(bytes))
}
pub(crate) constfn $to(f: $ty) -> [u8; $bytes] { // Clippy: The suggestion of using `f.to_bits()` instead doesn't // work because `to_bits` is not const-stable on our MSRV. #[allow(clippy::unnecessary_transmutes)] let bits: $bits = transmute!(f);
bits.$to()
}
};
}
/// For `f32` and `f64`, NaN values are not considered equal to /// themselves. This method is like `assert_eq!`, but it treats NaN /// values as equal. fn assert_eq_or_nan(self, other: Self) { let slf = (!self.is_nan()).then(|| self); let other = (!other.is_nan()).then(|| other);
assert_eq!(slf, other);
}
}
trait ByteArray:
FromBytes + IntoBytes + Immutable + Copy + AsRef<[u8]> + AsMut<[u8]> + Debug + Default + Eq
{ /// Invert the order of the bytes in the array. fn invert(self) -> Self;
}
/// For `f32` and `f64`, NaN values are not considered equal to /// themselves. This method is like `assert_eq!`, but it treats NaN /// values as equal. fn assert_eq_or_nan(self, other: Self) { let slf = (!self.get().is_nan()).then(|| self); let other = (!other.get().is_nan()).then(|| other);
assert_eq!(slf, other);
}
}
macro_rules! impl_traits {
($name:ident, $native:ident, $sign:ident $(, @$float:ident)?) => { impl Native for $native { // For some types, `0 as $native` is required (for example, when // `$native` is a floating-point type; `0` is an integer), but // for other types, it's a trivial cast. In all cases, Clippy // thinks it's dangerous. #[allow(trivial_numeric_casts, clippy::as_conversions)] const ZERO: $native = 0as $native; const MAX_VALUE: $native = $native::MAX;
type Distribution = Standard; const DIST: Standard = Standard;
#[cfg(target_endian = "big")] type NonNativeEndian = LittleEndian; #[cfg(target_endian = "little")] type NonNativeEndian = BigEndian;
// We use a `u64` seed so that we can use `SeedableRng::seed_from_u64`. // `SmallRng`'s `SeedableRng::Seed` differs by platform, so if we wanted to // call `SeedableRng::from_seed`, which takes a `Seed`, we would need // conditional compilation by `target_pointer_width`. const RNG_SEED: u64 = 0x7A03CAE2F32B5B8F;
const RAND_ITERS: usize = if cfg!(any(miri, kani)) { // The tests below which use this constant used to take a very long time // on Miri, which slows down local development and CI jobs. We're not // using Miri to check for the correctness of our code, but rather its // soundness, and at least in the context of these particular tests, a // single loop iteration is just as good for surfacing UB as multiple // iterations are. // // As of the writing of this comment, here's one set of measurements: // // $ # RAND_ITERS == 1 // $ cargo miri test -- -Z unstable-options --report-time endian // test byteorder::tests::test_native_endian ... ok <0.049s> // test byteorder::tests::test_non_native_endian ... ok <0.061s> // // $ # RAND_ITERS == 1024 // $ cargo miri test -- -Z unstable-options --report-time endian // test byteorder::tests::test_native_endian ... ok <25.716s> // test byteorder::tests::test_non_native_endian ... ok <38.127s> 1
} else { 1024
};
#[test] fn test_const_methods() { use big_endian::*;
#[test] fn test_ops_impls() { // Test implementations of traits in `core::ops`. Some of these are // fairly banal, but some are optimized to perform the operation without // swapping byte order (namely, bit-wise operations which are identical // regardless of byte order). These are important to test, and while // we're testing those anyway, it's trivial to test all of the impls.
FATT: Fn(&mut T, T),
FATN: Fn(&mut T, T::Native),
FANT: Fn(&mut T::Native, T),
{ letmut r = SmallRng::seed_from_u64(RNG_SEED); for _ in0..RAND_ITERS { let n0 = T::Native::rand(&mut r); let n1 = T::Native::rand(&mut r); let t0 = T::new(n0); let t1 = T::new(n1);
// If this operation would overflow/underflow, skip it rather // than attempt to catch and recover from panics. if matches!(&op_n_n_checked, Some(checked) if checked(n0, n1).is_none()) { continue;
}
let t_t_res = op_t_t(t0, t1); let t_n_res = op_t_n(t0, n1); let n_t_res = op_n_t(n0, t1); let n_n_res = op_n_n(n0, n1);
// For `f32` and `f64`, NaN values are not considered equal to // themselves. We store `Option<f32>`/`Option<f64>` and store // NaN as `None` so they can still be compared. let val_or_none = |t: T| (!T::Native::is_nan(t.get())).then(|| t.get()); let t_t_res = val_or_none(t_t_res); let t_n_res = val_or_none(t_n_res); let n_t_res = val_or_none(n_t_res); let n_n_res = (!T::Native::is_nan(n_n_res)).then(|| n_n_res);
assert_eq!(t_t_res, n_n_res);
assert_eq!(t_n_res, n_n_res);
assert_eq!(n_t_res, n_n_res);
// For `f32` and `f64`, NaN values are not considered equal to // themselves. We store `Option<f32>`/`Option<f64>` and store // NaN as `None` so they can still be compared. let t_t_res = val_or_none(t_t_res); let t_n_res = val_or_none(t_n_res); let n_t_res = (!T::Native::is_nan(n_t_res)).then(|| n_t_res);
assert_eq!(t_t_res, n_n_res);
assert_eq!(t_n_res, n_n_res);
assert_eq!(n_t_res, n_n_res);
}
}
}
#[test] fn test_debug_impl() { // Ensure that Debug applies format options to the inner value. let val = U16::<LE>::new(10);
assert_eq!(format!("{:?}", val), "U16(10)");
assert_eq!(format!("{:03?}", val), "U16(010)");
assert_eq!(format!("{:x?}", val), "U16(a)");
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.