Skip to content

Commit e22ff8b

Browse files
committed
implement more built-in traits
1 parent 45d00cd commit e22ff8b

File tree

5 files changed

+229
-5
lines changed

5 files changed

+229
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "byte-unit"
3-
version = "5.1.0"
3+
version = "5.1.1"
44
authors = ["Magic Len <[email protected]>"]
55
edition = "2021"
66
rust-version = "1.69"

src/bit/built_in_traits.rs

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::str::FromStr;
1+
use core::{cmp::Ordering, str::FromStr};
22

33
use super::Bit;
44
use crate::{ExceededBoundsError, ParseError, TryFromIntError};
@@ -201,3 +201,115 @@ impl FromStr for Bit {
201201
Bit::parse_str(s)
202202
}
203203
}
204+
205+
impl PartialEq<u64> for Bit {
206+
#[cfg(feature = "u128")]
207+
#[inline]
208+
fn eq(&self, other: &u64) -> bool {
209+
self.0 == *other as u128
210+
}
211+
212+
#[cfg(not(feature = "u128"))]
213+
#[inline]
214+
fn eq(&self, other: &u64) -> bool {
215+
self.0 == *other
216+
}
217+
}
218+
219+
impl PartialEq<u128> for Bit {
220+
#[cfg(feature = "u128")]
221+
#[inline]
222+
fn eq(&self, other: &u128) -> bool {
223+
self.0 == *other
224+
}
225+
226+
#[cfg(not(feature = "u128"))]
227+
#[inline]
228+
fn eq(&self, other: &u128) -> bool {
229+
self.0 as u128 == *other
230+
}
231+
}
232+
233+
impl PartialEq<Bit> for u64 {
234+
#[cfg(feature = "u128")]
235+
#[inline]
236+
fn eq(&self, other: &Bit) -> bool {
237+
*self as u128 == other.0
238+
}
239+
240+
#[cfg(not(feature = "u128"))]
241+
#[inline]
242+
fn eq(&self, other: &Bit) -> bool {
243+
*self == other.0
244+
}
245+
}
246+
247+
impl PartialEq<Bit> for u128 {
248+
#[cfg(feature = "u128")]
249+
#[inline]
250+
fn eq(&self, other: &Bit) -> bool {
251+
*self == other.0
252+
}
253+
254+
#[cfg(not(feature = "u128"))]
255+
#[inline]
256+
fn eq(&self, other: &Bit) -> bool {
257+
*self == other.0 as u128
258+
}
259+
}
260+
261+
impl PartialOrd<u64> for Bit {
262+
#[cfg(feature = "u128")]
263+
#[inline]
264+
fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
265+
self.0.partial_cmp(&(*other as u128))
266+
}
267+
268+
#[cfg(not(feature = "u128"))]
269+
#[inline]
270+
fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
271+
self.0.partial_cmp(other)
272+
}
273+
}
274+
275+
impl PartialOrd<u128> for Bit {
276+
#[cfg(feature = "u128")]
277+
#[inline]
278+
fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
279+
self.0.partial_cmp(other)
280+
}
281+
282+
#[cfg(not(feature = "u128"))]
283+
#[inline]
284+
fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
285+
(self.0 as u128).partial_cmp(other)
286+
}
287+
}
288+
289+
impl PartialOrd<Bit> for u64 {
290+
#[cfg(feature = "u128")]
291+
#[inline]
292+
fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
293+
(*self as u128).partial_cmp(&other.0)
294+
}
295+
296+
#[cfg(not(feature = "u128"))]
297+
#[inline]
298+
fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
299+
self.partial_cmp(&other.0)
300+
}
301+
}
302+
303+
impl PartialOrd<Bit> for u128 {
304+
#[cfg(feature = "u128")]
305+
#[inline]
306+
fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
307+
self.partial_cmp(&other.0)
308+
}
309+
310+
#[cfg(not(feature = "u128"))]
311+
#[inline]
312+
fn partial_cmp(&self, other: &Bit) -> Option<Ordering> {
313+
self.partial_cmp(&(other.0 as u128))
314+
}
315+
}

src/byte/built_in_traits.rs

Lines changed: 113 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::str::FromStr;
1+
use core::{cmp::Ordering, str::FromStr};
22

