Struct size::fmt::SizeFormatter
source · [−]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
sourceimpl<T: FormatterSize> SizeFormatter<T>
impl<T: FormatterSize> SizeFormatter<T>
sourcepub fn with_base(self, base: Base) -> Self
pub fn with_base(self, base: Base) -> Self
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.
sourcepub fn with_style(self, style: Style) -> Self
pub fn with_style(self, style: Style) -> Self
Specify the style used to write the accompanying unit for a formatted file size.
See Style
for more information.
sourceimpl SizeFormatter<()>
impl SizeFormatter<()>
sourcepub const fn new() -> SizeFormatter<()>
pub const fn new() -> SizeFormatter<()>
Create a new SizeFormatter
that can be used to repeatedly format a number of file sizes
according to its configured options.
sourceimpl SizeFormatter<&'a Size>
impl SizeFormatter<&'a Size>
sourcepub fn to_string(&self) -> String
pub fn to_string(&self) -> String
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.