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    #[allow(unused_imports)]
75    use std::rc as std_rc;
76
77
78    mod TEST_CUSTOM_TYPE {
79        #![allow(clippy::redundant_field_names)]
80        #![allow(non_snake_case)]
81
82        use super::ToU32;
83
84
85        struct CustomType {
86            value : u32,
87        }
88
89        impl ToU32 for CustomType {
90            fn to_u32(&self) -> u32 {
91                self.value
92            }
93        }
94
95        #[test]
96        fn TEST_RANGE_OF_VALUES() {
97            const VALUES : &[u32] = &[
98                // insert list:
99                0,
100                1,
101                2,
102                4,
103                8,
104                16,
105                32,
106                32,
107                32,
108                256,
109                u16::MAX as u32,
110                u32::MAX,
111            ];
112
113            for &value in VALUES {
114                let expected = value;
115                let instance = CustomType { value: value };
116                let actual = instance.to_u32();
117
118                assert_eq!(expected, actual);
119            }
120        }
121    }
122
123
124    #[cfg(feature = "implement-ToU32-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_u32_VALUES() {
133            const VALUES : &[u32] = &[
134                // insert list:
135                0,
136                1,
137                2,
138                4,
139                8,
140                16,
141                32,
142                32,
143                32,
144                256,
145                u32::MAX,
146            ];
147
148            for &value in VALUES {
149                let expected = value;
150                let actual = value.to_u32();
151
152                assert_eq!(expected, actual);
153            }
154        }
155
156        #[test]
157        fn TEST_RANGE_OF_u16_VALUES_REF() {
158            const VALUES : &[u16] = &[
159                // insert list:
160                0,
161                1,
162                2,
163                4,
164                8,
165                16,
166                32,
167                32,
168                32,
169                256,
170                u16::MAX,
171            ];
172
173            for &value in VALUES {
174                let expected = value as u32;
175                let value = &value;
176                let actual = value.to_u32();
177
178                assert_eq!(expected, actual);
179            }
180        }
181
182        #[test]
183        fn TEST_RANGE_OF_u32_VALUES_REF() {
184            const VALUES : &[u32] = &[
185                // insert list:
186                0,
187                1,
188                2,
189                4,
190                8,
191                16,
192                32,
193                32,
194                32,
195                256,
196                u32::MAX,
197            ];
198
199            for &value in VALUES {
200                let expected = value;
201                let value = &value;
202                let actual = value.to_u32();
203
204                assert_eq!(expected, actual);
205            }
206        }
207
208        #[test]
209        fn TEST_RANGE_OF_u32_VALUES_IN_Box() {
210            const VALUES : &[u32] = &[
211                // insert list:
212                0,
213                1,
214                2,
215                4,
216                8,
217                16,
218                32,
219                32,
220                32,
221                256,
222                u32::MAX,
223            ];
224
225            for &value in VALUES {
226                let expected = value;
227                let instance = Box::new(value);
228                let actual = instance.to_u32();
229
230                assert_eq!(expected, actual);
231            }
232        }
233
234        #[test]
235        fn TEST_RANGE_OF_u32_VALUES_IN_REF_Box() {
236            const VALUES : &[u32] = &[
237                // insert list:
238                0,
239                1,
240                2,
241                4,
242                8,
243                16,
244                32,
245                32,
246                32,
247                256,
248                u32::MAX,
249            ];
250
251            for &value in VALUES {
252                let expected = value;
253                let instance = &Box::new(value);
254                let actual = instance.to_u32();
255
256                assert_eq!(expected, actual);
257            }
258        }
259
260        #[test]
261        fn TEST_RANGE_OF_u32_VALUES_IN_Rc() {
262            const VALUES : &[u32] = &[
263                // insert list:
264                0,
265                1,
266                2,
267                4,
268                8,
269                16,
270                32,
271                32,
272                32,
273                256,
274                u32::MAX,
275            ];
276
277            for &value in VALUES {
278                let expected = value;
279                let instance = std_rc::Rc::new(value);
280                let actual = instance.to_u32();
281
282                assert_eq!(expected, actual);
283            }
284        }
285
286        #[test]
287        fn TEST_RANGE_OF_u32_VALUES_IN_REF_Rc() {
288            const VALUES : &[u32] = &[
289                // insert list:
290                0,
291                1,
292                2,
293                4,
294                8,
295                16,
296                32,
297                32,
298                32,
299                256,
300                u32::MAX,
301            ];
302
303            for &value in VALUES {
304                let expected = value;
305                let instance = &std_rc::Rc::new(value);
306                let actual = instance.to_u32();
307
308                assert_eq!(expected, actual);
309            }
310        }
311    }
312}
313
314
315// ///////////////////////////// end of file //////////////////////////// //