33
use super::Byte;
44
use crate::{ExceededBoundsError, ParseError, TryFromIntError};
@@ -202,3 +202,115 @@ impl FromStr for Byte {
202202
Byte::parse_str(s, false)
203203
}
204204
}
205+
206+
impl PartialEq<u64> for Byte {
207+
#[cfg(feature = "u128")]
208+
#[inline]
209+
fn eq(&self, other: &u64) -> bool {
210+
self.0 == *other as u128
211+
}
212+
213+
#[cfg(not(feature = "u128"))]
214+
#[inline]
215+
fn eq(&self, other: &u64) -> bool {
216+
self.0 == *other
217+
}
218+
}
219+
220+
impl PartialEq<u128> for Byte {
221+
#[cfg(feature = "u128")]
222+
#[inline]
223+
fn eq(&self, other: &u128) -> bool {
224+
self.0 == *other
225+
}
226+
227+
#[cfg(not(feature = "u128"))]
228+
#[inline]
229+
fn eq(&self, other: &u128) -> bool {
230+
self.0 as u128 == *other
231+
}
232+
}
233+
234+
impl PartialEq<Byte> for u64 {
235+
#[cfg(feature = "u128")]
236+
#[inline]
237+
fn eq(&self, other: &Byte) -> bool {
238+
*self as u128 == other.0
239+
}
240+
241+
#[cfg(not(feature = "u128"))]
242+
#[inline]
243+
fn eq(&self, other: &Byte) -> bool {
244+
*self == other.0
245+
}
246+
}
247+
248+
impl PartialEq<Byte> for u128 {
249+
#[cfg(feature = "u128")]
250+
#[inline]
251+
fn eq(&self, other: &Byte) -> bool {
252+
*self == other.0
253+
}
254+
255+
#[cfg(not(feature = "u128"))]
256+
#[inline]
257+
fn eq(&self, other: &Byte) -> bool {
258+
*self == other.0 as u128
259+
}
260+
}
261+
262+
impl PartialOrd<u64> for Byte {
263+
#[cfg(feature = "u128")]
264+
#[inline]
265+
fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
266+
self.0.partial_cmp(&(*other as u128))
267+
}
268+
269+
#[cfg(not(feature = "u128"))]
270+
#[inline]
271+
fn partial_cmp(&self, other: &u64) -> Option<Ordering> {
272+
self.0.partial_cmp(other)
273+
}
274+
}
275+
276+
impl PartialOrd<u128> for Byte {
277+
#[cfg(feature = "u128")]
278+
#[inline]
279+
fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
280+
self.0.partial_cmp(other)
281+
}
282+
283+
#[cfg(not(feature = "u128"))]
284+
#[inline]
285+
fn partial_cmp(&self, other: &u128) -> Option<Ordering> {
286+
(self.0 as u128).partial_cmp(other)
287+
}
288+
}
289+
290+
impl PartialOrd<Byte> for u64 {
291+
#[cfg(feature = "u128")]
292+
#[inline]
293+
fn partial_cmp(&self, other: &Byte) -> Option<Ordering> {
294+
(*self as u128).partial_cmp(&other.0)
295+
}
296+
297+
#[cfg(not(feature = "u128"))]
298+
#[inline]
299+
fn partial_cmp(&self, other: &Byte) -> Option<Ordering> {
300+
self.partial_cmp(&other.0)
301+
}
302+
}
303+
304+
impl PartialOrd<Byte> for u128 {
305+
#[cfg(feature = "u128")]
306+
#[inline]
307+
fn partial_cmp(&self, other: &Byte) -> Option<Ordering> {
308+
self.partial_cmp(&other.0)
309+
}
310+
311+
#[cfg(not(feature = "u128"))]
312+
#[inline]
313+
fn partial_cmp(&self, other: &Byte) -> Option<Ordering> {
314+
self.partial_cmp(&(other.0 as u128))
315+
}
316+
}

tests/bit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,6 @@ fn tests() {
152152
let bit = Bit::from_f64_with_unit(case.1 .0, case.1 .1).unwrap();
153153

154154
assert_eq!(case.0, serde_json::to_string(&bit).unwrap(), "{i}");
155-
assert_eq!(bit, serde_json::from_str(case.0).unwrap(), "{i}");
155+
assert_eq!(bit, serde_json::from_str::<Bit>(case.0).unwrap(), "{i}");
156156
}
157157
}

tests/byte.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,6 @@ fn tests() {
153153
let byte = Byte::from_f64_with_unit(case.1 .0, case.1 .1).unwrap();
154154

155155
assert_eq!(case.0, serde_json::to_string(&byte).unwrap(), "{i}");
156-
assert_eq!(byte, serde_json::from_str(case.0).unwrap(), "{i}");
156+
assert_eq!(byte, serde_json::from_str::<Byte>(case.0).unwrap(), "{i}");
157157
}
158158
}

0 commit comments

Comments
 (0)