base_traits/traits/
to_u128.rs

1// src/traits/to_u128.rs : `ToU128`
2
3/// Trait defining instance method `to_u128() : u128` that provides a
4/// no-cost or low-cost conversion into `u128`.
5///
6/// It is expected that the implementing type "is-a" `u128` in a logical
7/// manner.
8///
9/// # Additional Implementations on Foreign Types
10///
11/// ## Built-in Types
12///
13/// If the feature `"implement-ToU128-for-built_ins"`
14/// is defined (as it is by `"default"`), then this is also implemented
15/// for the following type(s):
16/// - [`u8`];
17/// - [`u16`];
18/// - [`u32`];
19/// - [`u64`];
20/// - [`u128`];
21pub trait ToU128 {
22    fn to_u128(&self) -> u128;
23}
24
25
26#[cfg(all(not(test), not(feature = "nostd")))]
27impl<T : ToU128 + ?Sized> ToU128 for Box<T> {
28    fn to_u128(&self) -> u128 {
29        (**self).to_u128()
30    }
31}
32
33#[cfg(all(not(test), not(feature = "nostd")))]
34impl<T : ToU128 + ?Sized> ToU128 for std::rc::Rc<T> {
35    fn to_u128(&self) -> u128 {
36        (**self).to_u128()
37    }
38}
39
40
41#[cfg(feature = "implement-ToU128-for-built_ins")]
42#[rustfmt::skip]
43mod impl_for_built_ins {
44    #![allow(non_snake_case)]
45
46
47    impl super::ToU128 for u128 {
48        #[inline]
49        fn to_u128(&self) -> u128 {
50            *self
51        }
52    }
53
54    macro_rules! implement_ToU128_ {
55        ($type:tt) => {
56            impl super::ToU128 for $type {
57                #[inline]
58                fn to_u128(&self) -> u128 {
59                    *self as u128
60                }
61            }
62        };
63    }
64
65    implement_ToU128_!(u8);
66    implement_ToU128_!(u16);
67    implement_ToU128_!(u32);
68    implement_ToU128_!(u64);
69}
70
71
72#[cfg(test)]
73mod tests {
74    #![allow(non_snake_case)]
75
76    use super::ToU128;
77
78    use std::rc as std_rc;
79
80
81    mod TEST_CUSTOM_TYPE {
82        #![allow(non_snake_case)]
83
84        use super::ToU128;
85
86
87        struct CustomType {
88            value : u128,
89        }
90
91        impl ToU128 for CustomType {
92            fn to_u128(&self) -> u128 {
93                self.value as u128
94            }
95        }
96
97        #[test]
98        fn TEST_RANGE_OF_VALUES() {
99
100            const VALUES : &[u128] = &[
101                // insert list:
102                0,
103                1,
104                2, 4, 8, 16, 32, 64, 128, 256,
105                u32::MAX as u128,
106            ];
107
108            for &value in VALUES {
109                let expected = value;
110                let instance = CustomType { value: value as u128 };
111                let actual = instance.to_u128();
112
113                assert_eq!(expected, actual);
114            }
115        }
116    }
117
118
119    #[cfg(feature = "implement-ToU128-for-built_ins")]
120    mod TEST_BUILTIN_TYPES {
121        #![allow(non_snake_case)]
122
123        use super::*;
124
125
126        #[test]
127        fn TEST_RANGE_OF_u128_VALUES() {
128
129            const VALUES : &[u128] = &[
130                // insert list:
131                0,
132                1,
133                2,
134                4,
135                8,
136                16,
137                32,
138                64,
139                128,
140                256,
141                u128::MAX,
142            ];
143
144            for &value in VALUES {
145                let expected = value;
146                let actual = value.to_u128();
147
148                assert_eq!(expected, actual);
149            }
150        }
151
152        #[test]
153        fn TEST_RANGE_OF_u16_VALUES_REF() {
154
155            const VALUES : &[u16] = &[
156                // insert list:
157                0,
158                1,
159                2,
160                4,
161                8,
162                16,
163                32,
164                64,
165                128,
166                256,
167                u16::MAX,
168            ];
169
170            for &value in VALUES {
171                let expected = value as u128;
172                let actual = (&value).to_u128();
173
174                assert_eq!(expected, actual);
175            }
176        }
177
178        #[test]
179        fn TEST_RANGE_OF_u32_VALUES_REF() {
180
181            const VALUES : &[u32] = &[
182                // insert list:
183                0,
184                1,
185                2,
186                4,
187                8,
188                16,
189                32,
190                64,
191                128,
192                256,
193                u32::MAX,
194            ];
195
196            for &value in VALUES {
197                let expected = value as u128;
198                let actual = (&value).to_u128();
199
200                assert_eq!(expected, actual);
201            }
202        }
203
204        #[test]
205        fn TEST_RANGE_OF_u128_VALUES_REF() {
206
207            const VALUES : &[u128] = &[
208                // insert list:
209                0,
210                1,
211                2,
212                4,
213                8,
214                16,
215                32,
216                64,
217                128,
218                256,
219                u128::MAX,
220            ];
221
222            for &value in VALUES {
223                let expected = value;
224                let actual = (&value).to_u128();
225
226                assert_eq!(expected, actual);
227            }
228        }
229
230        #[test]
231        fn TEST_RANGE_OF_u128_VALUES_IN_Box() {
232
233            const VALUES : &[u128] = &[
234                // insert list:
235                0,
236                1,
237                2,
238                4,
239                8,
240                16,
241                32,
242                64,
243                128,
244                256,
245                u128::MAX,
246            ];
247
248            for &value in VALUES {
249                let expected = value;
250                let instance = Box::new(value);
251                let actual = instance.to_u128();
252
253                assert_eq!(expected, actual);
254            }
255        }
256
257        #[test]
258        fn TEST_RANGE_OF_u128_VALUES_IN_REF_Box() {
259
260            const VALUES : &[u128] = &[
261                // insert list:
262                0,
263                1,
264                2,
265                4,
266                8,
267                16,
268                32,
269                64,
270                128,
271                256,
272                u128::MAX,
273            ];
274
275            for &value in VALUES {
276                let expected = value;
277                let instance = Box::new(value);
278                let actual = (&instance).to_u128();
279
280                assert_eq!(expected, actual);
281            }
282        }
283
284        #[test]
285        fn TEST_RANGE_OF_u128_VALUES_IN_Rc() {
286
287            const VALUES : &[u128] = &[
288                // insert list:
289                0,
290                1,
291                2,
292                4,
293                8,
294                16,
295                32,
296                64,
297                128,
298                256,
299                u128::MAX,
300            ];
301
302            for &value in VALUES {
303                let expected = value;
304                let instance = std_rc::Rc::new(value);
305                let actual = instance.to_u128();
306
307                assert_eq!(expected, actual);
308            }
309        }
310
311        #[test]
312        fn TEST_RANGE_OF_u128_VALUES_IN_REF_Rc() {
313
314            const VALUES : &[u128] = &[
315                // insert list:
316                0,
317                1,
318                2,
319                4,
320                8,
321                16,
322                32,
323                64,
324                128,
325                256,
326                u128::MAX,
327            ];
328
329            for &value in VALUES {
330                let expected = value;
331                let instance = std_rc::Rc::new(value);
332                let actual = (&instance).to_u128();
333
334                assert_eq!(expected, actual);
335            }
336        }
337    }
338}
339
340
341// ///////////////////////////// end of file //////////////////////////// //
342