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    #[allow(unused_imports)]
123    use std::rc as std_rc;
124
125
126    mod TEST_CUSTOM_TYPE {
127        #![allow(non_snake_case)]
128
129        use super::ToISize;
130
131
132        struct CustomType {
133            value : u64,
134        }
135
136        impl ToISize for CustomType {
137            fn to_isize(&self) -> isize {
138                self.value as isize
139            }
140        }
141
142        #[test]
143        fn TEST_RANGE_OF_VALUES() {
144            const VALUES : &[isize] = &[
145                // insert list:
146                0,
147                1,
148                2,
149                4,
150                8,
151                16,
152                32,
153                64,
154                128,
155                256,
156                u16::MAX as isize,
157                u32::MAX as isize,
158                u64::MAX as isize,
159            ];
160
161            for &value in VALUES {
162                let expected = value;
163                let instance = CustomType { value: value as u64 };
164                let actual = instance.to_isize();
165
166                assert_eq!(expected, actual);
167            }
168        }
169    }
170
171
172    #[cfg(feature = "implement-ToISize-for-built_ins")]
173    mod TEST_BUILTIN_TYPES {
174        #![allow(non_snake_case)]
175        #![allow(unexpected_cfgs)]
176
177        use super::*;
178
179
180        #[test]
181        fn TEST_RANGE_OF_isize_VALUES() {
182            const VALUES : &[isize] = &[
183                // insert list:
184                0,
185                1,
186                2,
187                4,
188                8,
189                16,
190                32,
191                64,
192                128,
193                256,
194                isize::MAX,
195            ];
196
197            for &value in VALUES {
198                let expected = value;
199                let actual = value.to_isize();
200
201                assert_eq!(expected, actual);
202            }
203        }
204
205        #[cfg(any(
206            target_pointer_width = "32",
207            target_pointer_width = "64",
208            target_pointer_width = "128",
209        ))]
210        #[test]
211        fn TEST_RANGE_OF_i16_VALUES_REF() {
212            const VALUES : &[i16] = &[
213                // insert list:
214                0,
215                1,
216                2,
217                4,
218                8,
219                16,
220                32,
221                64,
222                128,
223                256,
224                i16::MAX,
225            ];
226
227            for &value in VALUES {
228                let expected = value as isize;
229                let value = &value;
230                let actual = value.to_isize();
231
232                assert_eq!(expected, actual);
233            }
234        }
235
236        #[cfg(any(
237            target_pointer_width = "16",
238            target_pointer_width = "32",
239            target_pointer_width = "64",
240            target_pointer_width = "128",
241        ))]
242        #[test]
243        fn TEST_RANGE_OF_u16_VALUES_REF() {
244            const VALUES : &[u16] = &[
245                // insert list:
246                0,
247                1,
248                2,
249                4,
250                8,
251                16,
252                32,
253                64,
254                128,
255                256,
256                u16::MAX,
257            ];
258
259            for &value in VALUES {
260                let expected = value as isize;
261                let value = &value;
262                let actual = value.to_isize();
263
264                assert_eq!(expected, actual);
265            }
266        }
267
268        #[cfg(any(
269            target_pointer_width = "32",
270            target_pointer_width = "64",
271            target_pointer_width = "128",
272        ))]
273        #[test]
274        fn TEST_RANGE_OF_u32_VALUES_REF() {
275            const VALUES : &[u32] = &[
276                // insert list:
277                0,
278                1,
279                2,
280                4,
281                8,
282                16,
283                32,
284                64,
285                128,
286                256,
287                u32::MAX,
288            ];
289
290            for &value in VALUES {
291                let expected = value as isize;
292                let value = &value;
293                let actual = value.to_isize();
294
295                assert_eq!(expected, actual);
296            }
297        }
298
299        #[test]
300        fn TEST_RANGE_OF_isize_VALUES_REF() {
301            const VALUES : &[isize] = &[
302                // insert list:
303                0,
304                1,
305                2,
306                4,
307                8,
308                16,
309                32,
310                64,
311                128,
312                256,
313                isize::MAX,
314            ];
315
316            for &value in VALUES {
317                let expected = value;
318                let value = &value;
319                let actual = value.to_isize();
320
321                assert_eq!(expected, actual);
322            }
323        }
324
325        #[test]
326        fn TEST_RANGE_OF_isize_VALUES_IN_Box() {
327            const VALUES : &[isize] = &[
328                // insert list:
329                0,
330                1,
331                2,
332                4,
333                8,
334                16,
335                32,
336                64,
337                128,
338                256,
339                isize::MAX,
340            ];
341
342            for &value in VALUES {
343                let expected = value;
344                let instance = Box::new(value);
345                let actual = instance.to_isize();
346
347                assert_eq!(expected, actual);
348            }
349        }
350
351        #[test]
352        fn TEST_RANGE_OF_isize_VALUES_IN_REF_Box() {
353            const VALUES : &[isize] = &[
354                // insert list:
355                0,
356                1,
357                2,
358                4,
359                8,
360                16,
361                32,
362                64,
363                128,
364                256,
365                isize::MAX,
366            ];
367
368            for &value in VALUES {
369                let expected = value;
370                let instance = &Box::new(value);
371                let actual = instance.to_isize();
372
373                assert_eq!(expected, actual);
374            }
375        }
376
377        #[test]
378        fn TEST_RANGE_OF_isize_VALUES_IN_Rc() {
379            const VALUES : &[isize] = &[
380                // insert list:
381                0,
382                1,
383                2,
384                4,
385                8,
386                16,
387                32,
388                64,
389                128,
390                256,
391                isize::MAX,
392            ];
393
394            for &value in VALUES {
395                let expected = value;
396                let instance = std_rc::Rc::new(value);
397                let actual = instance.to_isize();
398
399                assert_eq!(expected, actual);
400            }
401        }
402
403        #[test]
404        fn TEST_RANGE_OF_isize_VALUES_IN_REF_Rc() {
405            const VALUES : &[isize] = &[
406                // insert list:
407                0,
408                1,
409                2,
410                4,
411                8,
412                16,
413                32,
414                64,
415                128,
416                256,
417                isize::MAX,
418            ];
419
420            for &value in VALUES {
421                let expected = value;
422                let instance = &std_rc::Rc::new(value);
423                let actual = instance.to_isize();
424
425                assert_eq!(expected, actual);
426            }
427        }
428    }
429}
430
431
432// ///////////////////////////// end of file //////////////////////////// //