Integer

Trait Integer 

Source
pub trait Integer:
    Sized
    + Copy
    + Clone
    + PartialOrd
    + Ord
    + PartialEq
    + Eq
    + Debug
    + Display
    + LowerHex
    + UpperHex
    + Octal
    + Binary
    + Sealed
    + Add<Self, Output = Self>
    + AddAssign<Self>
    + Sub<Self, Output = Self>
    + SubAssign<Self>
    + Div<Self, Output = Self>
    + DivAssign<Self>
    + Mul<Self, Output = Self>
    + MulAssign<Self>
    + Not<Output = Self>
    + BitAnd<Self, Output = Self>
    + BitAndAssign<Self>
    + BitOr<Self, Output = Self>
    + BitOrAssign<Self>
    + BitXor<Self, Output = Self>
    + BitXorAssign<Self> {
    type UnderlyingType: Integer + BuiltinInteger + Debug + TryFrom<u8> + TryFrom<u16> + TryFrom<u32> + TryFrom<u64> + TryFrom<u128>;
    type UnsignedInteger: UnsignedInteger;
    type SignedInteger: SignedInteger;

    const BITS: usize;
    const ZERO: Self;
    const MIN: Self;
    const MAX: Self;
Show 20 methods // 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: Integer>(value: T) -> Self; fn masked_new<T: Integer>(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; fn as_i8(self) -> i8; fn as_i16(self) -> i16; fn as_i32(self) -> i32; fn as_i64(self) -> i64; fn as_i128(self) -> i128; fn as_isize(self) -> isize; fn to_unsigned(self) -> Self::UnsignedInteger; fn from_unsigned(value: Self::UnsignedInteger) -> Self; // Provided method fn as_<T: Integer>(self) -> T { ... }
}
Expand description

The base trait for integer numbers, either built-in (u8, i8, u16, i16, u32, i32, u64, i64, u128, i128) or arbitrary-int (u1, i1, u7, i7 etc.).

Required Associated Constants§

Source

const BITS: usize

Number of bits that can fit in this type

Source

const ZERO: Self

The number 0

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§

Source

type UnderlyingType: Integer + BuiltinInteger + Debug + TryFrom<u8> + TryFrom<u16> + TryFrom<u32> + TryFrom<u64> + TryFrom<u128>

Source

type UnsignedInteger: UnsignedInteger

An equivalent type with the same number of bits but unsigned. If the type is already unsigned, this is the same type.

  • i4::UnsignedInteger == u4
  • u60::SignedInteger == u60
Source

type SignedInteger: SignedInteger

