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