byte_unit/bit/
built_in_traits.rs

1use core::{cmp::Ordering, str::FromStr};
2
3use super::Bit;
4use crate::{ExceededBoundsError, ParseError, TryFromIntError};
5
6impl TryFrom<u128> for Bit {
7    type Error = ExceededBoundsError;
8
9    #[inline]
10    fn try_from(value: u128) -> Result<Self, Self::Error> {
11        Bit::from_u128(value).ok_or(ExceededBoundsError)
12    }
13}
14
15impl From<u64> for Bit {
16    #[inline]
17    fn from(value: u64) -> Self {
18        Bit::from_u64(value)
19    }
20}
21
22impl From<u32> for Bit {
23    #[inline]
24    fn from(value: u32) -> Self {
25        Bit::from_u64(value as u64)
26    }
27}
28
29impl From<u16> for Bit {
30    #[inline]
31    fn from(value: u16) -> Self {
32        Bit::from_u64(value as u64)
33    }
34}
35
36impl From<u8> for Bit {
37    #[inline]
38    fn from(value: u8) -> Self {
39        Bit::from_u64(value as u64)
40    }
41}
42
43impl From<usize> for Bit {
44    #[allow(unexpected_cfgs)]
45    #[inline]
46    fn from(value: usize) -> Self {
47        #[cfg(target_pointer_width = "128")]
48        {
49            Byte::from_u128(value as u128).unwrap_or(Byte::MAX)
50        }
51
52        #[cfg(not(target_pointer_width = "128"))]
53        {
54            Bit::from_u64(value as u64)
55        }
56    }
57}
58
59impl TryFrom<i128> for Bit {
60    type Error = ExceededBoundsError;
61
62    #[inline]
63    fn try_from(value: i128) -> Result<Self, Self::Error> {
64        Bit::from_i128(value).ok_or(ExceededBoundsError)
65    }
66}
67
68impl TryFrom<i64> for Bit {
69    type Error = ExceededBoundsError;
70
71    #[inline]
72    fn try_from(value: i64) -> Result<Self, Self::Error> {
73        Bit::from_i64(value).ok_or(ExceededBoundsError)
74    }
75}
76
77impl TryFrom<i32> for Bit {
78    type Error = ExceededBoundsError;
79
80    #[inline]
81    fn try_from(value: i32) -> Result<Self, Self::Error> {
82        Bit::from_i64(value as i64).ok_or(ExceededBoundsError)
83    }
84}
85
86impl TryFrom<i16> for Bit {
87    type Error = ExceededBoundsError;
88
89    #[inline]
90    fn try_from(value: i16) -> Result<Self, Self::Error> {
91        Bit::from_i64(value as i64).ok_or(ExceededBoundsError)
92    }
93}
94
95impl TryFrom<i8> for Bit {
96    type Error = ExceededBoundsError;
97
98    #[inline]
99    fn try_from(value: i8) -> Result<Self, Self::Error> {
100        Bit::from_i64(value as i64).ok_or(ExceededBoundsError)
101    }
102}
103
104impl TryFrom<isize> for Bit {
105    type Error = ExceededBoundsError;
106
107    #[allow(unexpected_cfgs)]
108    #[inline]
109    fn try_from(value: isize) -> Result<Self, Self::Error> {
110        #[cfg(target_pointer_width = "128")]
111        {
112            Bit::from_i128(value as i128).ok_or(ExceededBoundsError)
113        }
114
115        #[cfg(not(target_pointer_width = "128"))]
116        {
117            Bit::from_i64(value as i64).ok_or(ExceededBoundsError)
118        }
119    }
120}
121
122impl TryFrom<f64> for Bit {
123    type Error = ExceededBoundsError;
124
125    #[inline]
126    fn try_from(value: f64) -> Result<Self, Self::Error> {
127        Bit::from_f64(value).ok_or(ExceededBoundsError)
128    }
129}
130
131impl TryFrom<f32> for Bit {
132    type Error = ExceededBoundsError;
133
134    #[inline]
135    fn try_from(value: f32) -> Result<Self, Self::Error> {
136        Bit::from_f32(value).ok_or(ExceededBoundsError)
137    }
138}
139
140impl From<Bit> for u128 {
141    #[inline]
142    fn from(bit: Bit) -> Self {
143        bit.as_u128()
144    }
145}
146
147impl From<Bit> for u64 {
148    #[inline]
149    fn from(bit: Bit) -> Self {
150        bit.as_u64()
151    }
152}
153
154impl TryFrom<Bit> for u32 {
155    type Error = TryFromIntError;
156
157    #[inline]
158    fn try_from(bit: Bit) -> Result<Self, Self::Error> {
159        u32::try_from(bit.as_u64())
160    }
161}
162
163impl TryFrom<Bit> for u16 {
164    type Error = TryFromIntError;
165
166    #[inline]
167    fn try_from(bit: Bit) -> Result<Self, Self::Error> {
168        u16::try_from(bit.as_u64())
169    }
170}
171
172impl TryFrom<Bit> for u8 {
173    type Error = TryFromIntError;
174
175    #[inline]
176    fn try_from(bit: Bit) -> Result<Self, Self::Error> {
177        u8::try_from(bit.as_u64())
178    }
179}
180
181impl TryFrom<Bit> for usize {
182    type Error = TryFromIntError;
183
184    #[allow(unexpected_cfgs)]
185    #[inline]
186    fn try_from(bit: Bit) -> Result<Self, Self::Error> {
187        #[cfg(target_pointer_width = "128")]
188        {
189            usize::try_from(bit.as_u128())
190        }
191
192        #[cfg(not(target_pointer_width = "128"))]
193        {
194            usize::try_from(bit.as_u64())
195        }
196    }
197}
198
199impl FromStr for Bit {
200    type Err = ParseError;
201
202    #[inline]
203    fn from_str(s: &str) -> Result<Self, Self::Err> {
204        Bit::parse_str(s)
205    }
206}
207
208impl PartialEq<u64> for Bit {
209    #[cfg(feature = "u128")]
210    #[inline]
211    fn eq(&self, other: &u64) -> bool {
212        self.0 == *other as u128
213    }
214
215    #[cfg(not(feature = "u128"))]
216    #[inline]
217    fn eq(&self, other: &u64) -> bool {
218        self.0 == *other
219    }
220}
221
222impl PartialEq<u128> for Bit {
223    #[cfg(feature = "u128")]
224    #[inline]
225    fn eq(&self, other: &u128) -> bool {
226        self.0 == *other
227    }
228
229    #[cfg(not(feature = "u128"))]
230    #[inline]
231    fn eq(&self, other: &u128) -> bool {
232        self.0 as u128 == *other
233    }
234}
235
236impl PartialEq<Bit> for u64 {
237    #[cfg(feature = "u128")]
238    #[inline]
239    fn eq(&self, other: &Bit) -> bool {
240        *self as u128 == other.0
241    }
242
243    #[cfg(not(feature = "u128"))]
244    #[inline]
245    fn eq(&self, other: &Bit) -> bool {
246        *self == other.0
247    }
248}
249
250impl PartialEq<Bit> for u128 {
251    #[cfg(feature = "u128")]
252    #[inline]
253    fn eq(&self, other: &Bit) -> bool {
254        *self == other.0
255    }
256
257    #[cfg(not(feature = "u128"))]
258    #[inline]
259    fn eq(&self, other: &Bit) -> bool {
260        *self == other.0 as u128
261    }
262}
263
264impl PartialOrd<u64> for Bit {
265    #[cfg(feature = "u128")]
266    #[inline]
267    fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
268        self.0.partial_cmp(&(*other as u128))
269    }
270
271    #[cfg(not(feature = "u128"))]
272    #[inline]
273    fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
274        self.0.partial_cmp(other)
275    }
276}
277
278impl PartialOrd<u128> for Bit {
279    #[cfg(feature = "u128")]
280    #[inline]
281    fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
282        self.0.partial_cmp(other)
283    }
284
285    #[cfg(not(feature = "u128"))]
286    #[inline]
287    fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
288        (self.0 as u128).partial_cmp(other)
289    }
290}
291
292impl PartialOrd<Bit> for u64 {
293    #[cfg(feature = "u128")]
294    #[inline]
295    fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
296        (*self as u128).partial_cmp(&other.0)
297    }
298
299    #[cfg(not(feature = "u128"))]
300    #[inline]
301    fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
302        self.partial_cmp(&other.0)
303    }
304}
305
306impl PartialOrd<Bit> for u128 {
307    #[cfg(feature = "u128")]
308    #[inline]
309    fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
310        self.partial_cmp(&other.0)
311    }
312
313    #[cfg(not(feature = "u128"))]
314    #[inline]
315    fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
316        self.partial_cmp(&(other.0 as u128))
317    }
318}