Skip to main content

PowerOfTwo

Struct PowerOfTwo 

Source
pub struct PowerOfTwo<T> { /* private fields */ }
Expand description

Proof that a value is a power of two (2^k, k ≥ 0), for an unsigned integer type T.

Representation is the exponent k, not the value — so the consuming operations in PowerOfTwoOps are pure shifts and masks with nothing recomputed per call, and a (future) big-integer backend carries a tiny u32 proof regardless of its width. The field is private, so the representation is an implementation detail. Consequently PowerOfTwo does not deref to T; use get (which reconstructs 1 << k) or exp.

PowerOfTwo<T> is always Copy (it stores only a u32), independent of whether T is.

§Examples

use const_num_traits::{PowerOfTwo, PowerOfTwoOps};

let p = PowerOfTwo::<u32>::new(16).unwrap();
assert_eq!(p.exp(), 4);
assert_eq!(p.get(), 16);
assert_eq!(100u32.div_pow2(p), 6); // 100 / 16
assert_eq!(100u32.rem_pow2(p), 4); // 100 % 16
assert!(PowerOfTwo::<u32>::new(6).is_none());

Implementations§

Source§

impl PowerOfTwo<u8>

Source

pub fn new_ct(value: u8) -> CtOption<PowerOfTwo<u8>>

Available on crate feature ct only.

Constant-time new: a CtOption that is None-masked when value is not a power of two.

Source§

impl PowerOfTwo<u16>

Source

pub fn new_ct(value: u16) -> CtOption<PowerOfTwo<u16>>

Available on crate feature ct only.

Constant-time new: a CtOption that is None-masked when value is not a power of two.

Source§

impl PowerOfTwo<u32>

Source

pub fn new_ct(value: u32) -> CtOption<PowerOfTwo<u32>>

Available on crate feature ct only.

Constant-time new: a CtOption that is None-masked when value is not a power of two.

Source§

impl PowerOfTwo<u64>

Source

pub fn new_ct(value: u64) -> CtOption<PowerOfTwo<u64>>

Available on crate feature ct only.

Constant-time new: a CtOption that is None-masked when value is not a power of two.

Source§

impl PowerOfTwo<u128>

Source

pub fn new_ct(value: u128) -> CtOption<PowerOfTwo<u128>>

Available on crate feature ct only.

Constant-time new: a CtOption that is None-masked when value is not a power of two.

Source§

impl PowerOfTwo<usize>

Source

pub fn new_ct(value: usize) -> CtOption<PowerOfTwo<usize>>

Available on crate feature ct only.

Constant-time new: a CtOption that is None-masked when value is not a power of two.

Source§

impl<T> PowerOfTwo<T>

Source

pub const unsafe fn from_exp_unchecked(exp: u32) -> Self

Constructs the proof directly from an exponent, without checking.

§Safety

exp must be < T::BITS (so that 1 << exp is a valid power of two of T). Passing a too-large exponent makes the consuming ops produce nonsense or overflow.

Source

pub const fn exp(self) -> u32

The exponent k such that the proven value is 2^k. Free (no recompute).

Source§

impl<T> PowerOfTwo<T>

Source

pub fn new_checked(value: T) -> Option<Self>

Safe constructor for any carrier, not just the primitives: Some iff value is a power of two. Keeps the unsafe crate-internal; the per-primitive new stays the const fast path.

Source§

impl PowerOfTwo<u8>

Source

pub const fn new(value: u8) -> Option<Self>

Checked constructor: Some iff value is a power of two.

const fn on stable and nightly (delegates to the inherent is_power_of_two/ilog2, both const since ≤ 1.67).

Source

pub const fn get(self) -> u8

Reconstructs the proven value, 1 << exp.

Source§

impl PowerOfTwo<u16>

Source

pub const fn new(value: u16) -> Option<Self>

Checked constructor: Some iff value is a power of two.

const fn on stable and nightly (delegates to the inherent is_power_of_two/ilog2, both const since ≤ 1.67).

Source

pub const fn get(self) -> u16

Reconstructs the proven value, 1 << exp.

Source§

impl PowerOfTwo<u32>

Source

pub const fn new(value: u32) -> Option<Self>

