1pub trait ToISize {
27 fn to_isize(&self) -> isize;
28}
29
30
31#[cfg(all(not(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(all(not(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 #[cfg(any(
106 target_pointer_width = "128",
107 ))]
108 mod bits_128plus {
109 implement_ToISize_!(u64);
110 implement_ToISize_!(i128);
111 }
112}
113
114
115#[cfg(test)]
116mod tests {
117 #![allow(non_snake_case)]
118
119 use super::ToISize;
120
121 use std::rc as std_rc;
122
123
124 mod TEST_CUSTOM_TYPE {
125 #![allow(non_snake_case)]
126
127 use super::ToISize;
128
129
130 struct CustomType {
131 value : u64,
132 }
133
134 impl ToISize for CustomType {
135 fn to_isize(&self) -> isize {
136 self.value as isize
137 }
138 }
139
140 #[test]
141 fn TEST_RANGE_OF_VALUES() {
142
143 const VALUES : &[isize] = &[
144 0,
146 1,
147 2, 4, 8, 16, 32, 64, 128, 256,
148 u16::MAX as isize,
149 u32::MAX as isize,
150 u64::MAX as isize,
151 ];
152
153 for &value in VALUES {
154 let expected = value;
155 let instance = CustomType { value: value as u64 };
156 let actual = instance.to_isize();
157
158 assert_eq!(expected, actual);
159 }
160 }
161 }
162
163
164 #[cfg(feature = "implement-ToISize-for-built_ins")]
165 mod TEST_BUILTIN_TYPES {
166 #![allow(non_snake_case)]
167 #![allow(unexpected_cfgs)]
168
169 use super::*;
170
171
172 #[test]
173 fn TEST_RANGE_OF_isize_VALUES() {
174
175 const VALUES : &[isize] = &[
176 0,
178 1,
179 2,
180 4,
181 8,
182 16,
183 32,
184 64,
185 128,
186 256,
187 isize::MAX,
188 ];
189
190 for &value in VALUES {
191 let expected = value;
192 let actual = value.to_isize();
193
194 assert_eq!(expected, actual);
195 }
196 }
197
198 #[cfg(any(
199 target_pointer_width = "32",
200 target_pointer_width = "64",
201 target_pointer_width = "128",
202 ))]
203 #[test]
204 fn TEST_RANGE_OF_i16_VALUES_REF() {
205
206 const VALUES : &[i16] = &[
207 0,
209 1,
210 2,
211 4,
212 8,
213 16,
214 32,
215 64,
216 128,
217 256,
218 i16::MAX,
219 ];
220
221 for &value in VALUES {
222 let expected = value as isize;
223 let actual = (&value).to_isize();
224
225 assert_eq!(expected, actual);
226 }
227 }
228
229 #[cfg(any(
230 target_pointer_width = "16",
231 target_pointer_width = "32",
232 target_pointer_width = "64",
233 target_pointer_width = "128",
234 ))]
235 #[test]
236 fn TEST_RANGE_OF_u16_VALUES_REF() {
237
238 const VALUES : &[u16] = &[
239 0,
241 1,
242 2,
243 4,
244 8,
245 16,
246 32,
247 64,
248 128,
249 256,
250 u16::MAX,
251 ];
252
253 for &value in VALUES {
254 let expected = value as isize;
255 let actual = (&value).to_isize();
256
257 assert_eq!(expected, actual);
258 }
259 }
260
261 #[cfg(any(
262 target_pointer_width = "32",
263 target_pointer_width = "64",
264 target_pointer_width = "128",
265 ))]
266 #[test]
267 fn TEST_RANGE_OF_u32_VALUES_REF() {
268
269 const VALUES : &[u32] = &[
270 0,
272 1,
273 2,
274 4,
275 8,
276 16,
277 32,
278 64,
279 128,
280 256,
281 u32::MAX,
282 ];
283
284 for &value in VALUES {
285 let expected = value as isize;
286 let actual = (&value).to_isize();
287
288 assert_eq!(expected, actual);
289 }
290 }
291
292 #[test]
293 fn TEST_RANGE_OF_isize_VALUES_REF() {
294
295 const VALUES : &[isize] = &[
296 0,
298 1,
299 2,
300 4,
301 8,
302 16,
303 32,
304 64,
305 128,
306 256,
307 isize::MAX,
308 ];
309
310 for &value in VALUES {
311 let expected = value;
312 let actual = (&value).to_isize();
313
314 assert_eq!(expected, actual);
315 }
316 }
317
318 #[test]
319 fn TEST_RANGE_OF_isize_VALUES_IN_Box() {
320
321 const VALUES : &[isize] = &[
322 0,
324 1,
325 2,
326 4,
327 8,
328 16,
329 32,
330 64,
331 128,
332 256,
333 isize::MAX,
334 ];
335
336 for &value in VALUES {
337 let expected = value;
338 let instance = Box::new(value);
339 let actual = instance.to_isize();
340
341 assert_eq!(expected, actual);
342 }
343 }
344
345 #[test]
346 fn TEST_RANGE_OF_isize_VALUES_IN_REF_Box() {
347
348 const VALUES : &[isize] = &[
349 0,
351 1,
352 2,
353 4,
354 8,
355 16,
356 32,
357 64,
358 128,
359 256,
360 isize::MAX,
361 ];
362
363 for &value in VALUES {
364 let expected = value;
365 let instance = Box::new(value);
366 let actual = (&instance).to_isize();
367
368 assert_eq!(expected, actual);
369 }
370 }
371
372 #[test]
373 fn TEST_RANGE_OF_isize_VALUES_IN_Rc() {
374
375 const VALUES : &[isize] = &[
376 0,
378 1,
379 2,
380 4,
381 8,
382 16,
383 32,
384 64,
385 128,
386 256,
387 isize::MAX,
388 ];
389
390 for &value in VALUES {
391 let expected = value;
392 let instance = std_rc::Rc::new(value);
393 let actual = instance.to_isize();
394
395 assert_eq!(expected, actual);
396 }
397 }
398
399 #[test]
400 fn TEST_RANGE_OF_isize_VALUES_IN_REF_Rc() {
401
402 const VALUES : &[isize] = &[
403 0,
405 1,
406 2,
407 4,
408 8,
409 16,
410 32,
411 64,
412 128,
413 256,
414 isize::MAX,
415 ];
416
417 for &value in VALUES {
418 let expected = value;
419 let instance = std_rc::Rc::new(value);
420 let actual = (&instance).to_isize();
421
422 assert_eq!(expected, actual);
423 }
424 }
425 }
426}
427
428
429