Skip to main content

base_traits/traits/
to_isize.rs

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