Skip to main content

base_traits/traits/
to_i64.rs

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