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            const VALUES : &[i16] = &[
97                // insert list:
98                0,
99                1,
100                2,
101                4,
102                8,
103                16,
104                16,
105                16,
106                16,
107                256,
108            ];
109
110            for &value in VALUES {
111                let expected = value;
112                let instance = CustomType { value: value };
113                let actual = instance.to_i16();
114
115                assert_eq!(expected, actual);
116            }
117        }
118    }
119
120
121    #[cfg(feature = "implement-ToI16-for-built_ins")]
122    mod TEST_BUILTIN_TYPES {
123        #![allow(non_snake_case)]
124
125        use super::*;
126
127
128        #[test]
129        fn TEST_RANGE_OF_i16_VALUES() {
130            const VALUES : &[i16] = &[
131                // insert list:
132                0,
133                1,
134                2,
135                4,
136                8,
137                16,
138                16,
139                16,
140                16,
141                256,
142                i16::MAX,
143            ];
144
145            for &value in VALUES {
146                let expected = value;
147                let actual = value.to_i16();
148
149                assert_eq!(expected, actual);
150            }
151        }
152
153        #[test]
154        fn TEST_RANGE_OF_i16_VALUES_REF() {
155            const VALUES : &[i16] = &[
156                // insert list:
157                0,
158                1,
159                2,
160                4,
161                8,
162                16,
163                16,
164                16,
165                16,
166                256,
167                i16::MAX,
168            ];
169
170            for &value in VALUES {
171                let expected = value;
172                let value = &value;
173                let actual = value.to_i16();
174
175                assert_eq!(expected, actual);
176            }
177        }
178
179        #[test]
180        fn TEST_RANGE_OF_i16_VALUES_IN_Box() {
181            const VALUES : &[i16] = &[
182                // insert list:
183                0,
184                1,
185                2,
186                4,
187                8,
188                16,
189                16,
190                16,
191                16,
192                256,
193                i16::MAX,
194            ];
195
196            for &value in VALUES {
197                let expected = value;
198                let instance = Box::new(value);
199                let actual = instance.to_i16();
200
201                assert_eq!(expected, actual);
202            }
203        }
204
205        #[test]
206        fn TEST_RANGE_OF_i16_VALUES_IN_REF_Box() {
207            const VALUES : &[i16] = &[
208                // insert list:
209                0,
210                1,
211                2,
212                4,
213                8,
214                16,
215                16,
216                16,
217                16,
218                256,
219                i16::MAX,
220            ];
221
222            for &value in VALUES {
223                let expected = value;
224                let instance = &Box::new(value);
225                let actual = instance.to_i16();
226
227                assert_eq!(expected, actual);
228            }
229        }
230
231        #[test]
232        fn TEST_RANGE_OF_i16_VALUES_IN_Rc() {
233            const VALUES : &[i16] = &[
234                // insert list:
235                0,
236                1,
237                2,
238                4,
239                8,
240                16,
241                16,
242                16,
243                16,
244                256,
245                i16::MAX,
246            ];
247
248            for &value in VALUES {
249                let expected = value;
250                let instance = std_rc::Rc::new(value);
251                let actual = instance.to_i16();
252
253                assert_eq!(expected, actual);
254            }
255        }
256
257        #[test]
258        fn TEST_RANGE_OF_i16_VALUES_IN_REF_Rc() {
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 //////////////////////////// //