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