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
144            const VALUES : &[isize] = &[
145                // insert list:
146                0,
147                1,
148                2, 4, 8, 16, 32, 64, 128, 256,
149                u16::MAX as isize,
150                u32::MAX as isize,
151                u64::MAX as isize,
152            ];
153
154            for &value in VALUES {
155                let expected = value;
156                let instance = CustomType { value: value as u64 };
157                let actual = instance.to_isize();
158
159                assert_eq!(expected, actual);
160            }
161        }
162    }
163
164
165    #[cfg(feature = "implement-ToISize-for-built_ins")]
166    mod TEST_BUILTIN_TYPES {
167        #![allow(non_snake_case)]
168        #![allow(unexpected_cfgs)]
169
170        use super::*;
171
172
173        #[test]
174        fn TEST_RANGE_OF_isize_VALUES() {
175
176            const VALUES : &[isize] = &[
177                // insert list:
178                0,
179                1,
180                2,
181                4,
182                8,
183                16,
184                32,
185                64,
186                128,
187                256,
188                isize::MAX,
189            ];
190
191            for &value in VALUES {
192                let expected = value;
193                let actual = value.to_isize();
194
195                assert_eq!(expected, actual);
196            }
197        }
198
199        #[cfg(any(
200            target_pointer_width = "32",
201            target_pointer_width = "64",
202            target_pointer_width = "128",
203        ))]
204        #[test]
205        fn TEST_RANGE_OF_i16_VALUES_REF() {
206
207            const VALUES : &[i16] = &[
208                // insert list:
209                0,
210                1,
211                2,
212                4,
213                8,
214                16,
215                32,
216                64,
217                128,
218                256,
219                i16::MAX,
220            ];
221
222            for &value in VALUES {
223                let expected = value as isize;
224                let value = &value;
225                let actual = value.to_isize();
226
227                assert_eq!(expected, actual);
228            }
229        }
230
231        #[cfg(any(
232            target_pointer_width = "16",
233            target_pointer_width = "32",
234            target_pointer_width = "64",
235            target_pointer_width = "128",
236        ))]
237        #[test]
238        fn TEST_RANGE_OF_u16_VALUES_REF() {
239
240            const VALUES : &[u16] = &[
241                // insert list:
242                0,
243                1,
244                2,
245                4,
246                8,
247                16,
248                32,
249                64,
250                128,
251                256,
252                u16::MAX,
253            ];
254
255            for &value in VALUES {
256                let expected = value as isize;
257                let value = &value;
258                let actual = value.to_isize();
259
260                assert_eq!(expected, actual);
261            }
262        }
263
264        #[cfg(any(
265            target_pointer_width = "32",
266            target_pointer_width = "64",
267            target_pointer_width = "128",
268        ))]
269        #[test]
270        fn TEST_RANGE_OF_u32_VALUES_REF() {
271
272            const VALUES : &[u32] = &[
273                // insert list:
274                0,
275                1,
276                2,
277                4,
278                8,
279                16,
280                32,
281                64,
282                128,
283                256,
284                u32::MAX,
285            ];
286
287            for &value in VALUES {
288                let expected = value as isize;
289                let value = &value;
290                let actual = value.to_isize();
291
292                assert_eq!(expected, actual);
293            }
294        }
295
296        #[test]
297        fn TEST_RANGE_OF_isize_VALUES_REF() {
298
299            const VALUES : &[isize] = &[
300                // insert list:
301                0,
302                1,
303                2,
304                4,
305                8,
306                16,
307                32,
308                64,
309                128,
310                256,
311                isize::MAX,
312            ];
313
314            for &value in VALUES {
315                let expected = value;
316                let value = &value;
317                let actual = value.to_isize();
318
319                assert_eq!(expected, actual);
320            }
321        }
322
323        #[test]
324        fn TEST_RANGE_OF_isize_VALUES_IN_Box() {
325
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
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
380            const VALUES : &[isize] = &[
381                // insert list:
382                0,
383                1,
384                2,
385                4,
386                8,
387                16,
388                32,
389                64,
390                128,
391                256,
392                isize::MAX,
393            ];
394
395            for &value in VALUES {
396                let expected = value;
397                let instance = std_rc::Rc::new(value);
398                let actual = instance.to_isize();
399
400                assert_eq!(expected, actual);
401            }
402        }
403
404        #[test]
405        fn TEST_RANGE_OF_isize_VALUES_IN_REF_Rc() {
406
407            const VALUES : &[isize] = &[
408                // insert list:
409                0,
410                1,
411                2,
412                4,
413                8,
414                16,
415                32,
416                64,
417                128,
418                256,
419                isize::MAX,
420            ];
421
422            for &value in VALUES {
423                let expected = value;
424                let instance = &std_rc::Rc::new(value);
425                let actual = instance.to_isize();
426
427                assert_eq!(expected, actual);
428            }
429        }
430    }
431}
432
433
434// ///////////////////////////// end of file //////////////////////////// //
435