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(all(not(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(all(not(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 actual = (&value).to_i32();
173
174                assert_eq!(expected, actual);
175            }
176        }
177
178        #[test]
179        fn TEST_RANGE_OF_u16_VALUES_REF() {
180
181            const VALUES : &[u16] = &[
182                // insert list:
183                0,
184                1,
185                2,
186                4,
187                8,
188                16,
189                32,
190                32,
191                32,
192                256,
193                u16::MAX,
194            ];
195
196            for &value in VALUES {
197                let expected = value as i32;
198                let actual = (&value).to_i32();
199
200                assert_eq!(expected, actual);
201            }
202        }
203
204        #[test]
205        fn TEST_RANGE_OF_i32_VALUES_REF() {
206
207            const VALUES : &[i32] = &[
208                // insert list:
209                0,
210                1,
211                2,
212                4,
213                8,
214                16,
215                32,
216                32,
217                32,
218                256,
219                i32::MAX,
220            ];
221
222            for &value in VALUES {
223                let expected = value;
224                let actual = (&value).to_i32();
225
226                assert_eq!(expected, actual);
227            }
228        }
229
230        #[test]
231        fn TEST_RANGE_OF_i32_VALUES_IN_Box() {
232
233            const VALUES : &[i32] = &[
234                // insert list:
235                0,
236                1,
237                2,
238                4,
239                8,
240                16,
241                32,
242                32,
243                32,
244                256,
245                i32::MAX,
246            ];
247
248            for &value in VALUES {
249                let expected = value;
250                let instance = Box::new(value);
251                let actual = instance.to_i32();
252
253                assert_eq!(expected, actual);
254            }
255        }
256
257        #[test]
258        fn TEST_RANGE_OF_i32_VALUES_IN_REF_Box() {
259
260            const VALUES : &[i32] = &[
261                // insert list:
262                0,
263                1,
264                2,
265                4,
266                8,
267                16,
268                32,
269                32,
270                32,
271                256,
272                i32::MAX,
273            ];
274
275            for &value in VALUES {
276                let expected = value;
277                let instance = Box::new(value);
278                let actual = (&instance).to_i32();
279
280                assert_eq!(expected, actual);
281            }
282        }
283
284        #[test]
285        fn TEST_RANGE_OF_i32_VALUES_IN_Rc() {
286
287            const VALUES : &[i32] = &[
288                // insert list:
289                0,
290                1,
291                2,
292                4,
293                8,
294                16,
295                32,
296                32,
297                32,
298                256,
299                i32::MAX,
300            ];
301
302            for &value in VALUES {
303                let expected = value;
304                let instance = std_rc::Rc::new(value);
305                let actual = instance.to_i32();
306
307                assert_eq!(expected, actual);
308            }
309        }
310
311        #[test]
312        fn TEST_RANGE_OF_i32_VALUES_IN_REF_Rc() {
313
314            const VALUES : &[i32] = &[
315                // insert list:
316                0,
317                1,
318                2,
319                4,
320                8,
321                16,
322                32,
323                32,
324                32,
325                256,
326                i32::MAX,
327            ];
328
329            for &value in VALUES {
330                let expected = value;
331                let instance = std_rc::Rc::new(value);
332                let actual = (&instance).to_i32();
333
334                assert_eq!(expected, actual);
335            }
336        }
337    }
338}
339
340
341// ///////////////////////////// end of file //////////////////////////// //
342