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