Trait AlignExt

Source
pub trait AlignExt {
    // Required methods
    fn is_power_of_two(&self) -> bool;
    fn align_up(self, power_of_two: Self) -> Self;
    fn align_down(self, power_of_two: Self) -> 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§

Source

fn is_power_of_two(&self) -> bool

Returns whether the number is a power of two

Source

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

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

Source§

fn is_power_of_two(&self) -> bool

Source§

fn align_up(self, align: Self) -> Self

Source§

fn align_down(self, align: Self) -> Self

Source§

impl AlignExt for u16

Source§

fn is_power_of_two(&self) -> bool

Source§

fn align_up(self, align: Self) -> Self

Source§

fn align_down(self, align: Self) -> Self

Source§

impl AlignExt for u32

Source§

fn is_power_of_two(&self) -> bool

Source§

fn align_up(self, align: Self) -> Self

Source§

fn align_down(self, align: Self) -> Self

Source§

impl AlignExt for u64

Source§

fn is_power_of_two(&self) -> bool

Source§

fn align_up(self, align: Self) -> Self

Source§

fn align_down(self, align: Self) -> Self

Source§

impl AlignExt for usize

Source§

fn is_power_of_two(&self) -> bool

Source§

fn align_up(self, align: Self) -> Self

Source§

fn align_down(self, align: Self) -> Self

Implementors§