Trait Number

Source
pub trait Number:
    Sized
    + Copy
    + Clone
    + PartialOrd
    + Ord
    + PartialEq
    + Eq {
    type UnderlyingType: Number + Debug + From<u8> + TryFrom<u16> + TryFrom<u32> + TryFrom<u64> + TryFrom<u128>;

    const BITS: usize;
    const MIN: Self;
    const MAX: Self;

    // Required methods
    fn new(value: Self::UnderlyingType) -> Self;
    fn try_new(value: Self::UnderlyingType) -> Result<Self, TryNewError>;
    fn value(self) -> Self::UnderlyingType;
    fn from_<T: Number>(value: T) -> Self;
    fn masked_new<T: Number>(value: T) -> Self;
    fn as_u8(&self) -> u8;
    fn as_u16(&self) -> u16;
    fn as_u32(&self) -> u32;
    fn as_u64(&self) -> u64;
    fn as_u128(&self) -> u128;
    fn as_usize(&self) -> usize;

    // Provided method
    fn as_<T: Number>(self) -> T { ... }
}

Required Associated Constants§

Source

const BITS: usize

Number of bits that can fit in this type

Source

const MIN: Self

Minimum value that can be represented by this type

Source

const MAX: Self

Maximum value that can be represented by this type

Required Associated Types§

Required Methods§

Source

fn new(value: Self::UnderlyingType) -> Self

Creates a number from the given value, throwing an error if the value is too large. This constructor is useful when creating a value from a literal.

Source

fn try_new(value: Self::UnderlyingType) -> Result<Self, TryNewError>

Creates a number from the given value, return None if the value is too large

Source

fn value(self) -> Self::UnderlyingType

Source

fn from_<T: Number>(value: T) -> Self

Creates a number from the given value, throwing an error if the value is too large. This constructor is useful when the value is convertable to T. Use Self::new for literals.

Source

fn masked_new<T: Number>(value: T) -> Self

Creates an instance from the given value. Unlike the various new... functions, this will never fail as the value is masked to the result size.

Source

fn as_u8(&self) -> u8

Source

fn as_u16(&self) -> u16

Source

fn as_u32(&self) -> u32

Source

fn as_u64(&self) -> u64

Source

fn as_u128(&self) -> u128

Source

fn as_usize(&self) -> usize

Provided Methods§

Source

fn as_<T: Number>(self) -> T

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 u8

Source§

const BITS: usize = 8usize

Source§

const MIN: Self = 0u8

Source§

const MAX: Self = 255u8

Source§

type UnderlyingType = u8

Source§

fn new(value: Self::UnderlyingType) -> Self

Source§

fn try_new(value: Self::UnderlyingType) -> Result<Self, TryNewError>

Source§

fn value(self) -> Self::UnderlyingType

Source§

fn from_<T: Number>(value: T) -> Self

Source§

fn masked_new<T: Number>(value: T) -> Self

Source§

fn as_u8(&self) -> u8

Source§

fn as_u16(&self) -> u16

Source§

fn as_u32(&self) -> u32

Source§

fn as_u64(&self) -> u64

Source§

fn as_u128(&self) -> u128

Source§

fn as_usize(&self) -> usize

Source§

impl Number for u16

Source§

const BITS: usize = 16usize

Source§

const MIN: Self = 0u16

Source§

const MAX: Self = 65_535u16

Source§

type UnderlyingType = u16

Source§

fn new(value: Self::UnderlyingType) -> Self

Source§

fn try_new(value: Self::UnderlyingType) -> Result<Self, TryNewError>

Source§

fn value(self) -> Self::UnderlyingType

Source§

fn from_<T: Number>(value: T) -> Self

Source§

fn masked_new<T: Number>(value: T) -> Self

Source§

fn as_u8(&self) -> u8

Source§

fn as_u16(&self) -> u16

Source§

fn as_u32(&self) -> u32

Source§

fn as_u64(&self) -> u64

Source§

fn as_u128(&self) -> u128

Source§

fn as_usize(&self) -> usize

Source§

impl Number for u32

Source§

const BITS: usize = 32usize

Source§

const MIN: Self = 0u32

Source§

const MAX: Self = 4_294_967_295u32

Source§

type UnderlyingType = u32

Source§

fn new(value: Self::UnderlyingType) -> Self

Source§

fn try_new(value: Self::UnderlyingType) -> Result<Self, TryNewError>

Source§

fn value(self) -> Self::UnderlyingType

Source§

fn from_<T: Number>(value: T) -> Self

Source§

fn masked_new<T: Number>(value: T) -> Self

Source§

fn as_u8(&self) -> u8

Source§

fn as_u16(&self) -> u16

Source§

fn as_u32(&self) -> u32

Source§

fn as_u64(&self) -> u64

Source§

fn as_u128(&self) -> u128

Source§

fn as_usize(&self) -> usize

Source§

impl Number for u64

Source§

const BITS: usize = 64usize

Source§

const MIN: Self = 0u64

Source§

const MAX: Self = 18_446_744_073_709_551_615u64

Source§

type UnderlyingType = u64

Source§

fn new(value: Self::UnderlyingType) -> Self

Source§

fn try_new(value: Self::UnderlyingType) -> Result<Self, TryNewError>

Source§

fn value(self) -> Self::UnderlyingType

Source§

fn from_<T: Number>(value: T) -> Self

Source§

fn masked_new<T: Number>(value: T) -> Self

Source§

fn as_u8(&self) -> u8

Source§

fn as_u16(&self) -> u16

Source§

fn as_u32(&self) -> u32

Source§

fn as_u64(&self) -> u64

Source§

fn as_u128(&self) -> u128

Source§

fn as_usize(&self) -> usize

Source§

impl Number for u128

Source§

const BITS: usize = 128usize

Source§

const MIN: Self = 0u128

Source§

const MAX: Self = 340_282_366_920_938_463_463_374_607_431_768_211_455u128

Source§

type UnderlyingType = u128

Source§

fn new(value: Self::UnderlyingType) -> Self

Source§

fn try_new(value: Self::UnderlyingType) -> Result<Self, TryNewError>

Source§

fn value(self) -> Self::UnderlyingType

Source§

fn from_<T: Number>(value: T) -> Self

Source§

fn masked_new<T: Number>(value: T) -> Self

Source§

fn as_u8(&self) -> u8

Source§

fn as_u16(&self) -> u16

Source§

fn as_u32(&self) -> u32

Source§

fn as_u64(&self) -> u64

Source§

fn as_u128(&self) -> u128

Source§

fn as_usize(&self) -> usize

Implementors§

Source§

impl<const BITS: usize> Number for UInt<u8, BITS>

Source§

const BITS: usize = BITS

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = u8

Source§

impl<const BITS: usize> Number for UInt<u16, BITS>

Source§

const BITS: usize = BITS

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = u16

Source§

impl<const BITS: usize> Number for UInt<u32, BITS>

Source§

const BITS: usize = BITS

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = u32

Source§

impl<const BITS: usize> Number for UInt<u64, BITS>

Source§

const BITS: usize = BITS

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = u64

Source§

impl<const BITS: usize> Number for UInt<u128, BITS>

Source§

const BITS: usize = BITS

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = u128