pub trait AsBool {
fn as_bool(&self) -> bool;
}
impl AsBool for bool {
fn as_bool(&self) -> bool {
*self
}
}
impl AsBool for i32 {
fn as_bool(&self) -> bool {
*self != 0
}
}
pub trait AsUsize {
fn as_usize(&self) -> usize;
}
macro_rules! impl_as_usize {
($type:ty) => {
impl AsUsize for $type {
fn as_usize(&self) -> usize {
*self as usize
}
}
};
}
impl_as_usize!(u8);
impl_as_usize!(u16);
impl_as_usize!(u32);
impl_as_usize!(usize);
pub trait IsInt {
fn is_int() -> bool;
}
impl IsInt for f32 {
fn is_int() -> bool {
false
}
}
impl IsInt for i32 {
fn is_int() -> bool {
true
}
}
pub trait Identities {
fn one() -> Self;
fn zero() -> Self;
}
macro_rules! impl_float_identities {
($type:ty) => {
impl Identities for $type {
fn one() -> Self {
1.
}
fn zero() -> Self {
0.
}
}
};
}
macro_rules! impl_int_identities {
($type:ty) => {
impl Identities for $type {
fn one() -> Self {
1
}
fn zero() -> Self {
0
}
}
};
}
impl_float_identities!(f32);
impl_int_identities!(isize);
impl_int_identities!(i32);
impl_int_identities!(i8);
impl_int_identities!(u8);
pub trait IsNaN {
#[allow(clippy::wrong_self_convention)] fn is_nan(self) -> bool;
}
macro_rules! impl_isnan_float {
($type:ty) => {
impl IsNaN for $type {
fn is_nan(self) -> bool {
<$type>::is_nan(self)
}
}
};
}
macro_rules! impl_isnan_int {
($type:ty) => {
impl IsNaN for $type {
fn is_nan(self) -> bool {
false
}
}
};
}
impl_isnan_float!(f32);
impl_isnan_int!(i32);
impl_isnan_int!(i8);
impl_isnan_int!(u8);
pub trait LeBytes {
type Bytes: AsRef<[u8]> + for<'a> TryFrom<&'a [u8], Error = std::array::TryFromSliceError>;
fn from_le_bytes(bytes: Self::Bytes) -> Self;
fn to_le_bytes(self) -> Self::Bytes;
}
macro_rules! impl_le_bytes {
($type:ty, $size:literal) => {
impl LeBytes for $type {
type Bytes = [u8; $size];
fn from_le_bytes(bytes: Self::Bytes) -> Self {
<$type>::from_le_bytes(bytes)
}
fn to_le_bytes(self) -> Self::Bytes {
<$type>::to_le_bytes(self)
}
}
};
}
impl_le_bytes!(i8, 1);
impl_le_bytes!(u8, 1);
impl_le_bytes!(i32, 4);
impl_le_bytes!(f32, 4);
impl_le_bytes!(u32, 4);
impl_le_bytes!(i64, 8);
impl_le_bytes!(u64, 8);
pub trait MinMax {
#[allow(unused)] fn max_val() -> Self;
fn min_val() -> Self;
#[allow(unused)] fn min(self, other: Self) -> Self;
fn max(self, other: Self) -> Self;
}
impl MinMax for f32 {
#[inline]
fn max_val() -> Self {
f32::INFINITY
}
#[inline]
fn min_val() -> Self {
f32::NEG_INFINITY
}
#[inline]
fn max(self, other: f32) -> f32 {
self.max(other)
}
#[inline]
fn min(self, other: f32) -> f32 {
self.min(other)
}
}
macro_rules! impl_minmax_int {
($type:ty) => {
impl MinMax for $type {
#[inline]
fn max_val() -> Self {
Self::MAX
}
#[inline]
fn min_val() -> Self {
Self::MIN
}
#[inline]
fn max(self, other: Self) -> Self {
Ord::max(self, other)
}
#[inline]
fn min(self, other: Self) -> Self {
Ord::min(self, other)
}
}
};
}
impl_minmax_int!(i32);
pub fn div_ceil(x: isize, y: isize) -> isize {
let d = x / y;
let r = x % y;
if r != 0 && x.signum() == y.signum() {
d + 1
} else {
d
}
}
pub trait Cast<T> {
fn cast(self) -> T;
}
macro_rules! impl_cast {
($src:ty, $dest:ty) => {
impl Cast<$dest> for $src {
fn cast(self) -> $dest {
self as $dest
}
}
};
}
impl_cast!(i32, f32);
impl_cast!(f32, i32);
impl_cast!(i8, u8);
impl_cast!(u8, i8);
#[cfg(test)]
mod tests {
use super::div_ceil;
#[test]
fn test_div_ceil() {
assert_eq!(div_ceil(10, 5), 2);
assert_eq!(div_ceil(11, 5), 3);
assert_eq!(div_ceil(10, -5), -2);
assert_eq!(div_ceil(11, -5), -2);
}
}