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