#![allow(unused_comparisons)]
use core::ops::{Range, RangeTo};
pub trait BoundedRange<T> {
fn to_range(self) -> Range<T>;
}
impl<T> BoundedRange<T> for Range<T> {
fn to_range(self) -> Range<T> { self }
}
pub trait Int : Copy {
fn negative(self) -> bool;
fn cast_i64(self) -> i64;
fn div_rem(self, other: Self) -> (Self, Self) where Self: Sized;
}
macro_rules! int_impl {
($name:ident) => {
impl Int for $name {
fn negative(self) -> bool { self < 0 }
fn cast_i64(self) -> i64 { self as i64 }
fn div_rem(self, other: Self) -> (Self, Self) { (self / other, self % other) }
}
}
}
int_impl!(u8);
int_impl!(u16);
int_impl!(u32);
int_impl!(u64);
int_impl!(u128);
int_impl!(usize);
int_impl!(i8);
int_impl!(i16);
int_impl!(i32);
int_impl!(i64);
int_impl!(i128);
int_impl!(isize);
pub trait UnsignedInt : Int+Sized {
fn next_power_of_two(&self) -> Self;
fn checked_next_power_of_two(&self) -> Option<Self>;
}
macro_rules! uint_impl {
($name:ident) => {
impl UnsignedInt for $name {
fn next_power_of_two(&self) -> $name {
$name::next_power_of_two(*self)
}
fn checked_next_power_of_two(&self) -> Option<$name> {
$name::checked_next_power_of_two(*self)
}
}
impl BoundedRange<$name> for RangeTo<$name> {
fn to_range(self) -> Range<$name> {
Range { start: 0, end: self.end }
}
}
}
}
uint_impl!(u8);
uint_impl!(u16);
uint_impl!(u32);
uint_impl!(u64);
uint_impl!(u128);
uint_impl!(usize);
pub trait SignedInt : Int {
}
macro_rules! sint_impl {
($name:ident) => {
impl SignedInt for $name {
}
}
}
sint_impl!(i8);
sint_impl!(i16);
sint_impl!(i32);
sint_impl!(i64);
sint_impl!(i128);
sint_impl!(isize);