Lines Matching +full:wire +full:- +full:sized
1 // SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
5 // Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
6 // <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
7 // license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
11 //! Byte order-aware numeric primitives.
13 //! This module contains equivalents of the native multi-byte integer types with
16 //! For each native multi-byte integer type - `u16`, `i16`, `u32`, etc - and
17 //! floating point type - `f32` and `f64` - an equivalent type is defined by
18 //! this module - [`U16`], [`I16`], [`U32`], [`F32`], [`F64`], etc. Unlike their
24 //! data structures whose memory layout matches a wire format such as that of a
25 //! network protocol or a file format. Such formats often have multi-byte values
32 //! Note that network-endian is a synonym for big-endian.
60 //! fn parse(bytes: &[u8]) -> Option<&UdpPacket> {
75 /// A type-level representation of byte order.
78 /// represent big-endian and little-endian byte order respectively. This module
83 /// module - for example, [`U32<BigEndian>`] is a 32-bit integer stored in
84 /// big-endian byte order.
109 /// Big-endian byte order.
121 fn fmt(&self, _: &mut Formatter<'_>) -> fmt::Result { in fmt()
126 /// Little-endian byte order.
138 fn fmt(&self, _: &mut Formatter<'_>) -> fmt::Result { in fmt()
172 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
188 fn fmt(&self, _f: &mut Formatter<'_>) -> fmt::Result {
208 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
242 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
267 fn not(self) -> $name<O> {
275 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
282 fn cmp(&self, other: &Self) -> Ordering {
289 fn partial_cmp(&self, other: &$native) -> Option<Ordering> {
299 fn neg(self) -> $name<O> {
302 $name::<O>::new(-self_native)
318 fn $method(self, rhs: $name<O>) -> $name<O> {
330 fn $method(self, rhs: $name<O>) -> $name<O> {
341 fn $method(self, rhs: $native) -> $name<O> {
381 fn $method(self, rhs: $name<O>) -> $name<O> {
393 fn $method(self, rhs: $name<O>) -> $name<O> {
394 // No runtime cost - just byte packing
396 // (Maybe) runtime cost - byte order swap
398 // No runtime cost - just byte packing
400 // Runtime cost - perform the operation
402 // No runtime cost - just byte unpacking
411 fn $method(self, rhs: $native) -> $name<O> {
412 // (Maybe) runtime cost - byte order swap
414 // No runtime cost - just byte packing
416 // No runtime cost - just byte packing
418 // Runtime cost - perform the operation
420 // No runtime cost - just byte unpacking
435 // (Maybe) runtime cost - byte order swap
437 // Runtime cost - perform the operation
468 // and floats because there's no way to do it generically - it would require
535 // layout as its only non-zero field, which is a `u8` array. `u8` arrays
550 fn default() -> $name<O> {
569 pub const fn from_bytes(bytes: [u8; $bytes]) -> $name<O> {
578 pub const fn to_bytes(self) -> [u8; $bytes] {
590 pub const fn new(n: $native) -> $name<O> {
606 pub const fn get(self) -> $native {
630 fn from(x: $name<O>) -> [u8; $bytes] {
637 fn from(bytes: [u8; $bytes]) -> $name<O> {
644 fn from(x: $name<O>) -> $native {
651 fn from(x: $native) -> $name<O> {
659 fn from(x: $name<O>) -> $larger_native {
669 fn try_from(x: $larger_native_try) -> Result<$name<O>, TryFromIntError> {
678 fn from(x: $name<O>) -> $larger_byteorder<P> {
688 fn try_from(x: $larger_byteorder_try<P>) -> Result<$name<O>, TryFromIntError> {
696 fn as_ref(&self) -> &[u8; $bytes] {
703 fn as_mut(&mut self) -> &mut [u8; $bytes] {
710 fn eq(&self, other: &$name<O>) -> bool {
717 fn eq(&self, other: &[u8; $bytes]) -> bool {
724 fn eq(&self, other: &$native) -> bool {
737 "A 16-bit unsigned integer",
754 "A 32-bit unsigned integer",
771 "A 64-bit unsigned integer",
788 "A 128-bit unsigned integer",
805 "A word-sized unsigned integer",
822 "A 16-bit signed integer",
839 "A 32-bit signed integer",
856 "A 64-bit signed integer",
873 "A 128-bit signed integer",
890 "A word-sized signed integer",
906 // FIXME(https://github.com/rust-lang/rust/issues/72447): Use the endianness
907 // conversion methods directly once those are const-stable.
919 // because `from_bits` is not const-stable on our MSRV.
921 pub(crate) const fn $from(bytes: [u8; $bytes]) -> $ty {
925 pub(crate) const fn $to(f: $ty) -> [u8; $bytes] {
927 // work because `to_bits` is not const-stable on our MSRV.
940 "A 32-bit floating point number",
957 "A 64-bit floating point number",
981 module!(@ty U16, $trait, "16-bit unsigned integer", $endianness_str);
982 module!(@ty U32, $trait, "32-bit unsigned integer", $endianness_str);
983 module!(@ty U64, $trait, "64-bit unsigned integer", $endianness_str);
984 module!(@ty U128, $trait, "128-bit unsigned integer", $endianness_str);
985 module!(@ty I16, $trait, "16-bit signed integer", $endianness_str);
986 module!(@ty I32, $trait, "32-bit signed integer", $endianness_str);
987 module!(@ty I64, $trait, "64-bit signed integer", $endianness_str);
988 module!(@ty I128, $trait, "128-bit signed integer", $endianness_str);
989 module!(@ty F32, $trait, "32-bit floating point number", $endianness_str);
990 module!(@ty F64, $trait, "64-bit floating point number", $endianness_str);
1003 module!(big_endian, BigEndian, "big-endian");
1004 module!(little_endian, LittleEndian, "little-endian");
1005 module!(network_endian, NetworkEndian, "network-endian");
1006 module!(native_endian, NativeEndian, "native-endian");
1032 pub(crate) fn seed_from_u64(_state: u64) -> Self { in seed_from_u64()
1038 fn sample<T, D: Distribution<T>>(&mut self, _distr: D) -> T in sample()
1064 fn rand<R: Rng>(rng: &mut R) -> Self { in rand()
1069 fn checked_add(self, rhs: Self) -> Option<Self>; in checked_add()
1072 fn checked_div(self, rhs: Self) -> Option<Self>; in checked_div()
1075 fn checked_mul(self, rhs: Self) -> Option<Self>; in checked_mul()
1078 fn checked_rem(self, rhs: Self) -> Option<Self>; in checked_rem()
1081 fn checked_sub(self, rhs: Self) -> Option<Self>; in checked_sub()
1084 fn checked_shl(self, rhs: Self) -> Option<Self>; in checked_shl()
1087 fn checked_shr(self, rhs: Self) -> Option<Self>; in checked_shr()
1089 fn is_nan(self) -> bool; in is_nan()
1105 fn invert(self) -> Self; in invert()
1116 fn new(native: Self::Native) -> Self; in new()
1117 fn get(self) -> Self::Native; in get()
1119 fn from_bytes(bytes: Self::ByteArray) -> Self; in from_bytes()
1120 fn into_bytes(self) -> Self::ByteArray; in into_bytes()
1139 fn invert(mut self) -> [u8; $bytes] {
1165 // `$native` is a floating-point type; `0` is an integer), but
1184 fn new(native: $native) -> $name<O> {
1188 fn get(self) -> $native {
1196 fn from_bytes(bytes: [u8; mem::size_of::<$native>()]) -> $name<O> {
1200 fn into_bytes(self) -> [u8; mem::size_of::<$native>()] {
1208 fn checked_add(self, rhs: Self) -> Option<Self> { self.checked_add(rhs) }
1209 fn checked_div(self, rhs: Self) -> Option<Self> { self.checked_div(rhs) }
1210 fn checked_mul(self, rhs: Self) -> Option<Self> { self.checked_mul(rhs) }
1211 fn checked_rem(self, rhs: Self) -> Option<Self> { self.checked_rem(rhs) }
1212 fn checked_sub(self, rhs: Self) -> Option<Self> { self.checked_sub(rhs) }
1213 …fn checked_shl(self, rhs: Self) -> Option<Self> { self.checked_shl(rhs.try_into().unwrap_or(u32::M…
1214 …fn checked_shr(self, rhs: Self) -> Option<Self> { self.checked_shr(rhs.try_into().unwrap_or(u32::M…
1215 fn is_nan(self) -> bool { false }
1218 fn checked_add(self, rhs: Self) -> Option<Self> { Some(self + rhs) }
1219 fn checked_div(self, rhs: Self) -> Option<Self> { Some(self / rhs) }
1220 fn checked_mul(self, rhs: Self) -> Option<Self> { Some(self * rhs) }
1221 fn checked_rem(self, rhs: Self) -> Option<Self> { Some(self % rhs) }
1222 fn checked_sub(self, rhs: Self) -> Option<Self> { Some(self - rhs) }
1223 fn checked_shl(self, _rhs: Self) -> Option<Self> { unimplemented!() }
1224 fn checked_shr(self, _rhs: Self) -> Option<Self> { unimplemented!() }
1225 fn is_nan(self) -> bool { self.is_nan() }
1299 // $ cargo miri test -- -Z unstable-options --report-time endian
1304 // $ cargo miri test -- -Z unstable-options --report-time endian
1390 // swapping byte order (namely, bit-wise operations which are identical in test_ops_impls()
1403 FTT: Fn(T, T) -> T, in test_ops_impls()
1404 FTN: Fn(T, T::Native) -> T, in test_ops_impls()
1405 FNT: Fn(T::Native, T) -> T, in test_ops_impls()
1406 FNN: Fn(T::Native, T::Native) -> T::Native, in test_ops_impls()
1407 FNNChecked: Fn(T::Native, T::Native) -> Option<T::Native>, in test_ops_impls()
1492 … let mut op_native_checked = None::<fn(T::Native, T::Native) -> Option<T::Native>>; in test_ops_impls()
1528 None::<fn(T::Native, T::Native) -> Option<T::Native>>, in test_ops_impls()