A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://docs.rs/simba/0.9.0/x86_64-unknown-linux-gnu/src/simba/scalar/subset.rs.html below:

subset.rs - source

1#[cfg(feature = "decimal")]
2use decimal::d128;
3use num::Zero;
4use num_complex::Complex;
5
6pub trait SubsetOf<T>: Sized {
25    fn to_superset(&self) -> T;
27
28    fn from_superset(element: &T) -> Option<Self> {
33        if Self::is_in_subset(element) {
34            Some(Self::from_superset_unchecked(element))
35        } else {
36            None
37        }
38    }
39
40    fn from_superset_unchecked(element: &T) -> Self;
42
43    fn is_in_subset(element: &T) -> bool;
45}
46
47pub trait SupersetOf<T>: Sized {
65    fn to_subset(&self) -> Option<T> {
70        if self.is_in_subset() {
71            Some(self.to_subset_unchecked())
72        } else {
73            None
74        }
75    }
76
77    fn is_in_subset(&self) -> bool;
79
80    fn to_subset_unchecked(&self) -> T;
82
83    fn from_subset(element: &T) -> Self;
85}
86
87impl<SS: SubsetOf<SP>, SP> SupersetOf<SS> for SP {
88    #[inline]
89    fn to_subset(&self) -> Option<SS> {
90        SS::from_superset(self)
91    }
92
93    #[inline]
94    fn is_in_subset(&self) -> bool {
95        SS::is_in_subset(self)
96    }
97
98    #[inline]
99    fn to_subset_unchecked(&self) -> SS {
100        SS::from_superset_unchecked(self)
101    }
102
103    #[inline]
104    fn from_subset(element: &SS) -> Self {
105        element.to_superset()
106    }
107}
108
109macro_rules! impl_subset (
110    ($($subset: ty as $( $superset: ty),+ );* $(;)*) => {
111        $($(
112        impl SubsetOf<$superset> for $subset {
113            #[inline]
114            fn to_superset(&self) -> $superset {
115                *self as $superset
116            }
117
118            #[inline]
119            fn from_superset_unchecked(element: &$superset) -> $subset {
120                *element as $subset
121            }
122
123            #[inline]
124            fn is_in_subset(_: &$superset) -> bool {
125                true
126            }
127        }
128        )+)*
129    }
130);
131
132impl_subset!(
133    u8    as u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64;
134    u16   as u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64;
135    u32   as u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64;
136    u64   as u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64;
137    u128  as u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64;
138    usize as u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64;
139
140    i8    as i8, i16, i32, i64, i128, isize, f32, f64;
141    i16   as i8, i16, i32, i64, i128, isize, f32, f64;
142    i32   as i8, i16, i32, i64, i128, isize, f32, f64;
143    i64   as i8, i16, i32, i64, i128, isize, f32, f64;
144    i128  as i8, i16, i32, i64, i128, isize, f32, f64;
145    isize as i8, i16, i32, i64, i128, isize, f32, f64;
146
147    f32 as f32, f64;
148    f64 as f32, f64;
149);
150impl<N1, N2: SupersetOf<N1>> SubsetOf<Complex<N2>> for Complex<N1> {
170    #[inline]
171    fn to_superset(&self) -> Complex<N2> {
172        Complex {
173            re: N2::from_subset(&self.re),
174            im: N2::from_subset(&self.im),
175        }
176    }
177
178    #[inline]
179    fn from_superset_unchecked(element: &Complex<N2>) -> Complex<N1> {
180        Complex {
181            re: element.re.to_subset_unchecked(),
182            im: element.im.to_subset_unchecked(),
183        }
184    }
185
186    #[inline]
187    fn is_in_subset(c: &Complex<N2>) -> bool {
188        c.re.is_in_subset() && c.im.is_in_subset()
189    }
190}
191
192macro_rules! impl_scalar_subset_of_complex (
193    ($($t: ident),*) => {$(
194        impl<N2: Zero + SupersetOf<$t>> SubsetOf<Complex<N2>> for $t {
195            #[inline]
196            fn to_superset(&self) -> Complex<N2> {
197                Complex {
198                    re: N2::from_subset(self),
199                    im: N2::zero()
200                }
201            }
202
203            #[inline]
204            fn from_superset_unchecked(element: &Complex<N2>) -> $t {
205                element.re.to_subset_unchecked()
206            }
207
208            #[inline]
209            fn is_in_subset(c: &Complex<N2>) -> bool {
210                c.re.is_in_subset() && c.im.is_zero()
211            }
212        }
213    )*}
214);
215
216impl_scalar_subset_of_complex!(
217    u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, f32, f64
218);
219#[cfg(feature = "decimal")]
220impl_scalar_subset_of_complex!(d128);

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4