pub trait PrimitiveNumber:
'static
+ Sealed
+ PartialEq
+ PartialOrd
+ From<bool>
+ Default
+ Debug
+ Display
+ LowerExp
+ UpperExp
+ Product<Self>
+ Sum<Self>
+ Copy
+ Send
+ Sync
+ Unpin
+ Add<Self, Output = Self>
+ AddAssign<Self>
+ Div<Self, Output = Self>
+ DivAssign<Self>
+ Mul<Self, Output = Self>
+ MulAssign<Self>
+ Rem<Self, Output = Self>
+ RemAssign<Self>
+ Sub<Self, Output = Self>
+ SubAssign<Self>
+ RefUnwindSafe
+ UnwindSafe
+ FromStr<Err: PrimitiveError>
+ for<'a> Product<&'a Self>
+ for<'a> Sum<&'a Self>
+ for<'a> Add<&'a Self, Output = Self>
+ for<'a> AddAssign<&'a Self>
+ for<'a> Div<&'a Self, Output = Self>
+ for<'a> DivAssign<&'a Self>
+ for<'a> Mul<&'a Self, Output = Self>
+ for<'a> MulAssign<&'a Self>
+ for<'a> Rem<&'a Self, Output = Self>
+ for<'a> RemAssign<&'a Self>
+ for<'a> Sub<&'a Self, Output = Self>
+ for<'a> SubAssign<&'a Self> {
type Bytes: Borrow<[u8]> + BorrowMut<[u8]>;
// Required methods
fn from_be_bytes(bytes: Self::Bytes) -> Self;
fn from_le_bytes(bytes: Self::Bytes) -> Self;
fn from_ne_bytes(bytes: Self::Bytes) -> Self;
fn to_be_bytes(self) -> Self::Bytes;
fn to_le_bytes(self) -> Self::Bytes;
fn to_ne_bytes(self) -> Self::Bytes;
}Expand description
Trait for all primitive numeric types.
This encapsulates trait implementations and inherent methods that are common among all of the
primitive numeric types: f32, f64, i8, i16, i32, i64, i128,
isize, u8, u16, u32, u64, u128, and usize. Unstable types like
f16 and f128 will be added once they are stabilized.
See the corresponding items on the individual types for more documentation and examples.
This trait is sealed with a private trait to prevent downstream implementations, so we may continue to expand along with the standard library without worrying about breaking changes for implementors.
§Examples
use num_primitive::{PrimitiveNumber, PrimitiveNumberRef};
fn dot_product<T: PrimitiveNumber>(a: &[T], b: &[T]) -> T {
assert_eq!(a.len(), b.len());
// Note that we have to dereference to use `T: Mul<&T>`
core::iter::zip(a, b).map(|(a, b)| (*a) * b).sum()
}
fn dot_product_ref<T>(a: &[T], b: &[T]) -> T
where
T: PrimitiveNumber,
for<'a> &'a T: PrimitiveNumberRef<T>,
{
assert_eq!(a.len(), b.len());
// In this case we can use `&T: Mul`
core::iter::zip(a, b).map(|(a, b)| a * b).sum()
}
assert_eq!(dot_product::<i32>(&[1, 3, -5], &[4, -2, -1]), 3);
assert_eq!(dot_product_ref::<f64>(&[1., 3., -5.], &[4., -2., -1.]), 3.);Required Associated Types§
Sourcetype Bytes: Borrow<[u8]> + BorrowMut<[u8]>
type Bytes: Borrow<[u8]> + BorrowMut<[u8]>
An array of bytes used by methods like from_be_bytes and
to_be_bytes. It is effectively [u8; size_of::<Self>()].
Required Methods§
Sourcefn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
Creates a value from its representation as a byte array in big endian.
Sourcefn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
Creates a value from its representation as a byte array in little endian.
Sourcefn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
Creates a value from its representation as a byte array in native endian.
Sourcefn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
Returns the memory representation of this number as a byte array in little-endian order.
Sourcefn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
Returns the memory representation of this number as a byte array in big-endian order.
Sourcefn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
Returns the memory representation of this number as a byte array in native-endian order.
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§
Source§impl PrimitiveNumber for f32
impl PrimitiveNumber for f32
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 4]
Source§impl PrimitiveNumber for f64
impl PrimitiveNumber for f64
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 8]
Source§impl PrimitiveNumber for i8
impl PrimitiveNumber for i8
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 1]
Source§impl PrimitiveNumber for i16
impl PrimitiveNumber for i16
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 2]
Source§impl PrimitiveNumber for i32
impl PrimitiveNumber for i32
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 4]
Source§impl PrimitiveNumber for i64
impl PrimitiveNumber for i64
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 8]
Source§impl PrimitiveNumber for i128
impl PrimitiveNumber for i128
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 16]
Source§impl PrimitiveNumber for isize
impl PrimitiveNumber for isize
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 8]
Source§impl PrimitiveNumber for u8
impl PrimitiveNumber for u8
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 1]
Source§impl PrimitiveNumber for u16
impl PrimitiveNumber for u16
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 2]
Source§impl PrimitiveNumber for u32
impl PrimitiveNumber for u32
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 4]
Source§impl PrimitiveNumber for u64
impl PrimitiveNumber for u64
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 8]
Source§impl PrimitiveNumber for u128
impl PrimitiveNumber for u128
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.
type Bytes = [u8; 16]
Source§impl PrimitiveNumber for usize
impl PrimitiveNumber for usize
Source§fn from_be_bytes(bytes: Self::Bytes) -> Self
fn from_be_bytes(bytes: Self::Bytes) -> Self
See the inherent from_be_bytes method.
Source§fn from_le_bytes(bytes: Self::Bytes) -> Self
fn from_le_bytes(bytes: Self::Bytes) -> Self
See the inherent from_le_bytes method.
Source§fn from_ne_bytes(bytes: Self::Bytes) -> Self
fn from_ne_bytes(bytes: Self::Bytes) -> Self
See the inherent from_ne_bytes method.
Source§fn to_be_bytes(self) -> Self::Bytes
fn to_be_bytes(self) -> Self::Bytes
See the inherent to_be_bytes method.
Source§fn to_le_bytes(self) -> Self::Bytes
fn to_le_bytes(self) -> Self::Bytes
See the inherent to_le_bytes method.
Source§fn to_ne_bytes(self) -> Self::Bytes
fn to_ne_bytes(self) -> Self::Bytes
See the inherent to_ne_bytes method.