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
//! Author --- daniel.bechaz@gmail.com  
//! Last Moddified --- 2019-03-30

use std::{num::*, ops::*, mem,};

/// A marker trait for types which have a [NonZero] equivilant.
pub trait Number: Add + AddAssign
  + Sub + SubAssign
  + Mul + MulAssign
  + Div + DivAssign
  + Eq + Ord
  + Copy + Sized {
  /// The [NonZero] equivilant.
  type NonZero: NonZero<Self>;

  /// The zero value of this type.
  const ZERO: Self;
}

impl Number for usize {
  type NonZero = NonZeroUsize;

  const ZERO: Self = 0;
}

impl Number for u8 {
  type NonZero = NonZeroU8;

  const ZERO: Self = 0;
}

impl Number for u16 {
  type NonZero = NonZeroU16;

  const ZERO: Self = 0;
}

impl Number for u32 {
  type NonZero = NonZeroU32;

  const ZERO: Self = 0;
}

impl Number for u64 {
  type NonZero = NonZeroU64;

  const ZERO: Self = 0;
}

impl Number for u128 {
  type NonZero = NonZeroU128;

  const ZERO: Self = 0;
}

impl Number for isize {
  type NonZero = NonZeroUsize;

  const ZERO: Self = 0;
}

impl Number for i8 {
  type NonZero = NonZeroU8;

  const ZERO: Self = 0;
}

impl Number for i16 {
  type NonZero = NonZeroU16;

  const ZERO: Self = 0;
}

impl Number for i32 {
  type NonZero = NonZeroU32;

  const ZERO: Self = 0;
}

impl Number for i64 {
  type NonZero = NonZeroU64;

  const ZERO: Self = 0;
}

impl Number for i128 {
  type NonZero = NonZeroU128;

  const ZERO: Self = 0;
}

/// A marker trait for types which have are `NonZero` numbers.
pub trait NonZero<Num,>: Eq + Ord + Copy + Sized
  where Num: Number, {
  /// Constructs a new value returning `None` for `0`.
  fn new(num: Num,) -> Option<Self>;
  /// Gets the inner value.
  fn get(self,) -> Num;
}

impl NonZero<usize> for NonZeroUsize {
  #[inline]
  fn new(num: usize,) -> Option<Self> { Self::new(num,) }
  #[inline]
  fn get(self,) -> usize { Self::get(self,) }
}

impl NonZero<u8> for NonZeroU8 {
  #[inline]
  fn new(num: u8,) -> Option<Self> { Self::new(num,) }
  #[inline]
  fn get(self,) -> u8 { Self::get(self,) }
}

impl NonZero<u16> for NonZeroU16 {
  #[inline]
  fn new(num: u16,) -> Option<Self> { Self::new(num,) }
  #[inline]
  fn get(self,) -> u16 { Self::get(self,) }
}

impl NonZero<u32> for NonZeroU32 {
  #[inline]
  fn new(num: u32,) -> Option<Self> { Self::new(num,) }
  #[inline]
  fn get(self,) -> u32 { Self::get(self,) }
}

impl NonZero<u64> for NonZeroU64 {
  #[inline]
  fn new(num: u64,) -> Option<Self> { Self::new(num,) }
  #[inline]
  fn get(self,) -> u64 { Self::get(self,) }
}

impl NonZero<u128> for NonZeroU128 {
  #[inline]
  fn new(num: u128,) -> Option<Self> { Self::new(num,) }
  #[inline]
  fn get(self,) -> u128 { Self::get(self,) }
}

impl NonZero<isize> for NonZeroUsize {
  #[inline]
  fn new(num: isize,) -> Option<Self> { Self::new(unsafe { mem::transmute(num,) },) }
  #[inline]
  fn get(self,) -> isize { unsafe { mem::transmute(Self::get(self,),) } }
}

impl NonZero<i8> for NonZeroU8 {
  #[inline]
  fn new(num: i8,) -> Option<Self> { Self::new(unsafe { mem::transmute(num,) },) }
  #[inline]
  fn get(self,) -> i8 { unsafe { mem::transmute(Self::get(self,),) } }
}

impl NonZero<i16> for NonZeroU16 {
  #[inline]
  fn new(num: i16,) -> Option<Self> { Self::new(unsafe { mem::transmute(num,) },) }
  #[inline]
  fn get(self,) -> i16 { unsafe { mem::transmute(Self::get(self,),) } }
}

impl NonZero<i32> for NonZeroU32 {
  #[inline]
  fn new(num: i32,) -> Option<Self> { Self::new(unsafe { mem::transmute(num,) },) }
  #[inline]
  fn get(self,) -> i32 { unsafe { mem::transmute(Self::get(self,),) } }
}

impl NonZero<i64> for NonZeroU64 {
  #[inline]
  fn new(num: i64,) -> Option<Self> { Self::new(unsafe { mem::transmute(num,) },) }
  #[inline]
  fn get(self,) -> i64 { unsafe { mem::transmute(Self::get(self,),) } }
}

impl NonZero<i128> for NonZeroU128 {
  #[inline]
  fn new(num: i128,) -> Option<Self> { Self::new(unsafe { mem::transmute(num,) },) }
  #[inline]
  fn get(self,) -> i128 { unsafe { mem::transmute(Self::get(self,),) } }
}

#[cfg(test,)]
mod tests {
  use super::*;

  #[test]
  fn test_i32_number() {
    /*Just a test that the `NonZero` implementations for i32
    (and assumingly other i-types) work as expected.
    */

    let two = <NonZeroU32 as NonZero<i32>>::new(2,);
    let neg_two = <NonZeroU32 as NonZero<i32>>::new(-2,);

    assert_eq!(two, NonZero::new(2u32,),);
    assert_eq!(neg_two, NonZero::new(std::u32::MAX - 1,),);
    
    let two = two.unwrap();
    let neg_two = neg_two.unwrap();

    assert_eq!(<NonZeroU32 as NonZero<i32>>::get(two,), 2i32,);
    assert_eq!(<NonZeroU32 as NonZero<i32>>::get(neg_two,), -2,);
  }
}