mirror of
https://github.com/LeopoldArkham/humansize
synced 2024-11-22 03:33:10 +00:00
fix struct name error
This commit is contained in:
parent
d6d5b342b5
commit
38943dc0b1
6 changed files with 26 additions and 26 deletions
|
@ -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));
|
||||
}
|
||||
|
|
|
@ -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<const N: usize>([u8; N], usize);
|
||||
|
@ -22,6 +22,6 @@ impl<const N: usize> Write for Buffer<N> {
|
|||
#[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");
|
||||
}
|
|
@ -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<FormatSizeOptions>) -> String {
|
||||
format!("{}", IFormatter::new(input, options))
|
||||
format!("{}", ISizeFormatter::new(input, options))
|
||||
}
|
||||
|
||||
pub fn format_size(input: impl ToF64 + Unsigned, options: impl AsRef<FormatSizeOptions>) -> String {
|
||||
|
|
|
@ -2,18 +2,18 @@ use libm::{fabs, modf};
|
|||
|
||||
use crate::{scales, utils::f64_eq, BaseUnit, FormatSizeOptions, Kilo, ToF64, Unsigned};
|
||||
|
||||
pub struct IFormatter<T: ToF64, O: AsRef<FormatSizeOptions>> {
|
||||
pub struct ISizeFormatter<T: ToF64, O: AsRef<FormatSizeOptions>> {
|
||||
value: T,
|
||||
options: O,
|
||||
}
|
||||
|
||||
impl<V: ToF64, O: AsRef<FormatSizeOptions>> IFormatter<V, O> {
|
||||
impl<V: ToF64, O: AsRef<FormatSizeOptions>> ISizeFormatter<V, O> {
|
||||
pub fn new(value: V, options: O) -> Self {
|
||||
IFormatter { value, options }
|
||||
ISizeFormatter { value, options }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToF64, O: AsRef<FormatSizeOptions>> core::fmt::Display for IFormatter<T, O> {
|
||||
impl<T: ToF64, O: AsRef<FormatSizeOptions>> core::fmt::Display for ISizeFormatter<T, O> {
|
||||
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<T: ToF64, O: AsRef<FormatSizeOptions>> core::fmt::Display for IFormatter<T,
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, U: ToF64 + Unsigned + Copy, O: AsRef<FormatSizeOptions>> From<&'a Formatter<U, O>>
|
||||
for IFormatter<U, &'a O>
|
||||
impl<'a, U: ToF64 + Unsigned + Copy, O: AsRef<FormatSizeOptions>> From<&'a SizeFormatter<U, O>>
|
||||
for ISizeFormatter<U, &'a O>
|
||||
{
|
||||
fn from(source: &'a Formatter<U, O>) -> Self {
|
||||
IFormatter {
|
||||
fn from(source: &'a SizeFormatter<U, O>) -> Self {
|
||||
ISizeFormatter {
|
||||
value: source.value,
|
||||
options: &source.options,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Formatter<T: ToF64 + Unsigned, O: AsRef<FormatSizeOptions>> {
|
||||
pub struct SizeFormatter<T: ToF64 + Unsigned, O: AsRef<FormatSizeOptions>> {
|
||||
value: T,
|
||||
options: O,
|
||||
}
|
||||
|
||||
impl<V: ToF64 + Unsigned, O: AsRef<FormatSizeOptions>> Formatter<V, O> {
|
||||
impl<V: ToF64 + Unsigned, O: AsRef<FormatSizeOptions>> SizeFormatter<V, O> {
|
||||
pub fn new(value: V, options: O) -> Self {
|
||||
Formatter { value, options }
|
||||
SizeFormatter { value, options }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToF64 + Unsigned + Copy, O: AsRef<FormatSizeOptions> + Copy> core::fmt::Display
|
||||
for Formatter<T, O>
|
||||
for SizeFormatter<T, O>
|
||||
{
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
|
||||
write!(f, "{}", IFormatter::from(self))
|
||||
write!(f, "{}", ISizeFormatter::from(self))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<T> {
|
||||
|
@ -11,12 +11,12 @@ pub trait FormatSizeI<T> {
|
|||
|
||||
impl<T: ToF64 + Unsigned + Copy> FormatSize<T> for T {
|
||||
fn format_size(&self, opts: FormatSizeOptions) -> String {
|
||||
format!("{}", Formatter::new(*self, opts))
|
||||
format!("{}", SizeFormatter::new(*self, opts))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToF64 + Signed + Copy> FormatSizeI<T> for T {
|
||||
fn format_size_i(&self, opts: FormatSizeOptions) -> String {
|
||||
format!("{}", IFormatter::new(*self, opts))
|
||||
format!("{}", ISizeFormatter::new(*self, opts))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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};
|
||||
|
|
Loading…
Reference in a new issue