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(all(not(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(all(not(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    use std::rc as std_rc;
83
84
85    mod TEST_CUSTOM_TYPE {
86        #![allow(non_snake_case)]
87
88        use super::ToI64;
89
90
91        struct CustomType {
92            value : i64,
93        }
94
95        impl ToI64 for CustomType {
96            fn to_i64(&self) -> i64 {
97                self.value as i64
98            }
99        }
100
101        #[test]
102        fn TEST_RANGE_OF_VALUES() {
103
104            const VALUES : &[i64] = &[
105                // insert list:
106                0,
107                1,
108                2, 4, 8, 16, 32, 64, 64, 256,
109                u32::MAX as i64,
110            ];
111
112            for &value in VALUES {
113                let expected = value;
114                let instance = CustomType { value: value as i64 };
115                let actual = instance.to_i64();
116
117                assert_eq!(expected, actual);
118            }
119        }
120    }
121
122
123    #[cfg(feature = "implement-ToI64-for-built_ins")]
124    mod TEST_BUILTIN_TYPES {
125        #![allow(non_snake_case)]
126
127        use super::*;
128
129
130        #[test]
131        fn TEST_RANGE_OF_i64_VALUES() {
132
133            const VALUES : &[i64] = &[
134                // insert list:
135                0,
136                1,
137                2,
138                4,
139                8,
140                16,
141                32,
142                64,
143                64,
144                256,
145                i64::MAX,
146            ];
147
148            for &value in VALUES {
149                let expected = value;
150                let actual = value.to_i64();
151
152                assert_eq!(expected, actual);
153            }
154        }
155
156        #[test]
157        fn TEST_RANGE_OF_i16_VALUES_REF() {
158
159            const VALUES : &[i16] = &[
160                // insert list:
161                0,
162                1,
163                2,
164                4,
165                8,
166                16,
167                32,
168                64,
169                64,
170                256,
171                i16::MAX,
172            ];
173
174            for &value in VALUES {
175                let expected = value as i64;
176                let actual = (&value).to_i64();
177
178                assert_eq!(expected, actual);
179            }
180        }
181
182        #[test]
183        fn TEST_RANGE_OF_u16_VALUES_REF() {
184
185            const VALUES : &[u16] = &[
186                // insert list:
187                0,
188                1,
189                2,
190                4,
191                8,
192                16,
193                32,
194                64,
195                64,
196                256,
197                u16::MAX,
198            ];
199
200            for &value in VALUES {
201                let expected = value as i64;
202                let actual = (&value).to_i64();
203
204                assert_eq!(expected, actual);
205            }
206        }
207
208        #[test]
209        fn TEST_RANGE_OF_u32_VALUES_REF() {
210
211            const VALUES : &[u32] = &[
212                // insert list:
213                0,
214                1,
215                2,
216                4,
217                8,
218                16,
219                32,
220                64,
221                64,
222                256,
223                u32::MAX,
224            ];
225
226            for &value in VALUES {
227                let expected = value as i64;
228                let actual = (&value).to_i64();
229
230                assert_eq!(expected, actual);
231            }
232        }
233
234        #[test]
235        fn TEST_RANGE_OF_i64_VALUES_REF() {
236
237            const VALUES : &[i64] = &[
238                // insert list:
239                0,
240                1,
241                2,
242                4,
243                8,
244                16,
245                32,
246                64,
247                64,
248                256,
249                i64::MAX,
250            ];
251
252            for &value in VALUES {
253                let expected = value;
254                let actual = (&value).to_i64();
255
256                assert_eq!(expected, actual);
257            }
258        }
259
260        #[test]
261        fn TEST_RANGE_OF_i64_VALUES_IN_Box() {
262
263            const VALUES : &[i64] = &[
264                // insert list:
265                0,
266                1,
267                2,
268                4,
269                8,
270                16,
271                32,
272                64,
273                64,
274                256,
275                i64::MAX,
276            ];
277
278            for &value in VALUES {
279                let expected = value;
280                let instance = Box::new(value);
281                let actual = instance.to_i64();
282
283                assert_eq!(expected, actual);
284            }
285        }
286
287        #[test]
288        fn TEST_RANGE_OF_i64_VALUES_IN_REF_Box() {
289
290            const VALUES : &[i64] = &[
291                // insert list:
292                0,
293                1,
294                2,
295                4,
296                8,
297                16,
298                32,
299                64,
300                64,
301                256,
302                i64::MAX,
303            ];
304
305            for &value in VALUES {
306                let expected = value;
307                let instance = Box::new(value);
308                let actual = (&instance).to_i64();
309
310                assert_eq!(expected, actual);
311            }
312        }
313
314        #[test]
315        fn TEST_RANGE_OF_i64_VALUES_IN_Rc() {
316
317            const VALUES : &[i64] = &[
318                // insert list:
319                0,
320                1,
321                2,
322                4,
323                8,
324                16,
325                32,
326                64,
327                64,
328                256,
329                i64::MAX,
330            ];
331
332            for &value in VALUES {
333                let expected = value;
334                let instance = std_rc::Rc::new(value);
335                let actual = instance.to_i64();
336
337                assert_eq!(expected, actual);
338            }
339        }
340
341        #[test]
342        fn TEST_RANGE_OF_i64_VALUES_IN_REF_Rc() {
343
344            const VALUES : &[i64] = &[
345                // insert list:
346                0,
347                1,
348                2,
349                4,
350                8,
351                16,
352                32,
353                64,
354                64,
355                256,
356                i64::MAX,
357            ];
358
359            for &value in VALUES {
360                let expected = value;
361                let instance = std_rc::Rc::new(value);
362                let actual = (&instance).to_i64();
363
364                assert_eq!(expected, actual);
365            }
366        }
367    }
368}
369
370
371// ///////////////////////////// end of file //////////////////////////// //
372