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(all(not(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(all(not(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 actual = (&value).as_isize();
189
190                assert_eq!(expected, actual);
191            }
192        }
193
194        #[test]
195        fn TEST_RANGE_OF_VALUES_IN_Box() {
196
197            const VALUES : &[isize] = &[
198                // insert list:
199                0,
200                1,
201                2,
202                4,
203                8,
204                16,
205                32,
206                64,
207                128,
208                256,
209                isize::MAX,
210            ];
211
212            for &value in VALUES {
213                let expected = value;
214                let instance = Box::new(value);
215                let actual = instance.as_isize();
216
217                assert_eq!(expected, actual);
218            }
219        }
220
221        #[test]
222        fn TEST_RANGE_OF_VALUES_IN_REF_Box() {
223
224            const VALUES : &[isize] = &[
225                // insert list:
226                0,
227                1,
228                2,
229                4,
230                8,
231                16,
232                32,
233                64,
234                128,
235                256,
236                isize::MAX,
237            ];
238
239            for &value in VALUES {
240                let expected = value;
241                let instance = Box::new(value);
242                let actual = (&instance).as_isize();
243
244                assert_eq!(expected, actual);
245            }
246        }
247
248        #[test]
249        fn TEST_RANGE_OF_VALUES_IN_Rc() {
250
251            const VALUES : &[isize] = &[
252                // insert list:
253                0,
254                1,
255                2,
256                4,
257                8,
258                16,
259                32,
260                64,
261                128,
262                256,
263                isize::MAX,
264            ];
265
266            for &value in VALUES {
267                let expected = value;
268                let instance = std_rc::Rc::new(value);
269                let actual = instance.as_isize();
270
271                assert_eq!(expected, actual);
272            }
273        }
274
275        #[test]
276        fn TEST_RANGE_OF_VALUES_IN_REF_Rc() {
277
278            const VALUES : &[isize] = &[
279                // insert list:
280                0,
281                1,
282                2,
283                4,
284                8,
285                16,
286                32,
287                64,
288                128,
289                256,
290                isize::MAX,
291            ];
292
293            for &value in VALUES {
294                let expected = value;
295                let instance = std_rc::Rc::new(value);
296                let actual = (&instance).as_isize();
297
298                assert_eq!(expected, actual);
299            }
300        }
301    }
302}
303
304
305// ///////////////////////////// end of file //////////////////////////// //
306