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            const VALUES : &[i32] = &[
100                // insert list:
101                0,
102                1,
103                2,
104                4,
105                8,
106                16,
107                32,
108                32,
109                32,
110                256,
111                u32::MAX as i32,
112            ];
113
114            for &value in VALUES {
115                let expected = value;
116                let instance = CustomType { value: value as u32 };
117                let actual = instance.to_i32();
118
119                assert_eq!(expected, actual);
120            }
121        }
122    }
123
124
125    #[cfg(feature = "implement-ToI32-for-built_ins")]
126    mod TEST_BUILTIN_TYPES {
127        #![allow(non_snake_case)]
128
129        use super::*;
130
131
132        #[test]
133        fn TEST_RANGE_OF_i32_VALUES() {
134            const VALUES : &[i32] = &[
135                // insert list:
136                0,
137                1,
138                2,
139                4,
140                8,
141                16,
142                32,
143                32,
144                32,
145                256,
146                i32::MAX,
147            ];
148
149            for &value in VALUES {
150                let expected = value;
151                let actual = value.to_i32();
152
153                assert_eq!(expected, actual);
154            }
155        }
156
157        #[test]
158        fn TEST_RANGE_OF_i16_VALUES_REF() {
159            const VALUES : &[i16] = &[
160                // insert list:
161                0,
162                1,
163                2,
164                4,
165                8,
166                16,
167                32,
168                32,
169                32,
170                256,
171                i16::MAX,
172            ];
173
174            for &value in VALUES {
175                let expected = value as i32;
176                let value = &value;
177                let actual = value.to_i32();
178
179                assert_eq!(expected, actual);
180            }
181        }
182
183        #[test]
184        fn TEST_RANGE_OF_u16_VALUES_REF() {
185            const VALUES : &[u16] = &[
186                // insert list:
187                0,
188                1,
189                2,
190                4,
191                8,
192                16,
193                32,
194                32,
195                32,
196                256,
197                u16::MAX,
198            ];
199
200            for &value in VALUES {
201                let expected = value as i32;
202                let value = &value;
203                let actual = value.to_i32();
204
205                assert_eq!(expected, actual);
206            }
207        }
208
209        #[test]
210        fn TEST_RANGE_OF_i32_VALUES_REF() {
211            const VALUES : &[i32] = &[
212                // insert list:
213                0,
214                1,
215                2,
216                4,
217                8,
218                16,
219                32,
220                32,
221                32,
222                256,
223                i32::MAX,
224            ];
225
226            for &value in VALUES {
227                let expected = value;
228                let value = &value;
229                let actual = value.to_i32();
230
231                assert_eq!(expected, actual);
232            }
233        }
234
235        #[test]
236        fn TEST_RANGE_OF_i32_VALUES_IN_Box() {
237            const VALUES : &[i32] = &[
238                // insert list:
239                0,
240                1,
241                2,
242                4,
243                8,
244                16,
245                32,
246                32,
247                32,
248                256,
249                i32::MAX,
250            ];
251
252            for &value in VALUES {
253                let expected = value;
254                let instance = Box::new(value);
255                let actual = instance.to_i32();
256
257                assert_eq!(expected, actual);
258            }
259        }
260
261        #[test]
262        fn TEST_RANGE_OF_i32_VALUES_IN_REF_Box() {
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            const VALUES : &[i32] = &[
290                // insert list:
291                0,
292                1,
293                2,
294                4,
295                8,
296                16,
297                32,
298                32,
299                32,
300                256,
301                i32::MAX,
302            ];
303
304            for &value in VALUES {
305                let expected = value;
306                let instance = std_rc::Rc::new(value);
307                let actual = instance.to_i32();
308
309                assert_eq!(expected, actual);
310            }
311        }
312
313        #[test]
314        fn TEST_RANGE_OF_i32_VALUES_IN_REF_Rc() {
315            const VALUES : &[i32] = &[
316                // insert list:
317                0,
318                1,
319                2,
320                4,
321                8,
322                16,
323                32,
324                32,
325                32,
326                256,
327                i32::MAX,
328            ];
329
330            for &value in VALUES {
331                let expected = value;
332                let instance = &std_rc::Rc::new(value);
333                let actual = instance.to_i32();
334
335                assert_eq!(expected, actual);
336            }
337        }
338    }
339}
340
341
342// ///////////////////////////// end of file //////////////////////////// //