base_traits/traits/
to_usize.rs

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