Skip to main content

base_traits/traits/
to_u32.rs

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