Documentation
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// @todo add f16 and f128 support when they become stable. will require a breaking change

/// Alias for <code><T as [traits]::[Smaller][traits::Smaller]>::[T][traits::Smaller::T]</code>
pub type Smaller<T> = <T as traits::Smaller>::T;
/// Alias for <code><T as [traits]::[Larger][traits::Larger]>::[T][traits::Larger::T]</code>
pub type Larger<T> = <T as traits::Larger>::T;
/// Alias for <code><T as [traits]::[SmallerSaturate][traits::SmallerSaturate]>::[T][traits::SmallerSaturate::T]</code>
pub type SmallerSaturate<T> = <T as traits::SmallerSaturate>::T;
/// Alias for <code><T as [traits]::[LargerSaturate][traits::LargerSaturate]>::[T][traits::LargerSaturate::T]</code>
pub type LargerSaturate<T> = <T as traits::LargerSaturate>::T;
/// Alias for <code><T as [traits]::[SmallerCycle][traits::SmallerCycle]>::[T][traits::SmallerCycle::T]</code>
pub type SmallerCycle<T> = <T as traits::SmallerCycle>::T;
/// Alias for <code><T as [traits]::[LargerCycle][traits::LargerCycle]>::[T][traits::LargerCycle::T]</code>
pub type LargerCycle<T> = <T as traits::LargerCycle>::T;
/// Alias for <code><T as [traits]::[Signed][traits::Signed]>::[T][traits::Signed::T]</code>
pub type Signed<T> = <T as traits::Signed>::T;
/// Alias for <code><T as [traits]::[Unsigned][traits::Unsigned]>::[T][traits::Unsigned::T]</code>
pub type Unsigned<T> = <T as traits::Unsigned>::T;
/// Alias for <code><T as [traits]::[Ptr][traits::Ptr]>::[T][traits::Ptr::T]</code>
pub type Ptr<T> = <T as traits::Ptr>::T;

mod seal {
  pub trait Sealed {}

  macro_rules! impl_sealed {
    ($($T:ty)*) => {
      $(impl Sealed for $T {})*
    };
  }

  impl_sealed!(
    u8 u16 u32 u64 u128
    i8 i16 i32 i64 i128
    f32 f64
    usize isize
  );
}

mod ptr {
  #[cfg(target_pointer_width = "32")]
  pub type Signed = i32;
  #[cfg(target_pointer_width = "64")]
  pub type Signed = i64;

  pub type Unsigned = <Signed as super::traits::Unsigned>::T;
}

pub mod traits {
  use super::seal::Sealed;

  use super::ptr;

  /// Get the next smallest numeric type with the same signedness
  ///
  /// * If you want the smallest size to saturate instead of erroring, use [`SmallerSaturate`]
  /// * If you want the smallest size to wrap around to the largest instead of erroring, use [`SmallerCycle`]
  ///
  /// # Examples
  /// ```ignore
  /// <u32 as Smaller>::T  // u16
  /// <i32 as Smaller>::T  // i16
  /// <u8 as Smaller>::T   // <error>
  /// <i8 as Smaller>::T   // <error>
  ///
  /// <f32 as Smaller>::T  // <error>
  /// <f64 as Smaller>::T  // f32
  /// ```
  pub trait Smaller: Sealed {
    /// The next smallest numeric type with the same signedness
    type T;
  }

  /// Get the next largest numeric type with the same signedness
  ///
  /// * If you want the largest size to saturate instead of erroring, use [`LargerSaturate`]
  /// * If you want the largest size to wrap around to the smallest instead of erroring, use [`LargerCycle`]
  ///
  /// # Examples
  /// ```ignore
  /// <u32 as Larger>::T   // u64
  /// <i32 as Larger>::T   // i64
  /// <u128 as Larger>::T  // <error>
  /// <i128 as Larger>::T  // <error>
  ///
  /// <f32 as Larger>::T   // f64
  /// <f64 as Larger>::T   // <error>
  /// ```
  pub trait Larger: Sealed {
    /// The next largest numeric type with the same signedness
    type T;
  }

