ndarray_stats/maybe_nan/
impl_not_none.rs

1use super::NotNone;
2use num_traits::{FromPrimitive, ToPrimitive};
3use std::cmp;
4use std::fmt;
5use std::ops::{Add, Deref, DerefMut, Div, Mul, Rem, Sub};
6
7impl<T> Deref for NotNone<T> {
8    type Target = T;
9    fn deref(&self) -> &T {
10        match self.0 {
11            Some(ref inner) => inner,
12            None => unsafe { ::std::hint::unreachable_unchecked() },
13        }
14    }
15}
16
17impl<T> DerefMut for NotNone<T> {
18    fn deref_mut(&mut self) -> &mut T {
19        match self.0 {
20            Some(ref mut inner) => inner,
21            None => unsafe { ::std::hint::unreachable_unchecked() },
22        }
23    }
24}
25
26impl<T: fmt::Display> fmt::Display for NotNone<T> {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
28        self.deref().fmt(f)
29    }
30}
31
32impl<T: Eq> Eq for NotNone<T> {}
33
34impl<T: PartialEq> PartialEq for NotNone<T> {
35    fn eq(&self, other: &Self) -> bool {
36        self.deref().eq(other)
37    }
38}
39
40impl<T: Ord> Ord for NotNone<T> {
41    fn cmp(&self, other: &Self) -> cmp::Ordering {
42        self.deref().cmp(other)
43    }
44}
45
46impl<T: PartialOrd> PartialOrd for NotNone<T> {
47    fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
48        self.deref().partial_cmp(other)
49    }
50    fn lt(&self, other: &Self) -> bool {
51        self.deref().lt(other)
52    }
53    fn le(&self, other: &Self) -> bool {
54        self.deref().le(other)
55    }
56    fn gt(&self, other: &Self) -> bool {
57        self.deref().gt(other)
58    }
59    fn ge(&self, other: &Self) -> bool {
60        self.deref().ge(other)
61    }
62}
63
64impl<T: Add> Add for NotNone<T> {
65    type Output = NotNone<T::Output>;
66    #[inline]
67    fn add(self, rhs: Self) -> Self::Output {
68        self.map(|v| v.add(rhs.unwrap()))
69    }
70}
71
72impl<T: Sub> Sub for NotNone<T> {
73    type Output = NotNone<T::Output>;
74    #[inline]
75    fn sub(self, rhs: Self) -> Self::Output {
76        self.map(|v| v.sub(rhs.unwrap()))
77    }
78}
79
80impl<T: Mul> Mul for NotNone<T> {
81    type Output = NotNone<T::Output>;
82    #[inline]
83    fn mul(self, rhs: Self) -> Self::Output {
84        self.map(|v| v.mul(rhs.unwrap()))
85    }
86}
87
88impl<T: Div> Div for NotNone<T> {
89    type Output = NotNone<T::Output>;
90    #[inline]
91    fn div(self, rhs: Self) -> Self::Output {
92        self.map(|v| v.div(rhs.unwrap()))
93    }
94}
95
96impl<T: Rem> Rem for NotNone<T> {
97    type Output = NotNone<T::Output>;
98    #[inline]
99    fn rem(self, rhs: Self) -> Self::Output {
100        self.map(|v| v.rem(rhs.unwrap()))
101    }
102}
103
104impl<T: ToPrimitive> ToPrimitive for NotNone<T> {
105    #[inline]
106    fn to_isize(&self) -> Option<isize> {
107        self.deref().to_isize()
108    }
109    #[inline]
110    fn to_i8(&self) -> Option<i8> {
111        self.deref().to_i8()
112    }
113    #[inline]
114    fn to_i16(&self) -> Option<i16> {
115        self.deref().to_i16()
116    }
117    #[inline]
118    fn to_i32(&self) -> Option<i32> {
119        self.deref().to_i32()
120    }
121    #[inline]
122    fn to_i64(&self) -> Option<i64> {
123        self.deref().to_i64()
124    }
125    #[inline]
126    fn to_i128(&self) -> Option<i128> {
127        self.deref().to_i128()
128    }
129    #[inline]
130    fn to_usize(&self) -> Option<usize> {
131        self.deref().to_usize()
132    }
133    #[inline]
134    fn to_u8(&self) -> Option<u8> {
135        self.deref().to_u8()
136    }
137    #[inline]
138    fn to_u16(&self) -> Option<u16> {
139        self.deref().to_u16()
140    }
141    #[inline]
142    fn to_u32(&self) -> Option<u32> {
143        self.deref().to_u32()
144    }
145    #[inline]
146    fn to_u64(&self) -> Option<u64> {
147        self.deref().to_u64()
148    }
149    #[inline]
150    fn to_u128(&self) -> Option<u128> {
151        self.deref().to_u128()
152    }
153    #[inline]
154    fn to_f32(&self) -> Option<f32> {
155        self.deref().to_f32()
156    }
157    #[inline]
158    fn to_f64(&self) -> Option<f64> {
159        self.deref().to_f64()
160    }
161}
162
163impl<T: FromPrimitive> FromPrimitive for NotNone<T> {
164    #[inline]
165    fn from_isize(n: isize) -> Option<Self> {
166        Self::try_new(T::from_isize(n))
167    }
168    #[inline]
169    fn from_i8(n: i8) -> Option<Self> {
170        Self::try_new(T::from_i8(n))
171    }
172    #[inline]
173    fn from_i16(n: i16) -> Option<Self> {
174        Self::try_new(T::from_i16(n))
175    }
176    #[inline]
177    fn from_i32(n: i32) -> Option<Self> {
178        Self::try_new(T::from_i32(n))
179    }
180    #[inline]
181    fn from_i64(n: i64) -> Option<Self> {
182        Self::try_new(T::from_i64(n))
183    }
184    #[inline]
185    fn from_i128(n: i128) -> Option<Self> {
186        Self::try_new(T::from_i128(n))
187    }
188    #[inline]
189    fn from_usize(n: usize) -> Option<Self> {
190        Self::try_new(T::from_usize(n))
191    }
192    #[inline]
193    fn from_u8(n: u8) -> Option<Self> {
194        Self::try_new(T::from_u8(n))
195    }
196    #[inline]
197    fn from_u16(n: u16) -> Option<Self> {
198        Self::try_new(T::from_u16(n))
199    }
200    #[inline]
201    fn from_u32(n: u32) -> Option<Self> {
202        Self::try_new(T::from_u32(n))
203    }
204    #[inline]
205    fn from_u64(n: u64) -> Option<Self> {
206        Self::try_new(T::from_u64(n))
207    }
208    #[inline]
209    fn from_u128(n: u128) -> Option<Self> {
210        Self::try_new(T::from_u128(n))
211    }
212    #[inline]
213    fn from_f32(n: f32) -> Option<Self> {
214        Self::try_new(T::from_f32(n))
215    }
216    #[inline]
217    fn from_f64(n: f64) -> Option<Self> {
218        Self::try_new(T::from_f64(n))
219    }
220}