pub struct SizeFormatter<T: FormatterSize = ()> { /* private fields */ }
Expand description

A standalone size formatter that is configured via the builder pattern (via the various .with_ methods) which can then be used to format an integral byte value as a pretty printed String in accordance with the configured properties.

Use of the strongly typed Size to hold and display sizes is strongly preferred over this approach, but it may come in handy when you have many sizes and all need to be formatted in an identical and manually-specified fashion.

use size::{Base, Size, SizeFormatter, Style};

let formatter = SizeFormatter::new()
    .with_base(Base::Base10)
    .with_style(Style::Abbreviated);

for raw_size in [ 1024, 2048, 4096 ]
{
    let formatted = formatter.format(raw_size);
    println!("{}", &formatted);
}

// Prints:
// 1.02 KB
// 2.05 KB
// 4.10 KB

Implementations

Specify the base of the units to be used when generating the textual description of the Size.

This lets users choose between “standard” base-10 units like “KB” and “MB” or the improved SI base-2 units like “KiB” and “MiB”. See Base for more information.

Specify the style used to write the accompanying unit for a formatted file size.

See Style for more information.

Create a new SizeFormatter that can be used to repeatedly format a number of file sizes according to its configured options.

Formats a provided size in bytes as a string, per the configuration of the current SizeFormatter instance.

Returns the formatted Size as a String, formatted according to the current state of the SizeFormatter instance as modified via with_style(), with_base(), and co.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.