Skip to main content

base_traits/traits/
to_i128.rs

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