1 //! Utilities for Rust primitives.
2 
3 use crate::lib::fmt;
4 use super::cast::{AsCast, TryCast};
5 
6 /// Type that can be converted to primitive with `as`.
7 pub trait AsPrimitive: Copy + PartialEq + PartialOrd + Send + Sync {
8     fn as_u8(self) -> u8;
9     fn as_u16(self) -> u16;
10     fn as_u32(self) -> u32;
11     fn as_u64(self) -> u64;
12     fn as_u128(self) -> u128;
icache_status(void)13     fn as_usize(self) -> usize;
14     fn as_i8(self) -> i8;
15     fn as_i16(self) -> i16;
16     fn as_i32(self) -> i32;
17     fn as_i64(self) -> i64;
icache_enable(void)18     fn as_i128(self) -> i128;
19     fn as_isize(self) -> isize;
20     fn as_f32(self) -> f32;
21     fn as_f64(self) -> f64;
22 }
23 
24 macro_rules! as_primitive {
icache_disable(void)25     ($($t:ty)*) => ($(
26         impl AsPrimitive for $t {
27             #[inline]
28             fn as_u8(self) -> u8 {
29                 self as u8
30             }
31 
32             #[inline]
33             fn as_u16(self) -> u16 {
34                 self as u16
35             }
36 
37             #[inline]
38             fn as_u32(self) -> u32 {
39                 self as u32
40             }
41 
42             #[inline]
43             fn as_u64(self) -> u64 {
44                 self as u64
45             }
46 
47             #[inline]
48             fn as_u128(self) -> u128 {
49                 self as u128
50             }
51 
52             #[inline]
53             fn as_usize(self) -> usize {
54                 self as usize
55             }
56 
57             #[inline]
58             fn as_i8(self) -> i8 {
59                 self as i8
60             }
61 
62             #[inline]
63             fn as_i16(self) -> i16 {
64                 self as i16
65             }
66 
67             #[inline]
68             fn as_i32(self) -> i32 {
69                 self as i32
70             }
71 
72             #[inline]
73             fn as_i64(self) -> i64 {
74                 self as i64
75             }
76 
77             #[inline]
78             fn as_i128(self) -> i128 {
79                 self as i128
80             }
81 
82             #[inline]
83             fn as_isize(self) -> isize {
84                 self as isize
85             }
86 
87             #[inline]
88             fn as_f32(self) -> f32 {
89                 self as f32
90             }
91 
92             #[inline]
93             fn as_f64(self) -> f64 {
94                 self as f64
95             }
96         }
97     )*)
98 }
99 
100 as_primitive! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize f32 f64 }
101 
102 macro_rules! def_try_primitive {
103     ($($t:ty)*) => (
104         /// Type that can be converted to primitive with `as`.
105         pub trait TryPrimitive:
106             AsCast +
107             $(TryCast<$t> +)*
108         {
109             #[inline]
110             fn try_u8(self) -> Option<u8> {
111                 self.try_cast()
112             }
113 
114             #[inline]
115             fn try_u16(self) -> Option<u16> {
116                 self.try_cast()
117             }
118 
119             #[inline]
120             fn try_u32(self) -> Option<u32> {
121                 self.try_cast()
122             }
123 
124             #[inline]
125             fn try_u64(self) -> Option<u64> {
126                 self.try_cast()
127             }
128 
129             #[inline]
130             fn try_u128(self) -> Option<u128> {
131                 self.try_cast()
132             }
133 
134             #[inline]
135             fn try_usize(self) -> Option<usize> {
136                 self.try_cast()
137             }
138 
139             #[inline]
140             fn try_i8(self) -> Option<i8> {
141                 self.try_cast()
142             }
143 
144             #[inline]
145             fn try_i16(self) -> Option<i16> {
146                 self.try_cast()
147             }
148 
149             #[inline]
150             fn try_i32(self) -> Option<i32> {
151                 self.try_cast()
152             }
153 
154             #[inline]
155             fn try_i64(self) -> Option<i64> {
156                 self.try_cast()
157             }
158 
159             #[inline]
160             fn try_i128(self) -> Option<i128> {
161                 self.try_cast()
162             }
163 
164             #[inline]
165             fn try_isize(self) -> Option<isize> {
166                 self.try_cast()
167             }
168 
169             #[inline]
170             fn try_f32(self) -> Option<f32> {
171                 self.try_cast()
172             }
173 
174             #[inline]
175             fn try_f64(self) -> Option<f64> {
176                 self.try_cast()
177             }
178         }
179     );
180 }
181 
182 def_try_primitive!(u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize f32 f64);
183 
184 macro_rules! try_primitive {
185     ($($t:ty)*) => ($(
186         impl TryPrimitive for $t {}
187     )*)
188 }
189 
190 try_primitive! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize f32 f64 }
191 
192 // PRIMITIVE
193 
194 /// Primitive type trait (which all have static lifetimes).
195 pub trait Primitive: 'static + fmt::Debug + fmt::Display + TryPrimitive
196 {}
197 
198 macro_rules! primitive {
199     ($($t:ty)*) => ($(
200         impl Primitive for $t {}
201     )*)
202 }
203 
204 primitive! { u8 u16 u32 u64 u128 usize i8 i16 i32 i64 i128 isize f32 f64 }
205 
206 // TEST
207 // ----
208 
209 #[cfg(test)]
210 mod tests {
211     use super::*;
212 
213     fn check_as_primitive<T: AsPrimitive>(t: T) {
214         let _: u8 = t.as_u8();
215         let _: u16 = t.as_u16();
216         let _: u32 = t.as_u32();
217         let _: u64 = t.as_u64();
218         let _: u128 = t.as_u128();
219         let _: usize = t.as_usize();
220         let _: i8 = t.as_i8();
221         let _: i16 = t.as_i16();
222         let _: i32 = t.as_i32();
223         let _: i64 = t.as_i64();
224         let _: i128 = t.as_i128();
225         let _: isize = t.as_isize();
226         let _: f32 = t.as_f32();
227         let _: f64 = t.as_f64();
228     }
229 
230     #[test]
231     fn as_primitive_test() {
232         check_as_primitive(1u8);
233         check_as_primitive(1u16);
234         check_as_primitive(1u32);
235         check_as_primitive(1u64);
236         check_as_primitive(1u128);
237         check_as_primitive(1usize);
238         check_as_primitive(1i8);
239         check_as_primitive(1i16);
240         check_as_primitive(1i32);
241         check_as_primitive(1i64);
242         check_as_primitive(1i128);
243         check_as_primitive(1isize);
244         check_as_primitive(1f32);
245         check_as_primitive(1f64);
246     }
247 }
248