byte_unit/bit/
built_in_traits.rs1use 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 Bit::from_u128(value as u128).unwrap_or(Bit::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 #[inline]
185 fn try_from(bit: Bit) -> Result<Self, Self::Error> {
186 usize::try_from(bit.as_u128())
187 }
188}
189
190impl FromStr for Bit {
191 type Err = ParseError;
192
193 #[inline]
194 fn from_str(s: &str) -> Result<Self, Self::Err> {
195 Bit::parse_str(s)
196 }
197}
198
199impl PartialEq<u64> for Bit {
200 #[cfg(feature = "u128")]
201 #[inline]
202 fn eq(&self, other: &u64) -> bool {
203 self.0 == *other as u128
204 }
205
206 #[cfg(not(feature = "u128"))]
207 #[inline]
208 fn eq(&self, other: &u64) -> bool {
209 self.0 == *other
210 }
211}
212
213impl PartialEq<u128> for Bit {
214 #[cfg(feature = "u128")]
215 #[inline]
216 fn eq(&self, other: &u128) -> bool {
217 self.0 == *other
218 }
219
220 #[cfg(not(feature = "u128"))]
221 #[inline]
222 fn eq(&self, other: &u128) -> bool {
223 self.0 as u128 == *other
224 }
225}
226
227impl PartialEq<Bit> for u64 {
228 #[cfg(feature = "u128")]
229 #[inline]
230 fn eq(&self, other: &Bit) -> bool {
231 *self as u128 == other.0
232 }
233
234 #[cfg(not(feature = "u128"))]
235 #[inline]
236 fn eq(&self, other: &Bit) -> bool {
237 *self == other.0
238 }
239}
240
241impl PartialEq<Bit> for u128 {
242 #[cfg(feature = "u128")]
243 #[inline]
244 fn eq(&self, other: &Bit) -> bool {
245 *self == other.0
246 }
247
248 #[cfg(not(feature = "u128"))]
249 #[inline]
250 fn eq(&self, other: &Bit) -> bool {
251 *self == other.0 as u128
252 }
253}
254
255impl PartialOrd<u64> for Bit {
256 #[cfg(feature = "u128")]
257 #[inline]
258 fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
259 self.0.partial_cmp(&(*other as u128))
260 }
261
262 #[cfg(not(feature = "u128"))]
263 #[inline]
264 fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
265 self.0.partial_cmp(other)
266 }
267}
268
269impl PartialOrd<u128> for Bit {
270 #[cfg(feature = "u128")]
271 #[inline]
272 fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
273 self.0.partial_cmp(other)
274 }
275
276 #[cfg(not(feature = "u128"))]
277 #[inline]
278 fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
279 (self.0 as u128).partial_cmp(other)
280 }
281}
282
283impl PartialOrd<Bit> for u64 {
284 #[cfg(feature = "u128")]
285 #[inline]
286 fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
287 (*self as u128).partial_cmp(&other.0)
288 }
289
290 #[cfg(not(feature = "u128"))]
291 #[inline]
292 fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
293 self.partial_cmp(&other.0)
294 }
295}
296
297impl PartialOrd<Bit> for u128 {
298 #[cfg(feature = "u128")]
299 #[inline]
300 fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
301 self.partial_cmp(&other.0)
302 }
303
304 #[cfg(not(feature = "u128"))]
305 #[inline]
306 fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
307 self.partial_cmp(&(other.0 as u128))
308 }
309}