Checked constructor: Some iff value is a power of two.

const fn on stable and nightly (delegates to the inherent is_power_of_two/ilog2, both const since ≤ 1.67).

Source

pub const fn get(self) -> u32

Reconstructs the proven value, 1 << exp.

Source§

impl PowerOfTwo<u64>

Source

pub const fn new(value: u64) -> Option<Self>

Checked constructor: Some iff value is a power of two.

const fn on stable and nightly (delegates to the inherent is_power_of_two/ilog2, both const since ≤ 1.67).

Examples found in repository?
examples/typestates.rs (line 22)
20fn power_of_two() {
21    let radix = 256u64;
22    let p = PowerOfTwo::<u64>::new(radix).expect("256 is 2^8");
23
24    // `x % radix` and `x / radix` without a division instruction
25    assert_eq!(700u64.rem_pow2(p), 700 % 256); // & 0xFF
26    assert_eq!(700u64.div_pow2(p), 700 / 256); // >> 8
27
28    // align-up to the next multiple, branch-free; checked form for the edge
29    assert_eq!(700u64.next_multiple_of_pow2(p), 768);
30    assert_eq!(u64::MAX.checked_next_multiple_of_pow2(p), None);
31
32    // built once from a runtime value, spent in a loop with no per-iter recheck
33    let mut folded = 0u64;
34    for limb in [10u64, 600, 70_000] {
35        folded ^= limb.rem_pow2(p);
36    }
37    assert_eq!(folded, (10 ^ 600 ^ 70_000) & 0xFF);
38}
Source

pub const fn get(self) -> u64

Reconstructs the proven value, 1 << exp.

Source§

impl PowerOfTwo<u128>

Source

pub const fn new(value: u128) -> Option<Self>

Checked constructor: Some iff value is a power of two.

const fn on stable and nightly (delegates to the inherent is_power_of_two/ilog2, both const since ≤ 1.67).

Source

pub const fn get(self) -> u128

Reconstructs the proven value, 1 << exp.

Source§

impl PowerOfTwo<usize>

Source

pub const fn new(value: usize) -> Option<Self>

Checked constructor: Some iff value is a power of two.

const fn on stable and nightly (delegates to the inherent is_power_of_two/ilog2, both const since ≤ 1.67).

Source

pub const fn get(self) -> usize

Reconstructs the proven value, 1 << exp.

Trait Implementations§

Source§

impl<T> Clone for PowerOfTwo<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T> Copy for PowerOfTwo<T>

Source§

impl TryFrom<u8> for PowerOfTwo<u8>

Checked construction by value; mirrors PowerOfTwo::new. (The generic carrier path stays PowerOfTwo::new_checked.)

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u8) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u16> for PowerOfTwo<u16>

Checked construction by value; mirrors PowerOfTwo::new. (The generic carrier path stays PowerOfTwo::new_checked.)

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u16) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u32> for PowerOfTwo<u32>

Checked construction by value; mirrors PowerOfTwo::new. (The generic carrier path stays PowerOfTwo::new_checked.)

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u64> for PowerOfTwo<u64>

Checked construction by value; mirrors PowerOfTwo::new. (The generic carrier path stays PowerOfTwo::new_checked.)

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u64) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<u128> for PowerOfTwo<u128>

Checked construction by value; mirrors PowerOfTwo::new. (The generic carrier path stays PowerOfTwo::new_checked.)

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u128) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<usize> for PowerOfTwo<usize>

Checked construction by value; mirrors PowerOfTwo::new. (The generic carrier path stays PowerOfTwo::new_checked.)

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: usize) -> Result<Self, TypestateError>

Performs the conversion.

Auto Trait Implementations§

§

impl<T> Freeze for PowerOfTwo<T>

§

impl<T> RefUnwindSafe for PowerOfTwo<T>
where T: RefUnwindSafe,

§

impl<T> Send for PowerOfTwo<T>
where T: Send,

§

impl<T> Sync for PowerOfTwo<T>
where T: Sync,

§

impl<T> Unpin for PowerOfTwo<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for PowerOfTwo<T>

§

impl<T> UnwindSafe for PowerOfTwo<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.