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