Skip to main content

base_traits/traits/
to_u64.rs

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