Trait Number

Source
pub trait Number:
    'static
    + Clone
    + Copy
    + Debug
    + Display
    + PartialEq
    + Eq
    + Hash
    + PartialOrd
    + Ord
    + Default
    + Private {
    type ByteRepr: AsRef<[u8]> + AsMut<[u8]> + Default;

    const NUMBER_TYPE: NumberType;
    const SIZE: usize;
    const ZERO: Self;
    const ONE: Self;
    const HIGHEST: Self;
Show 13 methods // Required methods fn wrapping_add(self, other: Self) -> Self; fn wrapping_sub(self, other: Self) -> Self; fn wrapping_mul(self, other: Self) -> Self; fn wrapping_div(self, other: Self) -> Self; fn wrapping_rem(self, other: Self) -> Self; fn wrapping_shl(self, other: u32) -> Self; fn wrapping_shr(self, other: u32) -> Self; fn as_u128(self) -> u128; fn from_u32(value: u32) -> Self; fn from_bytes(bytes: &[u8]) -> Self; fn to_bytes(self) -> Self::ByteRepr; unsafe fn from_u64_unchecked(value: u64) -> Self; unsafe fn from_u128_unchecked(value: u128) -> Self;
}
Expand description

Marker trait that abstracts the following numerical types: u32, u64 and u128.

§Notes

This trait depends on private trait, and thus extending it is not possible.

Required Associated Constants§

Source

const NUMBER_TYPE: NumberType

The type of the number.

Source

const SIZE: usize

The size of the number in bytes.

Source

const ZERO: Self

The number zero.

Source

const ONE: Self

The number one.

Source

const HIGHEST: Self

The highest possible value of the number.

Required Associated Types§

Source

type ByteRepr: AsRef<[u8]> + AsMut<[u8]> + Default

Represents the associated byte representation of the number, e.g. [u8; 4] for u32.

Required Methods§

Source

fn wrapping_add(self, other: Self) -> Self

Returns the sum of two numbers, wrapping around if the result is too large.

Source

fn wrapping_sub(self, other: Self) -> Self

Returns the difference of two numbers, wrapping around if the result is too small.

Source

fn wrapping_mul(self, other: Self) -> Self

Returns the product of two numbers, wrapping around if the result is too large.

Source

fn wrapping_div(self, other: Self) -> Self

Returns the quotient of two numbers, wrapping around if the result is too large.

Source

fn wrapping_rem(self, other: Self) -> Self

Returns the remainder of the division of two numbers.

Source

fn wrapping_shl(self, other: u32) -> Self

Returns the result of the left shift of the number by the given number of bits.

Source

fn wrapping_shr(self, other: u32) -> Self

Returns the result of the right shift of the number by the given number of bits.

Source

fn as_u128(self) -> u128

Returns the number as a u128, which is guaranteed to be big enough to hold any Number value.

Source

fn from_u32(value: u32) -> Self

Creates a number from a u32 value, which is guaranteed to be small enough to fit into the number.

Source

fn from_bytes(bytes: &[u8]) -> Self

Creates a number from its byte representation.

§Notes

This method is not portable, in particular it uses platform-specific endianness.

Source

fn to_bytes(self) -> Self::ByteRepr

Returns the byte representation of the number.

§Notes

This method is not portable, in particular it uses platform-specific endianness.

Source

unsafe fn from_u64_unchecked(value: u64) -> Self

Creates a number from a u32 value

§Safety

It does not check whether passed value is small enough to fit into Self.

Source

unsafe fn from_u128_unchecked(value: u128) -> Self

Creates a number from a u128 value

§Safety

It does not check whether passed value is small enough to fit into Self.

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 Number for u32

Source§

const NUMBER_TYPE: NumberType = NumberType::U32

Source§

const SIZE: usize = 4usize

Source§

const ZERO: Self = 0u32

Source§

const ONE: Self = 1u32

Source§

const HIGHEST: Self = 4_294_967_295u32

Source§

type ByteRepr = [u8; 4]

Source§

fn wrapping_add(self, other: Self) -> Self

Source§

fn wrapping_sub(self, other: Self) -> Self

Source§

fn wrapping_mul(self, other: Self) -> Self

Source§

fn wrapping_div(self, other: Self) -> Self

Source§

fn wrapping_rem(self, other: Self) -> Self

Source§

fn wrapping_shl(self, other: u32) -> Self

Source§

fn wrapping_shr(self, other: u32) -> Self

Source§

fn as_u128(self) -> u128

Source§

fn from_u32(value: u32) -> Self

Source§

fn from_bytes(bytes: &[u8]) -> Self

Source§

fn to_bytes(self) -> Self::ByteRepr

Source§

unsafe fn from_u64_unchecked(value: u64) -> Self

Source§

unsafe fn from_u128_unchecked(value: u128) -> Self

Source§

impl Number for u64

Source§

const NUMBER_TYPE: NumberType = NumberType::U64

Source§

const SIZE: usize = 8usize

Source§

const ZERO: Self = 0u64

Source§

const ONE: Self = 1u64

Source§

const HIGHEST: Self = 18_446_744_073_709_551_615u64

Source§

type ByteRepr = [u8; 8]

Source§

fn wrapping_add(self, other: Self) -> Self

Source§

fn wrapping_shl(self, other: u32) -> Self

Source§

fn wrapping_shr(self, other: u32) -> Self

Source§

fn wrapping_sub(self, other: Self) -> Self

Source§

fn wrapping_mul(self, other: Self) -> Self

Source§

fn wrapping_div(self, other: Self) -> Self

Source§

fn wrapping_rem(self, other: Self) -> Self

Source§

fn as_u128(self) -> u128

Source§

fn from_u32(value: u32) -> Self

Source§

fn from_bytes(bytes: &[u8]) -> Self

Source§

fn to_bytes(self) -> Self::ByteRepr

Source§

unsafe fn from_u64_unchecked(value: u64) -> Self

Source§

unsafe fn from_u128_unchecked(value: u128) -> Self

Source§

impl Number for u128

Source§

const NUMBER_TYPE: NumberType = NumberType::U128

Source§

const SIZE: usize = 16usize

Source§

const ZERO: Self = 0u128

Source§

const ONE: Self = 1u128

Source§

const HIGHEST: Self = 340_282_366_920_938_463_463_374_607_431_768_211_455u128

Source§

type ByteRepr = [u8; 16]

Source§

fn wrapping_add(self, other: Self) -> Self

Source§

fn wrapping_shl(self, other: u32) -> Self

Source§

fn wrapping_shr(self, other: u32) -> Self

Source§

fn wrapping_sub(self, other: Self) -> Self

Source§

fn wrapping_mul(self, other: Self) -> Self

Source§

fn wrapping_div(self, other: Self) -> Self

Source§

fn wrapping_rem(self, other: Self) -> Self

Source§

fn as_u128(self) -> u128

Source§

fn from_u32(value: u32) -> Self

Source§

fn from_bytes(bytes: &[u8]) -> Self

Source§

fn to_bytes(self) -> Self::ByteRepr

Source§

unsafe fn from_u64_unchecked(value: u64) -> Self

Source§

unsafe fn from_u128_unchecked(value: u128) -> Self

Implementors§