Struct pow2::Pow2

source ·
#[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

source

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.

source

pub const fn exponent(self) -> u8

Returns the exponent of the Pow2.

source

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));
source

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);
source

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);
source

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));
source

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));
source

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);
source

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
source

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;
source

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 Clone for Pow2

source§

fn clone(&self) -> Pow2

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Pow2

source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Div<Pow2> for Pow2

§

type Output = Pow2

The resulting type after applying the / operator.
source§

fn div(self, other: Pow2) -> Pow2

Performs the / operation. Read more
source§

impl Div<Pow2> for i128

§

type Output = i128

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for i16

§

type Output = i16

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for i32

§

type Output = i32

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for i64

§

type Output = i64

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for i8

§

type Output = i8

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for isize

§

type Output = isize

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for u128

§

type Output = u128

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for u16

§

type Output = u16

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for u32

§

type Output = u32

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for u64

§

type Output = u64

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for u8

§

type Output = u8

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl Div<Pow2> for usize

§

type Output = usize

The resulting type after applying the / operator.
source§

fn div(self, rhs: Pow2) -> Self

Performs the / operation. Read more
source§

impl From<Pow2> for i128

source§

fn from(p: Pow2) -> i128

Converts to this type from the input type.
source§

impl From<Pow2> for i16

source§

fn from(p: Pow2) -> i16

Converts to this type from the input type.
source§

impl From<Pow2> for i32

source§

fn from(p: Pow2) -> i32

Converts to this type from the input type.
source§

impl From<Pow2> for i64

source§

fn from(p: Pow2) -> i64

Converts to this type from the input type.
source§

impl From<Pow2> for i8

source§

fn from(p: Pow2) -> i8

Converts to this type from the input type.
source§

impl From<Pow2> for isize

source§

fn from(p: Pow2) -> isize

Converts to this type from the input type.
source§

impl From<Pow2> for u128

source§

fn from(p: Pow2) -> u128

Converts to this type from the input type.
source§

impl From<Pow2> for u16

source§

fn from(p: Pow2) -> u16

Converts to this type from the input type.
source§

impl From<Pow2> for u32

source§

fn from(p: Pow2) -> u32

Converts to this type from the input type.
source§

impl From<Pow2> for u64

source§

fn from(p: Pow2) -> u64

Converts to this type from the input type.
source§

impl From<Pow2> for u8

source§

fn from(p: Pow2) -> u8

Converts to this type from the input type.
source§

impl From<Pow2> for usize

source§

fn from(p: Pow2) -> usize

Converts to this type from the input type.
source§

impl Hash for Pow2

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Mul<Pow2> for Pow2

§

type Output = Pow2

The resulting type after applying the * operator.
source§

fn mul(self, other: Pow2) -> Pow2

Performs the * operation. Read more
source§

impl Mul<Pow2> for i128

§

type Output = i128

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for i16

§

type Output = i16

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for i32

§

type Output = i32

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for i64

§

type Output = i64

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for i8

§

type Output = i8

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for isize

§

type Output = isize

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for u128

§

type Output = u128

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for u16

§

type Output = u16

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for u32

§

type Output = u32

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for u64

§

type Output = u64

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for u8

§

type Output = u8

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Mul<Pow2> for usize

§

type Output = usize

The resulting type after applying the * operator.
source§

fn mul(self, rhs: Pow2) -> Self

Performs the * operation. Read more
source§

impl Ord for Pow2

source§

fn cmp(&self, other: &Pow2) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Selfwhere Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

Restrict a value to a certain interval. Read more
source§

impl PartialEq<Pow2> for Pow2

source§

fn eq(&self, other: &Pow2) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Pow2> for Pow2

source§

fn partial_cmp(&self, other: &Pow2) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl TryFrom<i128> for Pow2

§

type Error = NotPow2

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

fn try_from(n: i128) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<i16> for Pow2

§

type Error = NotPow2

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

fn try_from(n: i16) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<i32> for Pow2

§

type Error = NotPow2

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

fn try_from(n: i32) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<i64> for Pow2

§

type Error = NotPow2

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

fn try_from(n: i64) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<i8> for Pow2

§

type Error = NotPow2

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

fn try_from(n: i8) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<isize> for Pow2

§

type Error = NotPow2

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

fn try_from(n: isize) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<u128> for Pow2

§

type Error = NotPow2

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

fn try_from(n: u128) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<u16> for Pow2

§

type Error = NotPow2

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

fn try_from(n: u16) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<u32> for Pow2

§

type Error = NotPow2

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

fn try_from(n: u32) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<u64> for Pow2

§

type Error = NotPow2

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

fn try_from(n: u64) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<u8> for Pow2

§

type Error = NotPow2

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

fn try_from(n: u8) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl TryFrom<usize> for Pow2

§

type Error = NotPow2

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

fn try_from(n: usize) -> Result<Pow2, NotPow2>

Performs the conversion.
source§

impl Copy for Pow2

source§

impl Eq for Pow2

source§

impl StructuralEq for Pow2

source§

impl StructuralPartialEq for Pow2

Auto Trait Implementations§

§

impl RefUnwindSafe for Pow2

§

impl Send for Pow2

§

impl Sync for Pow2

§

impl Unpin for Pow2

§

impl UnwindSafe for Pow2

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.