Skip to main content

PrimInt

Trait PrimInt 

Source
pub trait PrimInt:
    Sized
    + Copy
    + PrimBits
    + Num
    + NumCast
    + Bounded
    + PartialOrd
    + Ord
    + Eq
    + CheckedAdd<Output = Self>
    + CheckedSub<Output = Self>
    + CheckedMul<Output = Self>
    + CheckedDiv<Output = Self>
    + Saturating {
    // Required method
    fn pow(self, exp: u32) -> Self;
}
Expand description

Generic trait for primitive integers.

The PrimInt trait is an abstraction over the builtin primitive integer types (e.g., u8, u32, isize, i128, …). It inherits the basic numeric traits and extends them with bitwise operators and non-wrapping arithmetic.

The trait explicitly inherits Copy, Eq, Ord, and Sized. The intention is that all types implementing this trait behave like primitive types that are passed by value by default and behave like builtin integers. Furthermore, the types are expected to expose the integer value in binary representation and support bitwise operators. The standard bitwise operations (e.g., bitwise-and, bitwise-or, right-shift, left-shift) are inherited, and the bit-level introspection methods (e.g., count_ones(), leading_zeros()), bitwise combinators (e.g., rotate_left()), and endianness converters (e.g., to_be()) come from the PrimBits supertrait.

All PrimInt types are expected to be fixed-width binary integers. The width can be queried via T::zero().count_zeros(). The trait currently lacks a way to query the width at compile-time.

While a default implementation for all builtin primitive integers is provided, the trait is in no way restricted to these. Other integer types that fulfil the requirements are free to implement the trait was well.

Types that cannot expose comparison or division (e.g. constant-time integers) should implement only PrimBits; PrimInt adds the Ord/Num/checked-arithmetic capabilities on top.

This trait and many of the method names originate in the unstable core::num::Int trait from the rust standard library. The original trait was never stabilized and thus removed from the standard library.

Required Methods§

Source

fn pow(self, exp: u32) -> Self

Raises self to the power of exp, using exponentiation by squaring.

§Examples
use const_num_traits::PrimInt;

assert_eq!(2i32.pow(4), 16);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl PrimInt for i8

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for i16

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for i32

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for i64

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for i128

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for isize

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for u8

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for u16

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for u32

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for u64

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for u128

Source§

fn pow(self, exp: u32) -> Self

Source§

impl PrimInt for usize

Source§

fn pow(self, exp: u32) -> Self

Implementors§