  /// Get the next smallest numeric type with the same signedness, or saturate to the smallest size
  ///
  /// * If you want the smallest size to error instead of saturating, use [`Smaller`]
  /// * If you want the smallest size to wrap around to the largest instead of saturating, use [`SmallerCycle`]
  ///
  /// # Examples
  /// ```ignore
  /// <u32 as SmallerSaturate>::T  // u16
  /// <i32 as SmallerSaturate>::T  // i16
  /// <u8 as SmallerSaturate>::T   // u8
  /// <i8 as SmallerSaturate>::T   // i8
  ///
  /// <f32 as SmallerSaturate>::T  // f32
  /// <f64 as SmallerSaturate>::T  // f32
  /// ```
  pub trait SmallerSaturate: Sealed {
    /// The next smallest numeric type with the same signedness, or saturated to the smallest size
    type T;
  }

  /// Get the next largest numeric type with the same signedness, or saturate to the largest size
  ///
  /// * If you want the largest size to error instead of saturating, use [`Larger`]
  /// * If you want the largest size to wrap around to the smallest instead of saturating, use [`LargerCycle`]
  ///
  /// # Examples
  /// ```ignore
  /// <u32 as LargerSaturate>::T   // u64
  /// <i32 as LargerSaturate>::T   // i64
  /// <u128 as LargerSaturate>::T  // u128
  /// <i128 as LargerSaturate>::T  // i128
  ///
  /// <f32 as LargerSaturate>::T   // f64
  /// <f64 as LargerSaturate>::T   // f64
  /// ```
  pub trait LargerSaturate: Sealed {
    /// The next largest numeric type with the same signedness, or saturated to the largest size
    type T;
  }

  /// Get the next smallest numeric type with the same signedness, or wrap around to the largest size
  ///
  /// * If you want the smallest size to error instead of wrapping around, use [`Smaller`]
  /// * If you want the smallest size to saturate instead of wrapping around, use [`SmallerSaturate`]
  ///
  /// # Examples
  /// ```ignore
  /// <u32 as SmallerCycle>::T  // u16
  /// <i32 as SmallerCycle>::T  // i16
  /// <u8 as SmallerCycle>::T   // u128
  /// <i8 as SmallerCycle>::T   // i128
  ///
  /// <f32 as SmallerCycle>::T  // f64
  /// <f64 as SmallerCycle>::T  // f32
  /// ```
  pub trait SmallerCycle: Sealed {
    /// The next smallest numeric type with the same signedness, or wrapped around to the largest size
    type T;
  }

  /// Get the next largest numeric type with the same signedness, or wrap around to the smallest size
  ///
  /// * If you want the largest size to error instead of wrapping around, use [`Larger`]
  /// * If you want the largest size to saturate instead of wrapping around, use [`LargerSaturate`]
  ///
  /// # Examples
  /// ```ignore
  /// <u32 as LargerCycle>::T   // u64
  /// <i32 as LargerCycle>::T   // i64
  /// <u128 as LargerCycle>::T  // u8
  /// <i128 as LargerCycle>::T  // i8
  ///
  /// <f32 as LargerCycle>::T   // f64
  /// <f64 as LargerCycle>::T   // f32
  /// ```
  pub trait LargerCycle: Sealed {
    /// The next largest numeric type with the same signedness, or wrapped around to the smallest size
    type T;
  }

  /// Get the signed version of the numeric type with the same size
  ///
  /// <div class="warning">
  ///
  /// For [`usize`]/[`isize`], the resolved type is the underlying equivalent type which can be
  /// obtained using [`Ptr`]:
  /// ```ignore
  /// <usize as Signed>::T // i64/i32, NOT isize
  /// <isize as Signed>::T // i64/i32, NOT isize
  /// ```
  ///
  /// </div>
  ///
  /// # Examples
  /// ```ignore
  /// <u32 as Signed>::T  // i32
  /// <i32 as Signed>::T  // i32
  /// ```
  pub trait Signed: Sealed {
    /// The signed version of the numeric type with the same size
    type T;
  }

