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    use std::rc as std_rc;
58
59
60    mod TEST_CUSTOM_TYPE {
61        #![allow(non_snake_case)]
62
63        use super::*;
64
65
66        struct CustomType {
67            value : u64,
68        }
69
70        impl AsISize for CustomType {
71            fn as_isize(&self) -> isize {
72                self.value as isize
73            }
74        }
75
76
77        #[test]
78        fn TEST_RANGE_OF_VALUES() {
79
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
109            const VALUES : &[isize] = &[
110                // insert list:
111                0,
112                1,
113                2,
114                4,
115                8,
116                16,
117                32,
118                64,
119                128,
120                256,
121                u32::MAX as isize,
122            ];
123
124            for &value in VALUES {
125                let expected = value;
126                let instance = Box::new(CustomType { value: value as u64 });
127                let actual = instance.as_isize();
128
129                assert_eq!(expected, actual);
130            }
131        }
132    }
133
134
135    #[cfg(feature = "implement-AsISize-for-built_ins")]
136    mod TEST_BUILTIN_TYPES {
137        #![allow(non_snake_case)]
138
139        use super::*;
140
141
142        #[test]
143        fn TEST_RANGE_OF_VALUES() {
144
145            const VALUES : &[isize] = &[
146                // insert list:
147                0,
148                1,
149                2,
150                4,
151                8,
152                16,
153                32,
154                64,
155                128,
156                256,
157                isize::MAX,
158            ];
159
160            for &value in VALUES {
161                let expected = value;
162                let actual = value.as_isize();
163
164                assert_eq!(expected, actual);
165            }
166        }
167
168        #[test]
169        fn TEST_RANGE_OF_VALUES_REF() {
170
171            const VALUES : &[isize] = &[
172                // insert list:
173                0,
174                1,
175                2,
176                4,
177                8,
178                16,
179                32,
180                64,
181                128,
182                256,
183                isize::MAX,
184            ];
185
186            for &value in VALUES {
187                let expected = value;
188                let value = &value;
189                let actual = value.as_isize();
190
191                assert_eq!(expected, actual);
192            }
193        }
194
195        #[test]
196        fn TEST_RANGE_OF_VALUES_IN_Box() {
197
198            const VALUES : &[isize] = &[
199                // insert list:
200                0,
201                1,
202                2,
203                4,
204                8,
205                16,
206                32,
207                64,
208                128,
209                256,
210                isize::MAX,
211            ];
212
213            for &value in VALUES {
214                let expected = value;
215                let instance = Box::new(value);
216                let actual = instance.as_isize();
217
218                assert_eq!(expected, actual);
219            }
220        }
221
222        #[test]
223        fn TEST_RANGE_OF_VALUES_IN_REF_Box() {
224
225            const VALUES : &[isize] = &[
226                // insert list:
227                0,
228                1,
229                2,
230                4,
231                8,
232                16,
233                32,
234                64,
235                128,
236                256,
237                isize::MAX,
238            ];
239
240            for &value in VALUES {
241                let expected = value;
242                let instance = &Box::new(value);
243                let actual = instance.as_isize();
244
245                assert_eq!(expected, actual);
246            }
247        }
248
249        #[test]
250        fn TEST_RANGE_OF_VALUES_IN_Rc() {
251
252            const VALUES : &[isize] = &[
253                // insert list:
254                0,
255                1,
256                2,
257                4,
258                8,
259                16,
260                32,
261                64,
262                128,
263                256,
264                isize::MAX,
265            ];
266
267            for &value in VALUES {
268                let expected = value;
269                let instance = std_rc::Rc::new(value);
270                let actual = instance.as_isize();
271
272                assert_eq!(expected, actual);
273            }
274        }
275
276        #[test]
277        fn TEST_RANGE_OF_VALUES_IN_REF_Rc() {
278
279            const VALUES : &[isize] = &[
280                // insert list:
281                0,
282                1,
283                2,
284                4,
285                8,
286                16,
287                32,
288                64,
289                128,
290                256,
291                isize::MAX,
292            ];
293
294            for &value in VALUES {
295                let expected = value;
296                let instance = &std_rc::Rc::new(value);
297                let actual = instance.as_isize();
298
299                assert_eq!(expected, actual);
300            }
301        }
302    }
303}
304
305
306// ///////////////////////////// end of file //////////////////////////// //
307