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