pub trait AlignExt {
// Required methods
exec fn align_up(self, power_of_two: Self) -> ret : Self;
exec fn align_down(self, power_of_two: Self) -> ret : Self;
}Expand description
An extension trait for Rust integer types, including u8, u16, u32,
u64, and usize, to provide methods to make integers aligned to a
power of two.
Required Methods§
Sourcefn align_up(self, power_of_two: Self) -> Self
fn align_up(self, power_of_two: Self) -> Self
Returns to the smallest number that is greater than or equal to
self and is a multiple of the given power of two.
The method panics if power_of_two is not a
power of two or is smaller than 2 or the calculation overflows
because self is too large.
§Examples
use crate::align_ext::AlignExt;
assert_eq!(12usize.align_up(2), 12);
assert_eq!(12usize.align_up(4), 12);
assert_eq!(12usize.align_up(8), 16);
assert_eq!(12usize.align_up(16), 16);Sourcefn align_down(self, power_of_two: Self) -> Self
fn align_down(self, power_of_two: Self) -> Self
Returns to the greatest number that is smaller than or equal to
self and is a multiple of the given power of two.
The method panics if power_of_two is not a
power of two or is smaller than 2 or the calculation overflows
because self is too large. In release mode,
§Examples
use crate::align_ext::AlignExt;
assert_eq!(12usize.align_down(2), 12);
assert_eq!(12usize.align_down(4), 12);
assert_eq!(12usize.align_down(8), 8);
assert_eq!(12usize.align_down(16), 0);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 AlignExt for u8
impl AlignExt for u8
§Functional correctness
The implementation meets the specification given in the trait AlignExt.
Source§exec fn align_up(self, align: Self) -> ret : Self
exec fn align_up(self, align: Self) -> ret : Self
exists |e: nat| pow2(e) == align,align >= 2,self + (align - 1) <= u8::MAX,ensuresret >= self,ret % align == 0,ret == nat_align_up(self as nat, align as nat),forall |n: nat| !(n >= self && #[trigger] (n % align as nat) == 0) || (ret <= n),§Preconditions
alignis a power of two.align >= 2.self + (align - 1)does not overflow.
§Postconditions
- The function will not panic.
- The return value is the smallest number that is greater than or equal to
selfand is a multiple ofalign.
Source§exec fn align_down(self, align: Self) -> ret : Self
exec fn align_down(self, align: Self) -> ret : Self
exists |e: nat| pow2(e) == align,align >= 2,ensuresret <= self,ret % align == 0,ret == nat_align_down(self as nat, align as nat),forall |n: nat| !(n <= self && #[trigger] (n % align as nat) == 0) || (ret >= n),§Preconditions
alignis a power of two.align >= 2.
§Postconditions
- The function will not panic.
- The return value is the greatest number that is smaller than or equal to
selfand is a multiple ofalign.
Source§impl AlignExt for u16
impl AlignExt for u16
§Functional correctness
The implementation meets the specification given in the trait AlignExt.
Source§exec fn align_up(self, align: Self) -> ret : Self
exec fn align_up(self, align: Self) -> ret : Self
exists |e: nat| pow2(e) == align,align >= 2,self + (align - 1) <= u16::MAX,ensuresret >= self,ret % align == 0,ret == nat_align_up(self as nat, align as nat),forall |n: nat| !(n >= self && #[trigger] (n % align as nat) == 0) || (ret <= n),§Preconditions
alignis a power of two.align >= 2.self + (align - 1)does not overflow.
§Postconditions
- The function will not panic.
- The return value is the smallest number that is greater than or equal to
selfand is a multiple ofalign.
Source§exec fn align_down(self, align: Self) -> ret : Self
exec fn align_down(self, align: Self) -> ret : Self
exists |e: nat| pow2(e) == align,align >= 2,ensuresret <= self,ret % align == 0,ret == nat_align_down(self as nat, align as nat),forall |n: nat| !(n <= self && #[trigger] (n % align as nat) == 0) || (ret >= n),§Preconditions
alignis a power of two.align >= 2.
§Postconditions
- The function will not panic.
- The return value is the greatest number that is smaller than or equal to
selfand is a multiple ofalign.
Source§impl AlignExt for u32
impl AlignExt for u32
§Functional correctness
The implementation meets the specification given in the trait AlignExt.
Source§exec fn align_up(self, align: Self) -> ret : Self
exec fn align_up(self, align: Self) -> ret : Self
exists |e: nat| pow2(e) == align,align >= 2,self + (align - 1) <= u32::MAX,ensuresret >= self,ret % align == 0,ret == nat_align_up(self as nat, align as nat),forall |n: nat| !(n >= self && #[trigger] (n % align as nat) == 0) || (ret <= n),§Preconditions
alignis a power of two.align >= 2.self + (align - 1)does not overflow.
§Postconditions
- The function will not panic.
- The return value is the smallest number that is greater than or equal to
selfand is a multiple ofalign.
Source§exec fn align_down(self, align: Self) -> ret : Self
exec fn align_down(self, align: Self) -> ret : Self
exists |e: nat| pow2(e) == align,align >= 2,ensuresret <= self,ret % align == 0,ret == nat_align_down(self as nat, align as nat),forall |n: nat| !(n <= self && #[trigger] (n % align as nat) == 0) || (ret >= n),§Preconditions
alignis a power of two.align >= 2.
§Postconditions
- The function will not panic.
- The return value is the greatest number that is smaller than or equal to
selfand is a multiple ofalign.
Source§impl AlignExt for u64
impl AlignExt for u64
§Functional correctness
The implementation meets the specification given in the trait AlignExt.
Source§exec fn align_up(self, align: Self) -> ret : Self
exec fn align_up(self, align: Self) -> ret : Self
exists |e: nat| pow2(e) == align,align >= 2,self + (align - 1) <= u64::MAX,ensuresret >= self,ret % align == 0,ret == nat_align_up(self as nat, align as nat),forall |n: nat| !(n >= self && #[trigger] (n % align as nat) == 0) || (ret <= n),§Preconditions
alignis a power of two.align >= 2.self + (align - 1)does not overflow.
§Postconditions
- The function will not panic.
- The return value is the smallest number that is greater than or equal to
selfand is a multiple ofalign.
Source§exec fn align_down(self, align: Self) -> ret : Self
exec fn align_down(self, align: Self) -> ret : Self
exists |e: nat| pow2(e) == align,align >= 2,ensuresret <= self,ret % align == 0,ret == nat_align_down(self as nat, align as nat),forall |n: nat| !(n <= self && #[trigger] (n % align as nat) == 0) || (ret >= n),§Preconditions
alignis a power of two.align >= 2.
§Postconditions
- The function will not panic.
- The return value is the greatest number that is smaller than or equal to
selfand is a multiple ofalign.
Source§impl AlignExt for usize
impl AlignExt for usize
§Functional correctness
The implementation meets the specification given in the trait AlignExt.
Source§exec fn align_up(self, align: Self) -> ret : Self
exec fn align_up(self, align: Self) -> ret : Self
exists |e: nat| pow2(e) == align,align >= 2,self + (align - 1) <= usize::MAX,ensuresret >= self,ret % align == 0,ret == nat_align_up(self as nat, align as nat),forall |n: nat| !(n >= self && #[trigger] (n % align as nat) == 0) || (ret <= n),§Preconditions
alignis a power of two.align >= 2.self + (align - 1)does not overflow.
§Postconditions
- The function will not panic.
- The return value is the smallest number that is greater than or equal to
selfand is a multiple ofalign.
Source§exec fn align_down(self, align: Self) -> ret : Self
exec fn align_down(self, align: Self) -> ret : Self
exists |e: nat| pow2(e) == align,align >= 2,ensuresret <= self,ret % align == 0,ret == nat_align_down(self as nat, align as nat),forall |n: nat| !(n <= self && #[trigger] (n % align as nat) == 0) || (ret >= n),§Preconditions
alignis a power of two.align >= 2.
§Postconditions
- The function will not panic.
- The return value is the greatest number that is smaller than or equal to
selfand is a multiple ofalign.