pub trait PrimInt: Sized + Copy + Num<Output = Self, Output = Self, Output = Self, Output = Self> + NumCast + Bounded + PartialOrd + Ord + Eq + Not<Output = Self> + BitAnd<Output = Self> + BitOr<Output = Self> + BitXor<Output = Self> + Shl<usize, Output = Self> + Shr<usize, Output = Self> + CheckedAdd + CheckedSub + CheckedMul + CheckedDiv + Saturating {
Show 19 methods // Required methods fn count_ones(self) -> u32; fn count_zeros(self) -> u32; fn leading_zeros(self) -> u32; fn trailing_zeros(self) -> u32; fn rotate_left(self, n: u32) -> Self; fn rotate_right(self, n: u32) -> Self; fn signed_shl(self, n: u32) -> Self; fn signed_shr(self, n: u32) -> Self; fn unsigned_shl(self, n: u32) -> Self; fn unsigned_shr(self, n: u32) -> Self; fn swap_bytes(self) -> Self; fn from_be(x: Self) -> Self; fn from_le(x: Self) -> Self; fn to_be(self) -> Self; fn to_le(self) -> Self; fn pow(self, exp: u32) -> Self; // Provided methods fn leading_ones(self) -> u32 { ... } fn trailing_ones(self) -> u32 { ... } fn reverse_bits(self) -> 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 trait extends these with introspective queries (e.g., PrimInt::count_ones(), PrimInt::leading_zeros()), bitwise combinators (e.g., PrimInt::rotate_left()), and endianness converters (e.g., PrimInt::to_be()).

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.

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 count_ones(self) -> u32

Returns the number of ones in the binary representation of self.

Examples
use num_traits::PrimInt;

let n = 0b01001100u8;

assert_eq!(n.count_ones(), 3);
source

fn count_zeros(self) -> u32

Returns the number of zeros in the binary representation of self.

Examples
use num_traits::PrimInt;

let n = 0b01001100u8;

assert_eq!(n.count_zeros(), 5);
source

fn leading_zeros(self) -> u32

Returns the number of leading zeros in the binary representation of self.

Examples
use num_traits::PrimInt;

let n = 0b0101000u16;

assert_eq!(n.leading_zeros(), 10);
source

fn trailing_zeros(self) -> u32

Returns the number of trailing zeros in the binary representation of self.

Examples
use num_traits::PrimInt;

let n = 0b0101000u16;

assert_eq!(n.trailing_zeros(), 3);
source

fn rotate_left(self, n: u32) -> Self

Shifts the bits to the left by a specified amount, n, wrapping the truncated bits to the end of the resulting integer.

Examples
use num_traits::PrimInt;

let n = 0x0123456789ABCDEFu64;
let m = 0x3456789ABCDEF012u64;

assert_eq!(n.rotate_left(12), m);
source

fn rotate_right(self, n: u32) -> Self

Shifts the bits to the right by a specified amount, n, wrapping the truncated bits to the beginning of the resulting integer.

Examples
use num_traits::PrimInt;

let n = 0x0123456789ABCDEFu64;
let m = 0xDEF0123456789ABCu64;

assert_eq!(n.rotate_right(12), m);
source

fn signed_shl(self, n: u32) -> Self

Shifts the bits to the left by a specified amount, n, filling zeros in the least significant bits.

This is bitwise equivalent to signed Shl.

Examples
use num_traits::PrimInt;

let n = 0x0123456789ABCDEFu64;
let m = 0x3456789ABCDEF000u64;

assert_eq!(n.signed_shl(12), m);
source

fn signed_shr(self, n: u32) -> Self

Shifts the bits to the right by a specified amount, n, copying the “sign bit” in the most significant bits even for unsigned types.

This is bitwise equivalent to signed Shr.

Examples
use num_traits::PrimInt;

let n = 0xFEDCBA9876543210u64;
let m = 0xFFFFEDCBA9876543u64;

assert_eq!(n.signed_shr(12), m);
source

fn unsigned_shl(self, n: u32) -> Self

Shifts the bits to the left by a specified amount, n, filling zeros in the least significant bits.

This is bitwise equivalent to unsigned Shl.

Examples
use num_traits::PrimInt;

let n = 0x0123456789ABCDEFi64;
let m = 0x3456789ABCDEF000i64;

assert_eq!(n.unsigned_shl(12), m);
source

fn unsigned_shr(self, n: u32) -> Self

Shifts the bits to the right by a specified amount, n, filling zeros in the most significant bits.

This is bitwise equivalent to unsigned Shr.

Examples
use num_traits::PrimInt;

let n = -8i8; // 0b11111000
let m = 62i8; // 0b00111110

assert_eq!(n.unsigned_shr(2), m);
source

fn swap_bytes(self) -> Self

Reverses the byte order of the integer.

Examples
use num_traits::PrimInt;

let n = 0x0123456789ABCDEFu64;
let m = 0xEFCDAB8967452301u64;

assert_eq!(n.swap_bytes(), m);
source

fn from_be(x: Self) -> Self

Convert an integer from big endian to the target’s endianness.

On big endian this is a no-op. On little endian the bytes are swapped.

Examples
use num_traits::PrimInt;

let n = 0x0123456789ABCDEFu64;

if cfg!(target_endian = "big") {
    assert_eq!(u64::from_be(n), n)
} else {
    assert_eq!(u64::from_be(n), n.swap_bytes())
}
source

fn from_le(x: Self) -> Self

Convert an integer from little endian to the target’s endianness.

On little endian this is a no-op. On big endian the bytes are swapped.

Examples
use num_traits::PrimInt;

let n = 0x0123456789ABCDEFu64;

if cfg!(target_endian = "little") {
    assert_eq!(u64::from_le(n), n)
} else {
    assert_eq!(u64::from_le(n), n.swap_bytes())
}
source

fn to_be(self) -> Self

Convert self to big endian from the target’s endianness.

On big endian this is a no-op. On little endian the bytes are swapped.

Examples
use num_traits::PrimInt;

let n = 0x0123456789ABCDEFu64;

if cfg!(target_endian = "big") {
    assert_eq!(n.to_be(), n)
} else {
    assert_eq!(n.to_be(), n.swap_bytes())
}
source

fn to_le(self) -> Self

Convert self to little endian from the target’s endianness.

On little endian this is a no-op. On big endian the bytes are swapped.

Examples
use num_traits::PrimInt;

let n = 0x0123456789ABCDEFu64;

if cfg!(target_endian = "little") {
    assert_eq!(n.to_le(), n)
} else {
    assert_eq!(n.to_le(), n.swap_bytes())
}
source

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

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

Examples
use num_traits::PrimInt;

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

Provided Methods§

source

fn leading_ones(self) -> u32

Returns the number of leading ones in the binary representation of self.

Examples
use num_traits::PrimInt;

let n = 0xF00Du16;

assert_eq!(n.leading_ones(), 4);
source

fn trailing_ones(self) -> u32

Returns the number of trailing ones in the binary representation of self.

Examples
use num_traits::PrimInt;

let n = 0xBEEFu16;

assert_eq!(n.trailing_ones(), 4);
source

fn reverse_bits(self) -> Self

Reverses the order of bits in the integer.

The least significant bit becomes the most significant bit, second least-significant bit becomes second most-significant bit, etc.

Examples
use num_traits::PrimInt;

let n = 0x12345678u32;
let m = 0x1e6a2c48u32;

assert_eq!(n.reverse_bits(), m);
assert_eq!(0u32.reverse_bits(), 0);

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl PrimInt for i8

source§

fn count_ones(self) -> u32

source§

fn count_zeros(self) -> u32

source§

fn leading_ones(self) -> u32

source§

fn leading_zeros(self) -> u32

source§

fn trailing_ones(self) -> u32

source§

fn trailing_zeros(self) -> u32

source§

fn rotate_left(self, n: u32) -> i8

source§

fn rotate_right(self, n: u32) -> i8

source§

fn signed_shl(self, n: u32) -> i8

source§

fn signed_shr(self, n: u32) -> i8

source§

fn unsigned_shl(self, n: u32) -> i8

source§

fn unsigned_shr(self, n: u32) -> i8

source§

fn swap_bytes(self) -> i8

source§

fn reverse_bits(self) -> i8

source§

fn from_be(x: i8) -> i8

source§

fn from_le(x: i8) -> i8

source§

fn to_be(self) -> i8

source§

fn to_le(self) -> i8

source§

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

source§

impl PrimInt for i16

source§

impl PrimInt for i32

source§

impl PrimInt for i64

source§

impl PrimInt for i128

source§

impl PrimInt for isize

source§

impl PrimInt for u8

source§

fn count_ones(self) -> u32

source§

fn count_zeros(self) -> u32

source§

fn leading_ones(self) -> u32

source§

fn leading_zeros(self) -> u32

source§

fn trailing_ones(self) -> u32

source§

fn trailing_zeros(self) -> u32

source§

fn rotate_left(self, n: u32) -> u8

source§

fn rotate_right(self, n: u32) -> u8

source§

fn signed_shl(self, n: u32) -> u8

source§

fn signed_shr(self, n: u32) -> u8

source§

fn unsigned_shl(self, n: u32) -> u8

source§

fn unsigned_shr(self, n: u32) -> u8

source§

fn swap_bytes(self) -> u8

source§

fn reverse_bits(self) -> u8

source§

fn from_be(x: u8) -> u8

source§

fn from_le(x: u8) -> u8

source§

fn to_be(self) -> u8

source§

fn to_le(self) -> u8

source§

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

source§

impl PrimInt for u16

source§

impl PrimInt for u32

source§

impl PrimInt for u64

source§

impl PrimInt for u128

source§

impl PrimInt for usize

Implementors§