An equivalent type with the same number of bits but signed. If the type is already signed, this is the same type.

  • i4::UnsignedInteger == i4
  • u60::SignedInteger == i60

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: Integer>(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 convertible to T. Use Self::new for literals.

Source

fn masked_new<T: Integer>(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

Source

fn as_i8(self) -> i8

Source

fn as_i16(self) -> i16

Source

fn as_i32(self) -> i32

Source

fn as_i64(self) -> i64

Source

fn as_i128(self) -> i128

Source

fn as_isize(self) -> isize

Source

fn to_unsigned(self) -> Self::UnsignedInteger

Converts the number to its unsigned equivalent. For types that have fewer bits than the underlying type, this involves a zero extension. Types that are already unsigned will return themselves.

Source

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Converts the number from its unsigned equivalent. For types that have fewer bits than the underlying type, this involves a sign extension, if this type is a signed type. Types that are already unsigned will return themselves.

Provided Methods§

Source

fn as_<T: Integer>(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 Integer for i8

Source§

const BITS: usize = 8usize

Source§

const ZERO: Self = 0i8

Source§

const MIN: Self = -128i8

Source§

const MAX: Self = 127i8

Source§

type UnderlyingType = i8

Source§

type UnsignedInteger = u8

Source§

type SignedInteger = i8

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: Integer>(value: T) -> Self

Source§

fn masked_new<T: Integer>(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§

fn as_i8(self) -> i8

Source§

fn as_i16(self) -> i16

Source§

fn as_i32(self) -> i32

Source§

fn as_i64(self) -> i64

Source§

fn as_i128(self) -> i128

Source§

fn as_isize(self) -> isize

Source§

fn to_unsigned(self) -> Self::UnsignedInteger

Source§

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Source§

impl Integer for i16

Source§

const BITS: usize = 16usize

Source§

const ZERO: Self = 0i16

Source§

const MIN: Self = -32_768i16

Source§

const MAX: Self = 32_767i16

Source§

type UnderlyingType = i16

Source§

type UnsignedInteger = u16

Source§

type SignedInteger = i16

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: Integer>(value: T) -> Self

Source§

fn masked_new<T: Integer>(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§

fn as_i8(self) -> i8

Source§

fn as_i16(self) -> i16

Source§

fn as_i32(self) -> i32

Source§

fn as_i64(self) -> i64

Source§

fn as_i128(self) -> i128

Source§

fn as_isize(self) -> isize

Source§

fn to_unsigned(self) -> Self::UnsignedInteger

Source§

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Source§

impl Integer for i32

Source§

const BITS: usize = 32usize

Source§

const ZERO: Self = 0i32

Source§

const MIN: Self = -2_147_483_648i32

Source§

const MAX: Self = 2_147_483_647i32

Source§

type UnderlyingType = i32

Source§

type UnsignedInteger = u32

Source§

type SignedInteger = i32

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: Integer>(value: T) -> Self

Source§

fn masked_new<T: Integer>(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§

fn as_i8(self) -> i8

Source§

fn as_i16(self) -> i16

Source§

fn as_i32(self) -> i32

Source§

fn as_i64(self) -> i64

Source§

fn as_i128(self) -> i128

Source§

fn as_isize(self) -> isize

Source§

fn to_unsigned(self) -> Self::UnsignedInteger

Source§

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Source§

impl Integer for i64

Source§

const BITS: usize = 64usize

Source§

const ZERO: Self = 0i64

Source§

const MIN: Self = -9_223_372_036_854_775_808i64

Source§

const MAX: Self = 9_223_372_036_854_775_807i64

Source§

type UnderlyingType = i64

Source§

type UnsignedInteger = u64

Source§

type SignedInteger = i64

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: Integer>(value: T) -> Self

Source§

fn masked_new<T: Integer>(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§

fn as_i8(self) -> i8

Source§

fn as_i16(self) -> i16

Source§

fn as_i32(self) -> i32

Source§

fn as_i64(self) -> i64

Source§

fn as_i128(self) -> i128

Source§

fn as_isize(self) -> isize

Source§

fn to_unsigned(self) -> Self::UnsignedInteger

Source§

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Source§

impl Integer for i128

Source§

const BITS: usize = 128usize

Source§

const ZERO: Self = 0i128

Source§

const MIN: Self = -170_141_183_460_469_231_731_687_303_715_884_105_728i128

Source§

const MAX: Self = 170_141_183_460_469_231_731_687_303_715_884_105_727i128

Source§

type UnderlyingType = i128

Source§

type UnsignedInteger = u128

Source§

type SignedInteger = i128

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: Integer>(value: T) -> Self

Source§

fn masked_new<T: Integer>(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§

fn as_i8(self) -> i8

Source§

fn as_i16(self) -> i16

Source§

fn as_i32(self) -> i32

Source§

fn as_i64(self) -> i64

Source§

fn as_i128(self) -> i128

Source§

fn as_isize(self) -> isize

Source§

fn to_unsigned(self) -> Self::UnsignedInteger

Source§

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Source§

impl Integer for u8

Source§

const BITS: usize = 8usize

Source§

const ZERO: Self = 0u8

Source§

const MIN: Self = 0u8

Source§

const MAX: Self = 255u8

Source§

type UnderlyingType = u8

Source§

type UnsignedInteger = u8

Source§

type SignedInteger = i8

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: Integer>(value: T) -> Self

Source§

fn masked_new<T: Integer>(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§

fn as_i8(self) -> i8

Source§

fn as_i16(self) -> i16

Source§

fn as_i32(self) -> i32

Source§

fn as_i64(self) -> i64

Source§

fn as_i128(self) -> i128

Source§

fn as_isize(self) -> isize

Source§

fn to_unsigned(self) -> Self::UnsignedInteger

Source§

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Source§

impl Integer for u16

Source§

const BITS: usize = 16usize

Source§

const ZERO: Self = 0u16

Source§

const MIN: Self = 0u16

Source§

const MAX: Self = 65_535u16

Source§

type UnderlyingType = u16

Source§

type UnsignedInteger = u16

Source§

type SignedInteger = i16

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: Integer>(value: T) -> Self

Source§

fn masked_new<T: Integer>(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§

fn as_i8(self) -> i8

Source§

fn as_i16(self) -> i16

Source§

fn as_i32(self) -> i32

Source§

fn as_i64(self) -> i64

Source§

fn as_i128(self) -> i128

Source§

fn as_isize(self) -> isize

Source§

fn to_unsigned(self) -> Self::UnsignedInteger

Source§

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Source§

impl Integer for u32

Source§

const BITS: usize = 32usize

Source§

const ZERO: Self = 0u32

Source§

const MIN: Self = 0u32

Source§

const MAX: Self = 4_294_967_295u32

Source§

type UnderlyingType = u32

Source§

type UnsignedInteger = u32

Source§

type SignedInteger = i32

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: Integer>(value: T) -> Self

Source§

fn masked_new<T: Integer>(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§

fn as_i8(self) -> i8

Source§

fn as_i16(self) -> i16

Source§

fn as_i32(self) -> i32

Source§

fn as_i64(self) -> i64

Source§

fn as_i128(self) -> i128

Source§

fn as_isize(self) -> isize

Source§

fn to_unsigned(self) -> Self::UnsignedInteger

Source§

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Source§

impl Integer for u64

Source§

const BITS: usize = 64usize

Source§

const ZERO: Self = 0u64

Source§

const MIN: Self = 0u64

Source§

const MAX: Self = 18_446_744_073_709_551_615u64

Source§

type UnderlyingType = u64

Source§

type UnsignedInteger = u64

Source§

type SignedInteger = i64

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: Integer>(value: T) -> Self

Source§

fn masked_new<T: Integer>(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§

fn as_i8(self) -> i8

Source§

fn as_i16(self) -> i16

Source§

fn as_i32(self) -> i32

Source§

fn as_i64(self) -> i64

Source§

fn as_i128(self) -> i128

Source§

fn as_isize(self) -> isize

Source§

fn to_unsigned(self) -> Self::UnsignedInteger

Source§

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Source§

impl Integer for u128

Source§

const BITS: usize = 128usize

Source§

const ZERO: Self = 0u128

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§

type UnsignedInteger = u128

Source§

type SignedInteger = i128

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: Integer>(value: T) -> Self

Source§

fn masked_new<T: Integer>(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§

fn as_i8(self) -> i8

Source§

fn as_i16(self) -> i16

Source§

fn as_i32(self) -> i32

Source§

fn as_i64(self) -> i64

Source§

fn as_i128(self) -> i128

Source§

fn as_isize(self) -> isize

Source§

fn to_unsigned(self) -> Self::UnsignedInteger

Source§

fn from_unsigned(value: Self::UnsignedInteger) -> Self

Implementors§

Source§

impl<const BITS: usize> Integer for Int<i8, BITS>

Source§

const BITS: usize = BITS

Source§

const ZERO: Self

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = i8

Source§

type SignedInteger = Int<i8, BITS>

Source§

type UnsignedInteger = UInt<u8, BITS>

Source§

impl<const BITS: usize> Integer for Int<i16, BITS>

Source§

const BITS: usize = BITS

Source§

const ZERO: Self

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = i16

Source§

type SignedInteger = Int<i16, BITS>

Source§

type UnsignedInteger = UInt<u16, BITS>

Source§

impl<const BITS: usize> Integer for Int<i32, BITS>

Source§

const BITS: usize = BITS

Source§

const ZERO: Self

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = i32

Source§

type SignedInteger = Int<i32, BITS>

Source§

type UnsignedInteger = UInt<u32, BITS>

Source§

impl<const BITS: usize> Integer for Int<i64, BITS>

Source§

const BITS: usize = BITS

Source§

const ZERO: Self

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = i64

Source§

type SignedInteger = Int<i64, BITS>

Source§

type UnsignedInteger = UInt<u64, BITS>

Source§

impl<const BITS: usize> Integer for Int<i128, BITS>

Source§

const BITS: usize = BITS

Source§

const ZERO: Self

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = i128

Source§

type SignedInteger = Int<i128, BITS>

Source§

type UnsignedInteger = UInt<u128, BITS>

Source§

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

Source§

const BITS: usize = BITS

Source§

const ZERO: Self

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = u8

Source§

type SignedInteger = Int<i8, BITS>

Source§

type UnsignedInteger = UInt<u8, BITS>

Source§

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

Source§

const BITS: usize = BITS

Source§

const ZERO: Self

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = u16

Source§

type SignedInteger = Int<i16, BITS>

Source§

type UnsignedInteger = UInt<u16, BITS>

Source§

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

Source§

const BITS: usize = BITS

Source§

const ZERO: Self

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = u32

Source§

type SignedInteger = Int<i32, BITS>

Source§

type UnsignedInteger = UInt<u32, BITS>

Source§

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

Source§

const BITS: usize = BITS

Source§

const ZERO: Self

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = u64

Source§

type SignedInteger = Int<i64, BITS>

Source§

type UnsignedInteger = UInt<u64, BITS>

Source§

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

Source§

const BITS: usize = BITS

Source§

const ZERO: Self

Source§

const MIN: Self

Source§

const MAX: Self

Source§

type UnderlyingType = u128

Source§

type SignedInteger = Int<i128, BITS>

Source§

type UnsignedInteger = UInt<u128, BITS>