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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//! Type to value conversions.

use std::ops::Add;
use typenum::*;

pub trait ToValue<I> {
    fn value() -> I;
}

pub trait TypeBound {
    type Min;
    type Max;
}

// 符号はそのままに絶対値を 1 だけ大きくする
pub trait Outer {
    type Output;
}

type OuterOf<T> = <T as Outer>::Output;

impl<U: Unsigned + NonZero> Outer for PInt<U>
where
    PInt<U>: Add<P1>,
{
    type Output = <PInt<U> as Add<P1>>::Output;
}

impl<U: Unsigned + NonZero> Outer for NInt<U>
where
    NInt<U>: Add<N1>,
{
    type Output = <NInt<U> as Add<N1>>::Output;
}

impl<U: Unsigned, B: Bit> Outer for UInt<U, B>
where
    UInt<U, B>: Add<B1>,
{
    type Output = Add1<UInt<U, B>>;
}

impl TypeBound for i8 {
    type Min = NInt<Exp<U2, U7>>;
    type Max = PInt<Sub1<Exp<U2, U7>>>;
}

impl TypeBound for i16 {
    type Min = NInt<Exp<U2, U15>>;
    type Max = PInt<Sub1<Exp<U2, U15>>>;
}

impl TypeBound for i32 {
    type Min = NInt<Exp<U2, U31>>;
    type Max = PInt<Sub1<Exp<U2, U31>>>;
}

impl TypeBound for i64 {
    type Min = NInt<Exp<U2, U63>>;
    type Max = PInt<Sub1<Exp<U2, U63>>>;
}

#[cfg(target_pointer_width = "16")]
impl TypeBound for isize {
    type Min = <i16 as TypeBound>::Min;
    type Max = <i16 as TypeBound>::Max;
}

#[cfg(target_pointer_width = "32")]
impl TypeBound for isize {
    type Min = <i32 as TypeBound>::Min;
    type Max = <i32 as TypeBound>::Max;
}

#[cfg(target_pointer_width = "64")]
impl TypeBound for isize {
    type Min = <i64 as TypeBound>::Min;
    type Max = <i64 as TypeBound>::Max;
}

macro_rules! impl_to_value_for_integer {
    ( $ToT:ty, $Const:tt ) => {
        impl<I: Integer> ToValue<$ToT> for I
        where
            I: Cmp<OuterOf<<$ToT as TypeBound>::Min>, Output = Greater>
                + Cmp<OuterOf<<$ToT as TypeBound>::Max>, Output = Less>,
        {
            #[inline]
            fn value() -> $ToT {
                Self::$Const
            }
        }
    };
}

impl_to_value_for_integer!(i8, I8);
impl_to_value_for_integer!(i16, I16);
impl_to_value_for_integer!(i32, I32);
impl_to_value_for_integer!(i64, I64);
impl_to_value_for_integer!(isize, ISIZE);

impl TypeBound for u8 {
    type Min = U0;
    type Max = Sub1<Exp<U2, U8>>;
}

impl TypeBound for u16 {
    type Min = U0;
    type Max = Sub1<Exp<U2, U16>>;
}

impl TypeBound for u32 {
    type Min = U0;
    type Max = Sub1<Exp<U2, U32>>;
}

impl TypeBound for u64 {
    type Min = U0;
    type Max = Sub1<Exp<U2, U64>>;
}

#[cfg(target_pointer_width = "16")]
impl TypeBound for usize {
    type Min = U0;
    type Max = <u16 as TypeBound>::Max;
}

#[cfg(target_pointer_width = "32")]
impl TypeBound for usize {
    type Min = U0;
    type Max = <u32 as TypeBound>::Max;
}

#[cfg(target_pointer_width = "64")]
impl TypeBound for usize {
    type Min = U0;
    type Max = <u64 as TypeBound>::Max;
}

macro_rules! impl_to_value_for_unsigned {
    ( $ToT:ty, $Const:tt ) => {
        impl<U: Unsigned> ToValue<$ToT> for U
        where
            U: Cmp<OuterOf<<$ToT as TypeBound>::Max>, Output = Less>,
        {
            #[inline]
            fn value() -> $ToT {
                Self::$Const
            }
        }
    };
}

impl_to_value_for_unsigned!(u8, U8);
impl_to_value_for_unsigned!(u16, U16);
impl_to_value_for_unsigned!(u32, U32);
impl_to_value_for_unsigned!(u64, U64);
impl_to_value_for_unsigned!(usize, USIZE);

#[cfg(test)]
mod tests {
    macro_rules! min_bound_tests {
    ( $pt:ty ) => {
		    #[test]
            fn inner_min() {
                assert_eq!(<$pt>::MIN, <$pt as TypeBound>::Min::value());
            }

		    #[test]
            fn outer_min() {
                use impls::impls;
                assert!(impls!(OuterOf<<$pt as TypeBound>::Min>: !ToValue<$pt>));
            }
	    };
    }

    macro_rules! max_bound_tests {
	    ( $pt:ty ) => {
		    #[test]
            fn inner_max() {
                assert_eq!(<$pt>::MAX, <$pt as TypeBound>::Max::value());
            }

		    #[test]
            fn outer_max() {
                use impls::impls;
                assert!(impls!(OuterOf<<$pt as TypeBound>::Max>: !ToValue<$pt>));
            }
	    };
    }

    macro_rules! zero_test {
        ( $pt:ty ) => {
            #[test]
            fn zero() {
                assert_eq!((0 as $pt), Z0::value());
            }
        };
    }

    mod i8 {
        use super::super::*;
        min_bound_tests!(i8);
        max_bound_tests!(i8);
        zero_test!(i8);
    }

    mod i16 {
        use super::super::*;
        min_bound_tests!(i16);
        max_bound_tests!(i16);
        zero_test!(i16);
    }

    mod i32 {
        use super::super::*;
        min_bound_tests!(i32);
        max_bound_tests!(i32);
        zero_test!(i32);
    }

    mod i64 {
        use super::super::*;
        min_bound_tests!(i64);
        max_bound_tests!(i64);
        zero_test!(i64);
    }

    mod isize {
        use super::super::*;
        min_bound_tests!(isize);
        max_bound_tests!(isize);
        zero_test!(isize);
    }

    mod u8 {
        use super::super::*;
        max_bound_tests!(u8);
    }

    mod u16 {
        use super::super::*;
        max_bound_tests!(u16);
    }

    mod u32 {
        use super::super::*;
        max_bound_tests!(u32);
    }

    mod u64 {
        use super::super::*;
        max_bound_tests!(u64);
    }

    mod usize {
        use super::super::*;
        max_bound_tests!(usize);
    }
}