Skip to main content

base_traits/traits/
as_isize.rs

1// src/traits/as_isize.rs : `AsISize`
2
3/// Trait defining instance method `as_isize() : isize` that provides a
4/// cost-free conversion into `isize`.
5///
6/// It is expected that the implementing type "is-a" `isize` in a direct
7/// manner as well as in a logical manner.
8///
9/// # Additional Implementations on Foreign Types
10///
11/// ## Built-in Types
12///
13/// If the feature `"implement-AsISize-for-built_ins"`
14/// is defined (as it is by `"default"`), then this is also implemented
15/// for the following type(s):
16/// - [`isize`];
17pub trait AsISize {
18    fn as_isize(&self) -> isize;
19}
20
21
22#[cfg(any(test, not(feature = "nostd")))]
23impl<T : AsISize + ?Sized> AsISize for Box<T> {
24    fn as_isize(&self) -> isize {
25        (**self).as_isize()
26    }
27}
28
29#[cfg(any(test, not(feature = "nostd")))]
30impl<T : AsISize + ?Sized> AsISize for std::rc::Rc<T> {
31    fn as_isize(&self) -> isize {
32        (**self).as_isize()
33    }
34}
35
36
37#[cfg(feature = "implement-AsISize-for-built_ins")]
38mod impl_for_built_ins {
39    #![allow(non_snake_case)]
40
41
42    impl super::AsISize for isize {
43        #[inline]
44        fn as_isize(&self) -> isize {
45            *self
46        }
47    }
48}
49
50
51#[cfg(test)]
52mod tests {
53    #![allow(non_snake_case)]
54
55    use super::AsISize;
56
57    #[allow(unused_imports)]
58    use std::rc as std_rc;
59
60
61    mod TEST_CUSTOM_TYPE {
62        #![allow(non_snake_case)]
63
64        use super::*;
65
66
67        struct CustomType {
68            value : u64,
69        }
70
71        impl AsISize for CustomType {
72            fn as_isize(&self) -> isize {
73                self.value as isize
74            }
75        }
76
77
78        #[test]
79        fn TEST_RANGE_OF_VALUES() {
80            const VALUES : &[isize] = &[
81                // insert list:
82                0,
83                1,
84                2,
85                4,
86                8,
87                16,
88                32,
89                64,
90                128,
91                256,
92                u16::MAX as isize,
93                u32::MAX as isize,
94                u64::MAX as isize,
95            ];
96
97            for &value in VALUES {
98                let expected = value;
99                let instance = CustomType { value: value as u64 };
100                let actual = instance.as_isize();
101
102                assert_eq!(expected, actual);
103            }
104        }
105
106        #[test]
107        fn TEST_RANGE_OF_VALUES_IN_Box() {
108            const VALUES : &[isize] = &[
109                // insert list:
110                0,
111                1,
112                2,
113                4,
114                8,
115                16,
116                32,
117                64,
118                128,
119                256,
120                u32::MAX as isize,
121            ];
122
123            for &value in VALUES {
124                let expected = value;
125                let instance = Box::new(CustomType { value: value as u64 });
126                let actual = instance.as_isize();
127
128                assert_eq!(expected, actual);
129            }
130        }
131    }
132
133
134    #[cfg(feature = "implement-AsISize-for-built_ins")]
135    mod TEST_BUILTIN_TYPES {
136        #![allow(non_snake_case)]
137
138        use super::*;
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                isize::MAX,
156            ];
157
158            for &value in VALUES {
159                let expected = value;
160                let actual = value.as_isize();
161
162                assert_eq!(expected, actual);
163            }
164        }
165
166        #[test]
167        fn TEST_RANGE_OF_VALUES_REF() {
168            const VALUES : &[isize] = &[
169                // insert list:
170                0,
171                1,
172                2,
173                4,
174                8,
175                16,
176                32,
177                64,
178                128,
179                256,
180                isize::MAX,
181            ];
182
183            for &value in VALUES {
184                let expected = value;
185                let value = &value;
186                let actual = value.as_isize();
187
188                assert_eq!(expected, actual);
189            }
190        }
191
192        #[test]
193        fn TEST_RANGE_OF_VALUES_IN_Box() {
194            const VALUES : &[isize] = &[
195                // insert list:
196                0,
197                1,
198                2,
199                4,
200                8,
201                16,
202                32,
203                64,
204                128,
205                256,
206                isize::MAX,
207            ];
208
209            for &value in VALUES {
210                let expected = value;
211                let instance = Box::new(value);
212                let actual = instance.as_isize();
213
214                assert_eq!(expected, actual);
215            }
216        }
217
218        #[test]
219        fn TEST_RANGE_OF_VALUES_IN_REF_Box() {
220            const VALUES : &[isize] = &[
221                // insert list:
222                0,
223                1,
224                2,
225                4,
226                8,
227                16,
228                32,
229                64,
230                128,
231                256,
232                isize::MAX,
233            ];
234
235            for &value in VALUES {
236                let expected = value;
237                let instance = &Box::new(value);
238                let actual = instance.as_isize();
239
240                assert_eq!(expected, actual);
241            }
242        }
243
244        #[test]
245        fn TEST_RANGE_OF_VALUES_IN_Rc() {
246            const VALUES : &[isize] = &[
247                // insert list:
248                0,
249                1,
250                2,
251                4,
252                8,
253                16,
254                32,
255                64,
256                128,
257                256,
258                isize::MAX,
259            ];
260
261            for &value in VALUES {
262                let expected = value;
263                let instance = std_rc::Rc::new(value);
264                let actual = instance.as_isize();
265
266                assert_eq!(expected, actual);
267            }
268        }
269
270        #[test]
271        fn TEST_RANGE_OF_VALUES_IN_REF_Rc() {
272            const VALUES : &[isize] = &[
273                // insert list:
274                0,
275                1,
276                2,
277                4,
278                8,
279                16,
280                32,
281                64,
282                128,
283                256,
284                isize::MAX,
285            ];
286
287            for &value in VALUES {
288                let expected = value;
289                let instance = &std_rc::Rc::new(value);
290                let actual = instance.as_isize();
291
292                assert_eq!(expected, actual);
293            }
294        }
295    }
296}
297
298
299// ///////////////////////////// end of file //////////////////////////// //