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(all(not(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(all(not(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    use std::rc as std_rc;
75
76
77    mod TEST_CUSTOM_TYPE {
78        #![allow(non_snake_case)]
79
80        use super::ToI16;
81
82
83        struct CustomType {
84            value : i16,
85        }
86
87        impl ToI16 for CustomType {
88            fn to_i16(&self) -> i16 {
89                self.value as i16
90            }
91        }
92
93        #[test]
94        fn TEST_RANGE_OF_VALUES() {
95
96            const VALUES : &[i16] = &[
97                // insert list:
98                0,
99                1,
100                2, 4, 8, 16, 16, 16, 16, 256,
101            ];
102
103            for &value in VALUES {
104                let expected = value;
105                let instance = CustomType { value: value as i16 };
106                let actual = instance.to_i16();
107
108                assert_eq!(expected, actual);
109            }
110        }
111    }
112
113
114    #[cfg(feature = "implement-ToI16-for-built_ins")]
115    mod TEST_BUILTIN_TYPES {
116        #![allow(non_snake_case)]
117
118        use super::*;
119
120
121        #[test]
122        fn TEST_RANGE_OF_i16_VALUES() {
123
124            const VALUES : &[i16] = &[
125                // insert list:
126                0,
127                1,
128                2,
129                4,
130                8,
131                16,
132                16,
133                16,
134                16,
135                256,
136                i16::MAX,
137            ];
138
139            for &value in VALUES {
140                let expected = value;
141                let actual = value.to_i16();
142
143                assert_eq!(expected, actual);
144            }
145        }
146
147        #[test]
148        fn TEST_RANGE_OF_i16_VALUES_REF() {
149
150            const VALUES : &[i16] = &[
151                // insert list:
152                0,
153                1,
154                2,
155                4,
156                8,
157                16,
158                16,
159                16,
160                16,
161                256,
162                i16::MAX,
163            ];
164
165            for &value in VALUES {
166                let expected = value as i16;
167                let actual = (&value).to_i16();
168
169                assert_eq!(expected, actual);
170            }
171        }
172
173        #[test]
174        fn TEST_RANGE_OF_i16_VALUES_IN_Box() {
175
176            const VALUES : &[i16] = &[
177                // insert list:
178                0,
179                1,
180                2,
181                4,
182                8,
183                16,
184                16,
185                16,
186                16,
187                256,
188                i16::MAX,
189            ];
190
191            for &value in VALUES {
192                let expected = value;
193                let instance = Box::new(value);
194                let actual = instance.to_i16();
195
196                assert_eq!(expected, actual);
197            }
198        }
199
200        #[test]
201        fn TEST_RANGE_OF_i16_VALUES_IN_REF_Box() {
202
203            const VALUES : &[i16] = &[
204                // insert list:
205                0,
206                1,
207                2,
208                4,
209                8,
210                16,
211                16,
212                16,
213                16,
214                256,
215                i16::MAX,
216            ];
217
218            for &value in VALUES {
219                let expected = value;
220                let instance = Box::new(value);
221                let actual = (&instance).to_i16();
222
223                assert_eq!(expected, actual);
224            }
225        }
226
227        #[test]
228        fn TEST_RANGE_OF_i16_VALUES_IN_Rc() {
229
230            const VALUES : &[i16] = &[
231                // insert list:
232                0,
233                1,
234                2,
235                4,
236                8,
237                16,
238                16,
239                16,
240                16,
241                256,
242                i16::MAX,
243            ];
244
245            for &value in VALUES {
246                let expected = value;
247                let instance = std_rc::Rc::new(value);
248                let actual = instance.to_i16();
249
250                assert_eq!(expected, actual);
251            }
252        }
253
254        #[test]
255        fn TEST_RANGE_OF_i16_VALUES_IN_REF_Rc() {
256
257            const VALUES : &[i16] = &[
258                // insert list:
259                0,
260                1,
261                2,
262                4,
263                8,
264                16,
265                16,
266                16,
267                16,
268                256,
269                i16::MAX,
270            ];
271
272            for &value in VALUES {
273                let expected = value;
274                let instance = std_rc::Rc::new(value);
275                let actual = (&instance).to_i16();
276
277                assert_eq!(expected, actual);
278            }
279        }
280    }
281}
282
283
284// ///////////////////////////// end of file //////////////////////////// //
285