diff --git a/CHANGELOG.md b/CHANGELOG.md index 3cc2adb..8f17df9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,3 +3,7 @@ ## Unreleased - Use SI format by default with `Display`. - Use "KiB" for SI unit. +- Implement `Sub` for `ByteSize`. +- Implement `Sub>` for `ByteSize`. +- Implement `SubAssign` for `ByteSize`. +- Implement `SubAssign>` for `ByteSize`. \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index 2063320..6fb33ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ use serde::{de, Deserialize, Deserializer, Serialize, Serializer}; use std::convert::TryFrom; use std::fmt::{self, Debug, Display, Formatter}; -use std::ops::{Add, AddAssign, Mul, MulAssign}; +use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign}; /// byte size for 1 byte pub const B: u64 = 1; @@ -284,6 +284,43 @@ where } } +impl Sub for ByteSize { + type Output = ByteSize; + + #[inline(always)] + fn sub(self, rhs: ByteSize) -> ByteSize { + ByteSize(self.0 - rhs.0) + } +} + +impl SubAssign for ByteSize { + #[inline(always)] + fn sub_assign(&mut self, rhs: ByteSize) { + self.0 -= rhs.0 + } +} + +impl Sub for ByteSize +where + T: Into, +{ + type Output = ByteSize; + #[inline(always)] + fn sub(self, rhs: T) -> ByteSize { + ByteSize(self.0 - (rhs.into())) + } +} + +impl SubAssign for ByteSize +where + T: Into, +{ + #[inline(always)] + fn sub_assign(&mut self, rhs: T) { + self.0 -= rhs.into(); + } +} + impl Mul for ByteSize where T: Into, @@ -380,6 +417,8 @@ mod tests { assert_eq!((x + y).as_u64(), 1_100_000u64); + assert_eq!((x - y).as_u64(), 900_000u64); + assert_eq!((x + (100 * 1000) as u64).as_u64(), 1_100_000); assert_eq!((x * 2u64).as_u64(), 2_000_000); @@ -403,6 +442,14 @@ mod tests { assert_eq!((x + B as u8).as_u64(), 1_000_001); + assert_eq!((x - MB as u64).as_u64(), 0); + + assert_eq!((x - MB as u32).as_u64(), 0); + + assert_eq!((x - KB as u32).as_u64(), 999_000); + + assert_eq!((x - B as u32).as_u64(), 999_999); + x += MB as u64; x += MB as u32; x += 10u16;