pub trait PcgOps {
fn wrap_mul(&self, rhs: Self) -> Self;
fn wrap_add(&self, rhs: Self) -> Self;
}
pub trait AsUsize {
fn as_usize(&self) -> usize;
}
pub trait BitSize {
const BITS: usize;
}
pub trait AsSmaller<T> {
fn shrink(self) -> T;
}
macro_rules! basic_ops {
( $( $t:ty, $bits:expr);*) => {
$(impl BitSize for $t {
const BITS: usize = $bits;
}
impl AsUsize for $t {
#[inline]
fn as_usize(&self) -> usize {
*self as usize
}
}
impl PcgOps for $t {
#[inline]
fn wrap_mul(&self, rhs : $t) -> $t {
self.wrapping_mul(rhs)
}
#[inline]
fn wrap_add(&self, rhs : $t) -> $t {
self.wrapping_add(rhs)
}
}
)*
}
}
basic_ops!(
u8, 8;
u16, 16;
u32, 32;
u64, 64;
u128, 128
);
macro_rules! smaller {
( $( $t:ty, $other:ty);*) => {
$(
impl AsSmaller<$other> for $t {
#[inline]
fn shrink(self) -> $other {
self as $other
}
}
)*
}
}
smaller!(
u128, u128;
u128, u64;
u128, u32;
u128, u16;
u128, u8;
u64, u64;
u64, u32;
u64, u16;
u64, u8;
u32, u32;
u32, u16;
u32, u8;
u16, u16;
u16, u8
);