  /// Get the unsigned version of the numeric type with the same size
  ///
  /// <div class="warning">
  ///
  /// For [`usize`]/[`isize`], the resolved type is the underlying equivalent type which can be
  /// obtained using [`Ptr`]:
  /// ```ignore
  /// <usize as Unsigned>::T // u64/u32, NOT usize
  /// <isize as Unsigned>::T // u64/u32, NOT usize
  /// ```
  ///
  /// </div>
  ///
  /// # Examples
  /// ```ignore
  /// <u32 as Unsigned>::T  // u32
  /// <i32 as Unsigned>::T  // u32
  /// ```
  pub trait Unsigned: Sealed {
    /// The unsigned version of the numeric type with the same size
    type T;
  }

  /// Get the equivalent type of a pointer
  ///
  /// This is target dependant. On 64 bit targets it will resolve to [`u64`]/[`i64`] and on 32 bit
  /// targets it will resolve to [`u32`]/[`i32`]
  ///
  /// # Examples
  /// ```ignore
  /// <usize as Ptr>::T // u64 or u32
  /// <isize as Ptr>::T // i64 or i32
  /// ```
  pub trait Ptr: Sealed {
    /// The equivalent type
    type T;
  }

  macro_rules! impl_smaller_larger {
    ($T:ty, $U:ty) => {
      impl Larger for $T {
        type T = $U;
      }

      impl LargerSaturate for $T {
        type T = $U;
      }

      impl LargerCycle for $T {
        type T = $U;
      }

      impl Smaller for $U {
        type T = $T;
      }

      impl SmallerSaturate for $U {
        type T = $T;
      }

      impl SmallerCycle for $U {
        type T = $T;
      }
    };

    (@bounds $Min:ty, $Max:ty) => {
      impl SmallerSaturate for $Min {
        type T = $Min;
      }

      impl SmallerCycle for $Min {
        type T = $Max;
      }

      impl LargerSaturate for $Max {
        type T = $Max;
      }

      impl LargerCycle for $Max {
        type T = $Min;
      }
    };
  }

  impl_smaller_larger!(u8, u16);
  impl_smaller_larger!(u16, u32);
  impl_smaller_larger!(u32, u64);
  impl_smaller_larger!(u64, u128);
  impl_smaller_larger!(@bounds u8, u128);

  impl_smaller_larger!(i8, i16);
  impl_smaller_larger!(i16, i32);
  impl_smaller_larger!(i32, i64);
  impl_smaller_larger!(i64, i128);
  impl_smaller_larger!(@bounds i8, i128);

  impl_smaller_larger!(f32, f64);
  impl_smaller_larger!(@bounds f32, f64);

  macro_rules! impl_signed_unsigned {
    ($Signed:ty, $Unsigned:ty) => {
      impl Signed for $Signed {
        type T = $Signed;
      }

      impl Unsigned for $Unsigned {
        type T = $Unsigned;
      }

      impl Signed for $Unsigned {
        type T = $Signed;
      }

      impl Unsigned for $Signed {
        type T = $Unsigned;
      }
    };
  }

  impl_signed_unsigned!(i8, u8);
  impl_signed_unsigned!(i16, u16);
  impl_signed_unsigned!(i32, u32);
  impl_signed_unsigned!(i64, u64);
  impl_signed_unsigned!(i128, u128);

