use core::mem;
use core::ptr::NonNull;
macro_rules! impl_iter {
($map_fn:expr, $iter_name:ident < $( $life:lifetime, )? $( $arg_ty:ident ),* > => $item_ty:ty ) => {
impl<$($life,)? $($arg_ty),*> core::iter::Iterator for $iter_name<$($life,)? $($arg_ty),*> {
type Item = $item_ty;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map($map_fn)
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.iter.size_hint()
}
#[inline]
fn count(self) -> usize {
self.iter.count()
}
#[inline]
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth(n).map($map_fn)
}
#[inline]
fn last(self) -> Option<Self::Item>
where
Self: Sized,
{
self.iter.last().map($map_fn)
}
}
};
}
pub(crate) use impl_iter;
macro_rules! impl_deiter {
($map_fn:expr, $iter_name:ident < $( $life:lifetime, )? $( $arg_ty:ident ),* > ) => {
impl<$($life,)? $($arg_ty),*> core::iter::DoubleEndedIterator for $iter_name<$($life,)? $($arg_ty),*> {
#[inline]
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map($map_fn)
}
#[inline]
fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth(n).map($map_fn)
}
}
};
}
pub(crate) use impl_deiter;
pub(crate) trait TypeProps<T> {
const SIZE: usize = mem::size_of::<T>();
const ALIGN: usize = mem::align_of::<T>();
const IS_ZST: bool = Self::SIZE == 0;
}
impl<T> TypeProps<T> for T {}
pub(crate) const fn max(a: usize, b: usize) -> usize {
if a > b { a } else { b }
}
pub(crate) const fn is_aligned_to(value: usize, alignment: usize) -> bool {
value.is_multiple_of(alignment)
}
pub(crate) fn ptr_is_aligned_to<T>(ptr: *mut T, alignment: usize) -> bool {
let value = ptr as usize;
value.is_multiple_of(alignment)
}
pub(crate) const fn round_down_to(n: usize, divisor: usize) -> usize {
debug_assert!(divisor.is_power_of_two());
n & !(divisor - 1)
}
pub(crate) fn round_mut_ptr_down_to<T>(ptr: *mut T, alignment: usize) -> *mut T {
debug_assert!(alignment.is_power_of_two());
let ptr_int = ptr as usize;
let new_ptr_int = round_down_to(ptr_int, alignment);
debug_assert!(ptr_int >= new_ptr_int);
let delta = ptr_int - new_ptr_int;
debug_assert!(delta < alignment);
ptr.wrapping_byte_sub(delta)
}
pub(crate) const fn round_down_to_pow2(n: usize) -> usize {
debug_assert!(n > 0);
let lead_zeros = n.leading_zeros();
let mask = usize::MAX << (usize::BITS - lead_zeros - 1);
n & mask
}
pub(crate) const fn zst_ptr<T>() -> NonNull<T> {
assert!(T::IS_ZST);
let non_zero = core::num::NonZero::new(T::ALIGN).unwrap();
NonNull::without_provenance(non_zero)
}
#[cfg(test)]
mod utest {
use super::*;
use pretty_assertions::assert_eq;
#[test]
fn max_min() {
assert_eq!(max(1, 2), 2);
assert_eq!(max(2, 1), 2);
}
#[test]
fn round_down_to_n() {
assert_eq!(round_down_to(0, 1), 0);
assert_eq!(round_down_to(0, 2), 0);
assert_eq!(round_down_to(0, 0x8000_0000_0000_0000), 0);
assert_eq!(round_down_to(3, 1), 3);
assert_eq!(round_down_to(3, 2), 2);
assert_eq!(round_down_to(3, 0x8000_0000_0000_0000), 0);
assert_eq!(round_down_to(7, 1), 7);
assert_eq!(round_down_to(7, 2), 6);
assert_eq!(round_down_to(7, 4), 4);
assert_eq!(round_down_to(7, 0x8000_0000_0000_0000), 0);
assert_eq!(round_down_to(1, 1), 1);
}
#[test]
#[cfg_attr(debug_assertions, should_panic)]
fn round_down_to_n_panics_1() {
assert_eq!(round_down_to(1, 0), 0);
}
#[test]
#[cfg_attr(debug_assertions, should_panic)]
fn round_down_to_n_panics_2() {
assert_eq!(round_down_to(usize::MAX, 0), 0);
}
#[test]
fn round_down_to_pow_2() {
assert_eq!(round_down_to_pow2(1), 1);
assert_eq!(round_down_to_pow2(2), 2);
assert_eq!(round_down_to_pow2(3), 2);
assert_eq!(round_down_to_pow2(4), 4);
assert_eq!(round_down_to_pow2(5), 4);
assert_eq!(round_down_to_pow2(6), 4);
assert_eq!(round_down_to_pow2(7), 4);
assert_eq!(round_down_to_pow2(8), 8);
assert_eq!(
round_down_to_pow2(isize::MAX as usize),
0x4000_0000_0000_0000
);
assert_eq!(
round_down_to_pow2(isize::MAX as usize + 1),
0x8000_0000_0000_0000
);
assert_eq!(round_down_to_pow2(usize::MAX - 1), 0x8000_0000_0000_0000);
assert_eq!(round_down_to_pow2(usize::MAX), 0x8000_0000_0000_0000);
}
#[test]
#[cfg_attr(debug_assertions, should_panic)]
fn round_down_to_pow_2_panics() {
assert_eq!(round_down_to_pow2(0), 0);
}
}