diff --git a/examples/sizes.rs b/examples/sizes.rs index a425c95..cd25a7c 100644 --- a/examples/sizes.rs +++ b/examples/sizes.rs @@ -1,6 +1,6 @@ extern crate humansize; -use humansize::{format_size, format_size_i, Formatter, IFormatter, BINARY, DECIMAL, WINDOWS}; +use humansize::{format_size, format_size_i, SizeFormatter, ISizeFormatter, BINARY, DECIMAL, WINDOWS}; fn main() { println!("{}", format_size(5456usize, BINARY)); @@ -11,6 +11,6 @@ fn main() { println!("{}", format_size(123456789usize, DECIMAL)); println!("{}", format_size_i(-123456789, WINDOWS)); - println!("{}", Formatter::new(1234u32, BINARY)); - println!("{}", IFormatter::new(1234, BINARY)); + println!("{}", SizeFormatter::new(1234u32, BINARY)); + println!("{}", ISizeFormatter::new(1234, BINARY)); } diff --git a/feature-tests/no-alloc/tests/no_alloc.rs b/feature-tests/no-alloc/tests/no_alloc.rs index 6489f66..bff64e3 100644 --- a/feature-tests/no-alloc/tests/no_alloc.rs +++ b/feature-tests/no-alloc/tests/no_alloc.rs @@ -1,7 +1,7 @@ // This sub-crate exists to make sure that everything works well with the `no_alloc` flag enabled use core::fmt::Write; -use humansize::{Formatter, DECIMAL}; +use humansize::{SizeFormatter, DECIMAL}; struct Buffer([u8; N], usize); @@ -22,6 +22,6 @@ impl Write for Buffer { #[test] fn test() { let mut result = Buffer([0u8; 4], 0); - write!(&mut result, "{}", Formatter::new(1000usize, DECIMAL)).unwrap(); + write!(&mut result, "{}", SizeFormatter::new(1000usize, DECIMAL)).unwrap(); assert_eq!(core::str::from_utf8(&result.0).unwrap(), "1 kB"); } \ No newline at end of file diff --git a/src/allocating.rs b/src/allocating.rs index ceead8e..cd8fcb9 100644 --- a/src/allocating.rs +++ b/src/allocating.rs @@ -2,10 +2,10 @@ use alloc::string::String; use crate::numeric_traits::*; use crate::options::FormatSizeOptions; -use crate::IFormatter; +use crate::ISizeFormatter; pub fn format_size_i(input: impl ToF64, options: impl AsRef) -> String { - format!("{}", IFormatter::new(input, options)) + format!("{}", ISizeFormatter::new(input, options)) } pub fn format_size(input: impl ToF64 + Unsigned, options: impl AsRef) -> String { diff --git a/src/formatters.rs b/src/formatters.rs index 7f7ad45..78de1b4 100644 --- a/src/formatters.rs +++ b/src/formatters.rs @@ -2,18 +2,18 @@ use libm::{fabs, modf}; use crate::{scales, utils::f64_eq, BaseUnit, FormatSizeOptions, Kilo, ToF64, Unsigned}; -pub struct IFormatter> { +pub struct ISizeFormatter> { value: T, options: O, } -impl> IFormatter { +impl> ISizeFormatter { pub fn new(value: V, options: O) -> Self { - IFormatter { value, options } + ISizeFormatter { value, options } } } -impl> core::fmt::Display for IFormatter { +impl> core::fmt::Display for ISizeFormatter { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { let opts = self.options.as_ref(); let divider = opts.kilo.value(); @@ -64,32 +64,32 @@ impl> core::fmt::Display for IFormatter> From<&'a Formatter> - for IFormatter +impl<'a, U: ToF64 + Unsigned + Copy, O: AsRef> From<&'a SizeFormatter> + for ISizeFormatter { - fn from(source: &'a Formatter) -> Self { - IFormatter { + fn from(source: &'a SizeFormatter) -> Self { + ISizeFormatter { value: source.value, options: &source.options, } } } -pub struct Formatter> { +pub struct SizeFormatter> { value: T, options: O, } -impl> Formatter { +impl> SizeFormatter { pub fn new(value: V, options: O) -> Self { - Formatter { value, options } + SizeFormatter { value, options } } } impl + Copy> core::fmt::Display - for Formatter + for SizeFormatter { fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - write!(f, "{}", IFormatter::from(self)) + write!(f, "{}", ISizeFormatter::from(self)) } } diff --git a/src/impl_style.rs b/src/impl_style.rs index 6cb7b59..13a86b1 100644 --- a/src/impl_style.rs +++ b/src/impl_style.rs @@ -1,4 +1,4 @@ -use crate::{FormatSizeOptions, Formatter, IFormatter, Signed, ToF64, Unsigned}; +use crate::{FormatSizeOptions, SizeFormatter, ISizeFormatter, Signed, ToF64, Unsigned}; use alloc::string::String; pub trait FormatSize { @@ -11,12 +11,12 @@ pub trait FormatSizeI { impl FormatSize for T { fn format_size(&self, opts: FormatSizeOptions) -> String { - format!("{}", Formatter::new(*self, opts)) + format!("{}", SizeFormatter::new(*self, opts)) } } impl FormatSizeI for T { fn format_size_i(&self, opts: FormatSizeOptions) -> String { - format!("{}", IFormatter::new(*self, opts)) + format!("{}", ISizeFormatter::new(*self, opts)) } } diff --git a/src/lib.rs b/src/lib.rs index a52337d..9c15201 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,9 +61,9 @@ humansize = { version = "2.0.0", features = ["no_alloc"] } ``` This excludes all allocating code from compilation. You may now use the library's internal `Formatter` struct, which implements `core::fmt::display` so that you can `write!` it to a custom buffer of your choice: ```rust -use humansize::{IFormatter, DECIMAL}; +use humansize::{ISizeFormatter, DECIMAL}; -let formatter = IFormatter::new(1_000_000, DECIMAL); +let formatter = ISizeFormatter::new(1_000_000, DECIMAL); assert_eq!(format!("{}", formatter), "1 MB"); ``` ### ... with the `impl` style API: @@ -137,4 +137,4 @@ mod impl_style; pub use impl_style::{FormatSize, FormatSizeI}; mod formatters; -pub use formatters::{Formatter, IFormatter}; +pub use formatters::{SizeFormatter, ISizeFormatter};