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