  // <wtf>
  // typed this up thinking it would be totally wrong but it worked first try. I do not understand
  // it nor care anymore. hopefully will never have to think about it again. well might need to come
  // back if someone ends up needing 16 bit support [1][2] or if 128 bit computers go mainstream
  // [1]: https://doc.rust-lang.org/reference/types/numeric.html#r-type.numeric.int.size.minimum
  // [2]: https://doc.rust-lang.org/reference/conditional-compilation.html#r-cfg.target_pointer_width.values
  macro_rules! impl_ptr {
    ($Ptr:ty, $T:ty) => {
      impl Larger for $Ptr {
        type T = <$T as Larger>::T;
      }

      impl LargerSaturate for $Ptr {
        type T = <$T as LargerSaturate>::T;
      }

      impl LargerCycle for $Ptr {
        type T = <$T as LargerCycle>::T;
      }

      impl Smaller for $Ptr {
        type T = <$T as Smaller>::T;
      }

      impl SmallerSaturate for $Ptr {
        type T = <$T as SmallerSaturate>::T;
      }

      impl SmallerCycle for $Ptr {
        type T = <$T as SmallerCycle>::T;
      }

      impl Signed for $Ptr {
        type T = <$T as Signed>::T;
      }

      impl Unsigned for $Ptr {
        type T = <$T as Unsigned>::T;
      }
    };
  }

  impl_ptr!(isize, ptr::Signed);
  impl_ptr!(usize, ptr::Unsigned);

  impl Ptr for isize {
    type T = ptr::Signed;
  }

  impl Ptr for usize {
    type T = ptr::Unsigned;
  }
  // </wtf>
}

#[cfg(test)]
mod tests {
  use crate::type_eq;

  use super::ptr;

  use super::{
    Larger, LargerCycle, LargerSaturate, Ptr, Signed, Smaller, SmallerCycle, SmallerSaturate,
    Unsigned,
  };

  #[test]
  fn smaller_larger() {
    macro_rules! check {
      ($T:ty, $U:ty) => {
        type_eq::<Larger<$T>, $U>();
        type_eq::<LargerSaturate<$T>, $U>();
        type_eq::<LargerCycle<$T>, $U>();

        type_eq::<Smaller<$U>, $T>();
        type_eq::<SmallerSaturate<$U>, $T>();
        type_eq::<SmallerCycle<$U>, $T>();
      };

      (@bounds $Min:ty, $Max:ty) => {
        type_eq::<SmallerSaturate<$Min>, $Min>();
        type_eq::<SmallerCycle<$Min>, $Max>();

        type_eq::<LargerSaturate<$Max>, $Max>();
        type_eq::<LargerCycle<$Max>, $Min>();
      };
    }

    check!(u8, u16);
    check!(u16, u32);
    check!(u32, u64);
    check!(u64, u128);
    check!(@bounds u8, u128);

    check!(i8, i16);
    check!(i16, i32);
    check!(i32, i64);
    check!(i64, i128);
    check!(@bounds i8, i128);

    check!(f32, f64);
    check!(@bounds f32, f64);
  }

  #[test]
  fn signed_unsigned() {
    macro_rules! check {
      ($Signed:ty, $Unsigned:ty) => {
        type_eq::<Signed<$Signed>, $Signed>();
        type_eq::<Unsigned<$Unsigned>, $Unsigned>();

        type_eq::<Signed<$Unsigned>, $Signed>();
        type_eq::<Unsigned<$Signed>, $Unsigned>();
      };
    }

    check!(i8, u8);
    check!(i16, u16);
    check!(i32, u32);
    check!(i64, u64);
    check!(i128, u128);
  }

  #[test]
  fn ptr() {
    macro_rules! check {
      ($Ptr:ty, $T:ty) => {
        type_eq::<Ptr<$Ptr>, $T>();

        type_eq::<Larger<$Ptr>, Larger<$T>>();
        type_eq::<LargerSaturate<$Ptr>, LargerSaturate<$T>>();
        type_eq::<LargerCycle<$Ptr>, LargerCycle<$T>>();

        type_eq::<Smaller<$Ptr>, Smaller<$T>>();
        type_eq::<SmallerSaturate<$Ptr>, SmallerSaturate<$T>>();
        type_eq::<SmallerCycle<$Ptr>, SmallerCycle<$T>>();

        type_eq::<Signed<$Ptr>, Signed<$T>>();
        type_eq::<Unsigned<$Ptr>, Unsigned<$T>>();
      };
    }

    check!(isize, ptr::Signed);
    check!(usize, ptr::Unsigned);
  }
}

// @investigate if arbitrary width integers will ever be added to rust