From 296ad6890fa4be38f4f7ff167678090f15d68f5b Mon Sep 17 00:00:00 2001 From: ChanTsune <41658782+ChanTsune@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:15:38 +0900 Subject: [PATCH 1/2] Add `BinaryDisplay` for `ByteSize` binary prefixes printing helper --- src/lib.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 3ab3483c..881c81f1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -186,6 +186,25 @@ impl ByteSize { pub fn to_string_as(&self, si_unit: bool) -> String { to_string(self.0, si_unit) } + + /// Returns an object that implements [`Display`] for binary prefixes printing. + /// + /// [`Display`]: fmt::Display + /// + /// # Examples + /// + /// ``` + /// use bytesize::ByteSize; + /// + /// assert_eq!("100 B", format!("{}", ByteSize::b(100).binary_display())); + /// assert_eq!("3.0 MiB", format!("{}", ByteSize::mib(3).binary_display())); + /// assert_eq!("1.8 TiB", format!("{}", ByteSize::tb(2).binary_display())); + /// assert_eq!("5.0 TiB", format!("{}", ByteSize::tib(5).binary_display())); + /// ``` + #[inline(always)] + pub fn binary_display(&self) -> BinaryDisplay { + BinaryDisplay(self) + } } pub fn to_string(bytes: u64, si_prefix: bool) -> String { @@ -348,6 +367,19 @@ where } } +/// Helper struct for binary prefixes printing with [`format!`] and `{}`. +/// +/// This `struct` implements the [`Display`] trait. +/// It is created by the [`binary_display`](ByteSize::binary_display) method on [`ByteSize`]. +pub struct BinaryDisplay<'b>(&'b ByteSize); + +impl Display for BinaryDisplay<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", to_string(self.0 .0, true)) + } +} + + #[cfg(test)] mod tests { use super::*; From 0d9d9c4f49c3bbd2e1a9546fa4364c461ee40f3e Mon Sep 17 00:00:00 2001 From: ChanTsune <41658782+ChanTsune@users.noreply.github.com> Date: Thu, 10 Aug 2023 01:21:56 +0900 Subject: [PATCH 2/2] Add `DecimalDisplay` for `ByteSize` decimal prefixes printing helper --- src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 881c81f1..0d7925f4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -205,6 +205,24 @@ impl ByteSize { pub fn binary_display(&self) -> BinaryDisplay { BinaryDisplay(self) } + + /// Returns an object that implements [`Display`] for SI(decimal) prefixes printing. + /// + /// [`Display`]: fmt::Display + /// + /// # Examples + /// + /// ``` + /// use bytesize::ByteSize; + /// + /// assert_eq!("100 B", format!("{}", ByteSize::b(100).decimal_display())); + /// assert_eq!("3.0 MB", format!("{}", ByteSize::mb(3).decimal_display())); + /// assert_eq!("5.0 TB", format!("{}", ByteSize::tb(5).decimal_display())); + /// ``` + #[inline(always)] + pub fn decimal_display(&self) -> DecimalDisplay { + DecimalDisplay(self) + } } pub fn to_string(bytes: u64, si_prefix: bool) -> String { @@ -379,6 +397,17 @@ impl Display for BinaryDisplay<'_> { } } +/// Helper struct for SI(decimal) prefixes printing with [`format!`] and `{}`. +/// +/// This `struct` implements the [`Display`] trait. +/// It is created by the [`decimal_display`](ByteSize::decimal_display) method on [`ByteSize`]. +pub struct DecimalDisplay<'b>(&'b ByteSize); + +impl Display for DecimalDisplay<'_> { + fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { + write!(f, "{}", to_string(self.0 .0, false)) + } +} #[cfg(test)] mod tests {