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
97            const VALUES : &[u32] = &[
98                // insert list:
99                0,
100                1,
101                2, 4, 8, 16, 32, 32, 32, 256,
102                u16::MAX as u32,
103                u32::MAX,
104            ];
105
106            for &value in VALUES {
107                let expected = value;
108                let instance = CustomType { value: value };
109                let actual = instance.to_u32();
110
111                assert_eq!(expected, actual);
112            }
113        }
114    }
115
116
117    #[cfg(feature = "implement-ToU32-for-built_ins")]
118    mod TEST_BUILTIN_TYPES {
119        #![allow(non_snake_case)]
120
121        use super::*;
122
123
124        #[test]
125        fn TEST_RANGE_OF_u32_VALUES() {
126
127            const VALUES : &[u32] = &[
128                // insert list:
129                0,
130                1,
131                2,
132                4,
133                8,
134                16,
135                32,
136                32,
137                32,
138                256,
139                u32::MAX,
140            ];
141
142            for &value in VALUES {
143                let expected = value;
144                let actual = value.to_u32();
145
146                assert_eq!(expected, actual);
147            }
148        }
149
150        #[test]
151        fn TEST_RANGE_OF_u16_VALUES_REF() {
152
153            const VALUES : &[u16] = &[
154                // insert list:
155                0,
156                1,
157                2,
158                4,
159                8,
160                16,
161                32,
162                32,
163                32,
164                256,
165                u16::MAX,
166            ];
167
168            for &value in VALUES {
169                let expected = value as u32;
170                let value = &value;
171                let actual = value.to_u32();
172
173                assert_eq!(expected, actual);
174            }
175        }
176
177        #[test]
178        fn TEST_RANGE_OF_u32_VALUES_REF() {
179
180            const VALUES : &[u32] = &[
181                // insert list:
182                0,
183                1,
184                2,
185                4,
186                8,
187                16,
188                32,
189                32,
190                32,
191                256,
192                u32::MAX,
193            ];
194
195            for &value in VALUES {
196                let expected = value;
197                let value = &value;
198                let actual = value.to_u32();
199
200                assert_eq!(expected, actual);
201            }
202        }
203
204        #[test]
205        fn TEST_RANGE_OF_u32_VALUES_IN_Box() {
206
207            const VALUES : &[u32] = &[
208                // insert list:
209                0,
210                1,
211                2,
212                4,
213                8,
214                16,
215                32,
216                32,
217                32,
218                256,
219                u32::MAX,
220            ];
221
222            for &value in VALUES {
223                let expected = value;
224                let instance = Box::new(value);
225                let actual = instance.to_u32();
226
227                assert_eq!(expected, actual);
228            }
229        }
230
231        #[test]
232        fn TEST_RANGE_OF_u32_VALUES_IN_REF_Box() {
233
234            const VALUES : &[u32] = &[
235                // insert list:
236                0,
237                1,
238                2,
239                4,
240                8,
241                16,
242                32,
243                32,
244                32,
245                256,
246                u32::MAX,
247            ];
248
249            for &value in VALUES {
250                let expected = value;
251                let instance = &Box::new(value);
252                let actual = instance.to_u32();
253
254                assert_eq!(expected, actual);
255            }
256        }
257
258        #[test]
259        fn TEST_RANGE_OF_u32_VALUES_IN_Rc() {
260
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
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 //////////////////////////// //
316