1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#![no_std]
#![cfg_attr(test, recursion_limit = "256")]
#![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;
}