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