1pub 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 #[allow(unused_imports)]
79 use std::rc as std_rc;
80
81
82 mod TEST_CUSTOM_TYPE {
83 #![allow(non_snake_case)]
84
85 use super::ToI32;
86
87
88 struct CustomType {
89 value : u32,
90 }
91
92 impl ToI32 for CustomType {
93 fn to_i32(&self) -> i32 {
94 self.value as i32
95 }
96 }
97
98 #[test]
99 fn TEST_RANGE_OF_VALUES() {
100 const VALUES : &[i32] = &[
101 0,
103 1,
104 2,
105 4,
106 8,
107 16,
108 32,
109 32,
110 32,
111 256,
112 u32::MAX as i32,
113 ];
114
115 for &value in VALUES {
116 let expected = value;
117 let instance = CustomType { value: value as u32 };
118 let actual = instance.to_i32();
119
120 assert_eq!(expected, actual);
121 }
122 }
123 }
124
125
126 #[cfg(feature = "implement-ToI32-for-built_ins")]
127 mod TEST_BUILTIN_TYPES {
128 #![allow(non_snake_case)]
129
130 use super::*;
131
132
133 #[test]
134 fn TEST_RANGE_OF_i32_VALUES() {
135 const VALUES : &[i32] = &[
136 0,
138 1,
139 2,
140 4,
141 8,
142 16,
143 32,
144 32,
145 32,
146 256,
147 i32::MAX,
148 ];
149
150 for &value in VALUES {
151 let expected = value;
152 let actual = value.to_i32();
153
154 assert_eq!(expected, actual);
155 }
156 }
157
158 #[test]
159 fn TEST_RANGE_OF_i16_VALUES_REF() {
160 const VALUES : &[i16] = &[
161 0,
163 1,
164 2,
165 4,
166 8,
167 16,
168 32,
169 32,
170 32,
171 256,
172 i16::MAX,
173 ];
174
175 for &value in VALUES {
176 let expected = value as i32;
177 let value = &value;
178 let actual = value.to_i32();
179
180 assert_eq!(expected, actual);
181 }
182 }
183
184 #[test]
185 fn TEST_RANGE_OF_u16_VALUES_REF() {
186 const VALUES : &[u16] = &[
187 0,
189 1,
190 2,
191 4,
192 8,
193 16,
194 32,
195 32,
196 32,
197 256,
198 u16::MAX,
199 ];
200
201 for &value in VALUES {
202 let expected = value as i32;
203 let value = &value;
204 let actual = value.to_i32();
205
206 assert_eq!(expected, actual);
207 }
208 }
209
210 #[test]
211 fn TEST_RANGE_OF_i32_VALUES_REF() {
212 const VALUES : &[i32] = &[
213 0,
215 1,
216 2,
217 4,
218 8,
219 16,
220 32,
221 32,
222 32,
223 256,
224 i32::MAX,
225 ];
226
227 for &value in VALUES {
228 let expected = value;
229 let value = &value;
230 let actual = value.to_i32();
231
232 assert_eq!(expected, actual);
233 }
234 }
235
236 #[test]
237 fn TEST_RANGE_OF_i32_VALUES_IN_Box() {
238 const VALUES : &[i32] = &[
239 0,
241 1,
242 2,
243 4,
244 8,
245 16,
246 32,
247 32,
248 32,
249 256,
250 i32::MAX,
251 ];
252
253 for &value in VALUES {
254 let expected = value;
255 let instance = Box::new(value);
256 let actual = instance.to_i32();
257
258 assert_eq!(expected, actual);
259 }
260 }
261
262 #[test]
263 fn TEST_RANGE_OF_i32_VALUES_IN_REF_Box() {
264 const VALUES : &[i32] = &[
265 0,
267 1,
268 2,
269 4,
270 8,
271 16,
272 32,
273 32,
274 32,
275 256,
276 i32::MAX,
277 ];
278
279 for &value in VALUES {
280 let expected = value;
281 let instance = &Box::new(value);
282 let actual = instance.to_i32();
283
284 assert_eq!(expected, actual);
285 }
286 }
287
288 #[test]
289 fn TEST_RANGE_OF_i32_VALUES_IN_Rc() {
290 const VALUES : &[i32] = &[
291 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 const VALUES : &[i32] = &[
317 0,
319 1,
320 2,
321 4,
322 8,
323 16,
324 32,
325 32,
326 32,
327 256,
328 i32::MAX,
329 ];
330
331 for &value in VALUES {
332 let expected = value;
333 let instance = &std_rc::Rc::new(value);
334 let actual = instance.to_i32();
335
336 assert_eq!(expected, actual);
337 }
338 }
339 }
340}
341
342
343