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