Skip to main content

base_traits/traits/
to_i16.rs

1// src/traits/to_i16.rs : `ToI16`
2
3/// Trait defining instance method `to_i16() : i16` that provides a
4/// no-cost or low-cost conversion into `i16`.
5///
6/// It is expected that the implementing type "is-a" `i16` in a logical
7/// manner.
8///
9/// # Additional Implementations on Foreign Types
10///
11/// ## Built-in Types
12///
13/// If the feature `"implement-ToI16-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/// - [`u8`];
19pub trait ToI16 {
20    fn to_i16(&self) -> i16;
21}
22
23
24#[cfg(any(test, not(feature = "nostd")))]
25impl<T : ToI16 + ?Sized> ToI16 for Box<T> {
26    fn to_i16(&self) -> i16 {
27        (**self).to_i16()
28    }
29}
30
31#[cfg(any(test, not(feature = "nostd")))]
32impl<T : ToI16 + ?Sized> ToI16 for std::rc::Rc<T> {
33    fn to_i16(&self) -> i16 {
34        (**self).to_i16()
35    }
36}
37
38
39#[cfg(feature = "implement-ToI16-for-built_ins")]
40#[rustfmt::skip]
41mod impl_for_built_ins {
42    #![allow(non_snake_case)]
43
44
45    impl super::ToI16 for i16 {
46        #[inline]
47        fn to_i16(&self) -> i16 {
48            *self
49        }
50    }
51
52    macro_rules! implement_ToI16_ {
53        ($type:tt) => {
54            impl super::ToI16 for $type {
55                #[inline]
56                fn to_i16(&self) -> i16 {
57                    *self as i16
58                }
59            }
60        };
61    }
62
63    implement_ToI16_!(i8);
64    implement_ToI16_!(u8);
65}
66
67
68#[cfg(test)]
69mod tests {
70    #![allow(non_snake_case)]
71
72    use super::ToI16;
73
74    #[allow(unused_imports)]
75    use std::rc as std_rc;
76
77
78    mod TEST_CUSTOM_TYPE {
79        #![allow(clippy::redundant_field_names)]
80        #![allow(non_snake_case)]
81
82        use super::ToI16;
83
84
85        struct CustomType {
86            value : i16,
87        }
88
89        impl ToI16 for CustomType {
90            fn to_i16(&self) -> i16 {
91                self.value
92            }
93        }
94
95        #[test]
96        fn TEST_RANGE_OF_VALUES() {
97            const VALUES : &[i16] = &[
98                // insert list:
99                0,
100                1,
101                2,
102                4,
103                8,
104                16,
105                16,
106                16,
107                16,
108                256,
109            ];
110
111            for &value in VALUES {
112                let expected = value;
113                let instance = CustomType { value: value };
114                let actual = instance.to_i16();
115
116                assert_eq!(expected, actual);
117            }
118        }
119    }
120
121
122    #[cfg(feature = "implement-ToI16-for-built_ins")]
123    mod TEST_BUILTIN_TYPES {
124        #![allow(non_snake_case)]
125
126        use super::*;
127
128
129        #[test]
130        fn TEST_RANGE_OF_i16_VALUES() {
131            const VALUES : &[i16] = &[
132                // insert list:
133                0,
134                1,
135                2,
136                4,
137                8,
138                16,
139                16,
140                16,
141                16,
142                256,
143                i16::MAX,
144            ];
145
146            for &value in VALUES {
147                let expected = value;
148                let actual = value.to_i16();
149
150                assert_eq!(expected, actual);
151            }
152        }
153
154        #[test]
155        fn TEST_RANGE_OF_i16_VALUES_REF() {
156            const VALUES : &[i16] = &[
157                // insert list:
158                0,
159                1,
160                2,
161                4,
162                8,
163                16,
164                16,
165                16,
166                16,
167                256,
168                i16::MAX,
169            ];
170
171            for &value in VALUES {
172                let expected = value;
173                let value = &value;
174                let actual = value.to_i16();
175
176                assert_eq!(expected, actual);
177            }
178        }
179
180        #[test]
181        fn TEST_RANGE_OF_i16_VALUES_IN_Box() {
182            const VALUES : &[i16] = &[
183                // insert list:
184                0,
185                1,
186                2,
187                4,
188                8,
189                16,
190                16,
191                16,
192                16,
193                256,
194                i16::MAX,
195            ];
196
197            for &value in VALUES {
198                let expected = value;
199                let instance = Box::new(value);
200                let actual = instance.to_i16();
201
202                assert_eq!(expected, actual);
203            }
204        }
205
206        #[test]
207        fn TEST_RANGE_OF_i16_VALUES_IN_REF_Box() {
208            const VALUES : &[i16] = &[
209                // insert list:
210                0,
211                1,
212                2,
213                4,
214                8,
215                16,
216                16,
217                16,
218                16,
219                256,
220                i16::MAX,
221            ];
222
223            for &value in VALUES {
224                let expected = value;
225                let instance = &Box::new(value);
226                let actual = instance.to_i16();
227
228                assert_eq!(expected, actual);
229            }
230        }
231
232        #[test]
233        fn TEST_RANGE_OF_i16_VALUES_IN_Rc() {
234            const VALUES : &[i16] = &[
235                // insert list:
236                0,
237                1,
238                2,
239                4,
240                8,
241                16,
242                16,
243                16,
244                16,
245                256,
246                i16::MAX,
247            ];
248
249            for &value in VALUES {
250                let expected = value;
251                let instance = std_rc::Rc::new(value);
252                let actual = instance.to_i16();
253
254                assert_eq!(expected, actual);
255            }
256        }
257
258        #[test]
259        fn TEST_RANGE_OF_i16_VALUES_IN_REF_Rc() {
260            const VALUES : &[i16] = &[
261                // insert list:
262                0,
263                1,
264                2,
265                4,
266                8,
267                16,
268                16,
269                16,
270                16,
271                256,
272                i16::MAX,
273            ];
274
275            for &value in VALUES {
276                let expected = value;
277                let instance = &std_rc::Rc::new(value);
278                let actual = instance.to_i16();
279
280                assert_eq!(expected, actual);
281            }
282        }
283    }
284}
285
286
287// ///////////////////////////// end of file //////////////////////////// //