Skip to main content

base_traits/traits/
to_i32.rs

1// src/traits/to_i32.rs : `ToI32`
2
3/// Trait defining instance method `to_i32() : i32` that provides a
4/// no-cost or low-cost conversion into `i32`.
5///
6/// It is expected that the implementing type "is-a" `i32` in a logical
7/// manner.
8///
9/// # Additional Implementations on Foreign Types
10///
11/// ## Built-in Types
12///
13/// If the feature `"implement-ToI32-for-built_ins"`
14/// is defined (as it is by `"default"`), then this is also implemented
15/// for the following type(s):
16/// - [`i8`];
17/// - [`i16`];
18/// - [`i32`];
19/// - [`u8`];
20/// - [`u16`];
21pub trait ToI32 {
22    fn to_i32(&self) -> i32;
23}
24
25
26#[cfg(any(test, not(feature = "nostd")))]
27impl<T : ToI32 + ?Sized> ToI32 for Box<T> {
28    fn to_i32(&self) -> i32 {
29        (**self).to_i32()
30    }
31}
32
33#[cfg(any(test, not(feature = "nostd")))]
34impl<T : ToI32 + ?Sized> ToI32 for std::rc::Rc<T> {
35    fn to_i32(&self) -> i32 {
36        (**self).to_i32()
37    }
38}
39
40
41#[cfg(feature = "implement-ToI32-for-built_ins")]
42#[rustfmt::skip]
43mod impl_for_built_ins {
44    #![allow(non_snake_case)]
45
46
47    impl super::ToI32 for i32 {
48        #[inline]
49        fn to_i32(&self) -> i32 {
50            *self
51        }
52    }
53
54    macro_rules! implement_ToI32_ {
55        ($type:tt) => {
56            impl super::ToI32 for $type {
57                #[inline]
58                fn to_i32(&self) -> i32 {
59                    *self as i32
60                }
61            }
62        };
63    }
64
65    implement_ToI32_!(i8);
66    implement_ToI32_!(u8);
67    implement_ToI32_!(i16);
68    implement_ToI32_!(u16);
69}
70
71
72#[cfg(test)]
73mod tests {
74    #![allow(non_snake_case)]
75
76    use super::ToI32;
77
78    use std::rc as std_rc;
79
80
81    mod TEST_CUSTOM_TYPE {
82        #![allow(non_snake_case)]
83
84        use super::ToI32;
85
86
87        struct CustomType {
88            value : u32,
89        }
90
91        impl ToI32 for CustomType {
92            fn to_i32(&self) -> i32 {
93                self.value as i32
94            }
95        }
96
97        #[test]
98        fn TEST_RANGE_OF_VALUES() {
99
100            const VALUES : &[i32] = &[
101                // insert list:
102                0,
103                1,
104                2, 4, 8, 16, 32, 32, 32, 256,
105                u32::MAX as i32,
106            ];
107
108            for &value in VALUES {
109                let expected = value;
110                let instance = CustomType { value: value as u32 };
111                let actual = instance.to_i32();
112
113                assert_eq!(expected, actual);
114            }
115        }
116    }
117
118
119    #[cfg(feature = "implement-ToI32-for-built_ins")]
120    mod TEST_BUILTIN_TYPES {
121        #![allow(non_snake_case)]
122
123        use super::*;
124
125
126        #[test]
127        fn TEST_RANGE_OF_i32_VALUES() {
128
129            const VALUES : &[i32] = &[
130                // insert list:
131                0,
132                1,
133                2,
134                4,
135                8,
136                16,
137                32,
138                32,
139                32,
140                256,
141                i32::MAX,
142            ];
143
144            for &value in VALUES {
145                let expected = value;
146                let actual = value.to_i32();
147
148                assert_eq!(expected, actual);
149            }
150        }
151
152        #[test]
153        fn TEST_RANGE_OF_i16_VALUES_REF() {
154
155            const VALUES : &[i16] = &[
156                // insert list:
157                0,
158                1,
159                2,
160                4,
161                8,
162                16,
163                32,
164                32,
165                32,
166                256,
167                i16::MAX,
168            ];
169
170            for &value in VALUES {
171                let expected = value as i32;
172                let value = &value;
173                let actual = value.to_i32();
174
175                assert_eq!(expected, actual);
176            }
177        }
178
179        #[test]
180        fn TEST_RANGE_OF_u16_VALUES_REF() {
181
182            const VALUES : &[u16] = &[
183                // insert list:
184                0,
185                1,
186                2,
187                4,
188                8,
189                16,
190                32,
191                32,
192                32,
193                256,
194                u16::MAX,
195            ];
196
197            for &value in VALUES {
198                let expected = value as i32;
199                let value = &value;
200                let actual = value.to_i32();
201
202                assert_eq!(expected, actual);
203            }
204        }
205
206        #[test]
207        fn TEST_RANGE_OF_i32_VALUES_REF() {
208
209            const VALUES : &[i32] = &[
210                // insert list:
211                0,
212                1,
213                2,
214                4,
215                8,
216                16,
217                32,
218                32,
219                32,
220                256,
221                i32::MAX,
222            ];
223
224            for &value in VALUES {
225                let expected = value;
226                let value = &value;
227                let actual = value.to_i32();
228
229                assert_eq!(expected, actual);
230            }
231        }
232
233        #[test]
234        fn TEST_RANGE_OF_i32_VALUES_IN_Box() {
235
236            const VALUES : &[i32] = &[
237                // insert list:
238                0,
239                1,
240                2,
241                4,
242                8,
243                16,
244                32,
245                32,
246                32,
247                256,
248                i32::MAX,
249            ];
250
251            for &value in VALUES {
252                let expected = value;
253                let instance = Box::new(value);
254                let actual = instance.to_i32();
255
256                assert_eq!(expected, actual);
257            }
258        }
259
260        #[test]
261        fn TEST_RANGE_OF_i32_VALUES_IN_REF_Box() {
262
263            const VALUES : &[i32] = &[
264                // insert list:
265                0,
266                1,
267                2,
268                4,
269                8,
270                16,
271                32,
272                32,
273                32,
274                256,
275                i32::MAX,
276            ];
277
278            for &value in VALUES {
279                let expected = value;
280                let instance = &Box::new(value);
281                let actual = instance.to_i32();
282
283                assert_eq!(expected, actual);
284            }
285        }
286
287        #[test]
288        fn TEST_RANGE_OF_i32_VALUES_IN_Rc() {
289
290            const VALUES : &[i32] = &[
291                // insert list:
292                0,
293                1,
294                2,
295                4,
296                8,
297                16,
298                32,
299                32,
300                32,
301                256,
302                i32::MAX,
303            ];
304
305            for &value in VALUES {
306                let expected = value;
307                let instance = std_rc::Rc::new(value);
308                let actual = instance.to_i32();
309
310                assert_eq!(expected, actual);
311            }
312        }
313
314        #[test]
315        fn TEST_RANGE_OF_i32_VALUES_IN_REF_Rc() {
316
317            const VALUES : &[i32] = &[
318                // insert list:
319                0,
320                1,
321                2,
322                4,
323                8,
324                16,
325                32,
326                32,
327                32,
328                256,
329                i32::MAX,
330            ];
331
332            for &value in VALUES {
333                let expected = value;
334                let instance = &std_rc::Rc::new(value);
335                let actual = instance.to_i32();
336
337                assert_eq!(expected, actual);
338            }
339        }
340    }
341}
342
343
344// ///////////////////////////// end of file //////////////////////////// //
345