#![no_std]
#![cfg_attr(test, recursion_limit = "256")]
#![forbid(unsafe_code)]
#![warn(clippy::pedantic)]
#![allow(clippy::default_trait_access)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::must_use_candidate)]
use num_traits::{NumAssign, PrimInt};
use typenum::Unsigned;
#[cfg(test)]
mod tests;
mod detail {
use super::SmallestUIntFor;
use core::ops::Sub;
use num_traits::{NumAssign, PrimInt};
use typenum::{Len, Max, Unsigned};
use typenum::{Length, Maximum, Sub1};
use typenum::{B1, U1};
type Max1<N> = Maximum<N, U1>;
pub trait GetSmallestUIntFor: Unsigned {
type Type: Default + PrimInt + NumAssign;
}
impl<N> GetSmallestUIntFor for N
where
N: Unsigned + Max<U1>,
Max1<N>: Len,
Length<Max1<N>>: Sub<B1>,
Sub1<Length<Max1<N>>>: Len,
Length<Sub1<Length<Max1<N>>>>: Get,
{
type Type = <Length<Sub1<Length<Max1<N>>>> as Get>::Type;
}
pub trait GetSmallestUIntUpTo: Unsigned {
type Type: Default + PrimInt + NumAssign;
}
impl<N> GetSmallestUIntUpTo for N
where
N: Unsigned + Max<U1>,
Max1<N>: Sub<B1>,
Sub1<Max1<N>>: super::GetSmallestUIntFor,
{
type Type = SmallestUIntFor<Sub1<Max1<N>>>;
}
pub trait Get {
type Type: Default + PrimInt + NumAssign;
}
impl Get for typenum::U0 {
type Type = u8;
}
impl Get for typenum::U1 {
type Type = u8;
}
impl Get for typenum::U2 {
type Type = u8;
}
impl Get for typenum::U3 {
type Type = u8;
}
impl Get for typenum::U4 {
type Type = u16;
}
impl Get for typenum::U5 {
type Type = u32;
}
impl Get for typenum::U6 {
type Type = u64;
}
#[cfg(feature = "u128")]
impl Get for typenum::U7 {
type Type = u128;
}
}
pub trait GetSmallestUIntFor: Unsigned + detail::GetSmallestUIntFor {
type Type: Default + PrimInt + NumAssign;
}
pub type SmallestUIntFor<N> = <N as GetSmallestUIntFor>::Type;
impl<N> GetSmallestUIntFor for N
where
N: detail::GetSmallestUIntFor,
{
type Type = <N as detail::GetSmallestUIntFor>::Type;
}
pub trait GetSmallestUIntUpTo: Unsigned + detail::GetSmallestUIntUpTo {
type Type: Default + PrimInt + NumAssign;
}
pub type SmallestUIntUpTo<N> = <N as GetSmallestUIntUpTo>::Type;
impl<N> GetSmallestUIntUpTo for N
where
N: detail::GetSmallestUIntUpTo,
{
type Type = <N as detail::GetSmallestUIntUpTo>::Type;
}