#[repr(transparent)]pub struct Pow2(_);
Expand description
Represents a non-negative power of 2, by storing its exponent.
This type is limited in range to 2^0 .. 2^255
(inclusive). It is simply a wrapper around a
u8
, which stores the exponent.
See module docs for more info.
Implementations§
source§impl Pow2
impl Pow2
sourcepub const fn from_exponent(exponent: u8) -> Pow2
pub const fn from_exponent(exponent: u8) -> Pow2
Creates a Pow2
from an exponent. Note that this function does not check whether the
given exponent is valid for any specific primitive type; many values will be useless.
For example, using Pow2::from_exponent(250)
will not be useful for any current primitive
type.
sourcepub const fn align_of<T>() -> Pow2
pub const fn align_of<T>() -> Pow2
Returns a Pow2
value for the memory alignment of a given type T
.
use pow2::Pow2;
assert_eq!(Pow2::align_of::<u32>(), Pow2::from_exponent(2));
assert_eq!(Pow2::align_of::<u64>(), Pow2::from_exponent(3));
sourcepub fn checked_mul(self, other: Pow2) -> Option<Pow2>
pub fn checked_mul(self, other: Pow2) -> Option<Pow2>
Multiplies two Pow2
values by adding their exponents. If the values are out of range,
returns None
.
use pow2::Pow2;
assert_eq!(Pow2::from_exponent(2).checked_mul(Pow2::from_exponent(3)), Some(Pow2::from_exponent(5)));
assert_eq!(Pow2::from_exponent(200).checked_mul(Pow2::from_exponent(200)), None);
sourcepub fn checked_div(self, other: Pow2) -> Option<Pow2>
pub fn checked_div(self, other: Pow2) -> Option<Pow2>
Divides a Pow2
value by another Pow2
. If the divisor is greater than the quotient,
then returns None
.
use pow2::Pow2;
assert_eq!(Pow2::from_exponent(4).checked_div(Pow2::from_exponent(1)), Some(Pow2::from_exponent(3)));
assert_eq!(Pow2::from_exponent(4).checked_div(Pow2::from_exponent(8)), None);
sourcepub const fn mul(self, other: Pow2) -> Pow2
pub const fn mul(self, other: Pow2) -> Pow2
Multiplies two Pow2
values by adding their exponents. If the values are out of range,
then normal arithmetic overflow handling occurs.
use pow2::Pow2;
assert_eq!(Pow2::from_exponent(4) * Pow2::from_exponent(3), Pow2::from_exponent(7));
sourcepub const fn div(self, other: Pow2) -> Pow2
pub const fn div(self, other: Pow2) -> Pow2
Divides a Pow2
value by another Pow2
. If the divisor is greater than the quotient,
then normal arithmetic overflow handling occurs.
use pow2::Pow2;
assert_eq!(Pow2::from_exponent(12) / Pow2::from_exponent(2), Pow2::from_exponent(10));
sourcepub fn is_aligned<T: IntPow2>(self, n: T) -> bool
pub fn is_aligned<T: IntPow2>(self, n: T) -> bool
Returns true
if n
is an integer multiple of self
(is aligned).
use pow2::Pow2;
const PAGE_SIZE: Pow2 = Pow2::from_exponent(12);
assert_eq!(PAGE_SIZE.is_aligned(0u32), true);
assert_eq!(PAGE_SIZE.is_aligned(1000u32), false);
assert_eq!(PAGE_SIZE.is_aligned(4095u32), false);
assert_eq!(PAGE_SIZE.is_aligned(4096u32), true);
assert_eq!(PAGE_SIZE.is_aligned(4097u32), false);
assert_eq!(PAGE_SIZE.is_aligned(8192u32), true);
assert_eq!(PAGE_SIZE.is_aligned(core::u32::MAX), false);
sourcepub fn align_up<T: IntPow2>(self, n: T) -> Option<T>
pub fn align_up<T: IntPow2>(self, n: T) -> Option<T>
Returns the smallest multiple of self
(a power of 2) that is greater than or equal to n
.
If the aligned value would overflow the range of T
, then this function returns None
.
use pow2::Pow2;
const PAGE_SIZE: Pow2 = Pow2::from_exponent(12);
assert_eq!(PAGE_SIZE.align_up(5000u32), Some(8192));
assert_eq!(PAGE_SIZE.align_up(core::u32::MAX), None); // would overflow
sourcepub fn align_up_unchecked<T: IntPow2>(self, n: T) -> T
pub fn align_up_unchecked<T: IntPow2>(self, n: T) -> T
Returns the smallest multiple of self
(a power of 2) that is greater than or equal to n
.
If the aligned value would overflow the range of T
, then normal arithmetic overflow
handling occurs.
use pow2::Pow2;
sourcepub fn align_down<T: IntPow2>(self, n: T) -> T
pub fn align_down<T: IntPow2>(self, n: T) -> T
Returns the largest multiple of self
(a power of 2) that is less than or equal to n
.
Trait Implementations§
source§impl Ord for Pow2
impl Ord for Pow2
source§impl PartialEq<Pow2> for Pow2
impl PartialEq<Pow2> for Pow2
source§impl PartialOrd<Pow2> for Pow2
impl PartialOrd<Pow2> for Pow2
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more