Skip to main content

UnsignedInt

Trait UnsignedInt 

Source
pub trait UnsignedInt: Integer {
    // Required methods
    fn checked_next_power_of_two(self) -> Option<Self>;
    fn is_power_of_two(self) -> bool;
    fn next_power_of_two(self) -> Self;

    // Provided methods
    fn is_non_negative(self) -> bool { ... }
    fn abs(self) -> Self
       where Self: Sized { ... }
}
Expand description

A trait for unsigned integer types.

Extends Integer with operations and guarantees specific to unsigned types.

§Mathematical Background

Unsigned integers represent $\mathbb{N}_0$ (natural numbers including zero) within a bounded range. They do NOT form an additive group under standard arithmetic since subtraction can produce values outside the representable range (negative numbers).

Under modular/wrapping arithmetic, they form $\mathbb{Z}/2^n\mathbb{Z}$, a ring.

Required Methods§

Source

fn checked_next_power_of_two(self) -> Option<Self>

Returns the next power of two greater than or equal to self.

Returns None if the result would overflow.

Source

fn is_power_of_two(self) -> bool

Returns true if self is a power of two.

Source

fn next_power_of_two(self) -> Self

Returns the smallest power of two greater than or equal to self.

§Panics

Panics if the result would overflow.

Provided Methods§

Source

fn is_non_negative(self) -> bool

Returns true. Provided for API consistency with SignedInt.

Source

fn abs(self) -> Self
where Self: Sized,

Returns the value unchanged. Provided for API consistency.

For unsigned types, abs(x) == x always.

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 UnsignedInt for u8

Source§

impl UnsignedInt for u16

Source§

impl UnsignedInt for u32

Source§

impl UnsignedInt for u64

Source§

impl UnsignedInt for u128

Source§

impl UnsignedInt for usize

Implementors§