pub struct Display { /* private fields */ }Expand description
Display wrapper for formatting byte sizes as human-readable strings.
You may create this wrapper with Display::new, display, or ByteSize::display, then
pass custom DisplayOptions with Display::options.
§Examples
Display with the DisplayOptions::BINARY and DisplayOptions::DECIMAL presets.
use bsize::BSize64;
assert_eq!(
"41.0 KiB",
BSize64::kb(42).display().to_string(), // default to binary
);
assert_eq!("1.0 MiB", BSize64::mib(1).display().binary().to_string(),);
assert_eq!("42.0 kB", BSize64::kb(42).display().decimal().to_string(),);The free display function accepts any supported integer byte size.
assert_eq!("1.5 KiB", bsize::display(1536u64).to_string());Use Display::new when the byte count is already represented as an f64.
use bsize::Display;
assert_eq!("1.5 KiB", Display::new(1536.5).to_string());
assert_eq!("1.54 kB", format!("{:.2}", Display::new(1536.5).decimal()));Use standard formatter precision to control the number of fractional digits.
use bsize::BSize64;
assert_eq!("1.54 KiB", format!("{:.2}", BSize64::b(1575).display()));
assert_eq!("1.575 KiB", format!("{:.3}", bsize::display(1613u64)));Standard formatter width, fill, and alignment options are supported.
let size = bsize::display(1536u64);
assert_eq!("1.5 KiB ", format!("{size:10}"));
assert_eq!(" 1.5 KiB", format!("{size:>10}"));
assert_eq!(" 1.5 KiB ", format!("{size:^10}"));
assert_eq!("*1.5 KiB**", format!("{size:*^10}"));
assert_eq!("**1.50 KiB", format!("{size:*>10.2}"));Use DisplayOptions to choose a fixed scale or show values as bits.
use bsize::DisplayBaseUnit;
use bsize::DisplayScale;
let as_kibits = bsize::display(1536u64).options(|opts| {
opts.base_unit(DisplayBaseUnit::Bit)
.scale(DisplayScale::Kilo)
});
assert_eq!("12.0 Kibit", as_kibits.to_string());Decimal units use a base of 1000 and SI prefixes.
use bsize::DisplayScale;
use bsize::DisplayUnitSystem;
let display = bsize::display(1_500_000u64).options(|opts| {
opts.unit_system(DisplayUnitSystem::Decimal)
.scale(DisplayScale::Mega)
});
assert_eq!("1.500 MB", format!("{display:.3}"));Implementations§
Source§impl Display
impl Display
Sourcepub fn binary(self) -> Self
pub fn binary(self) -> Self
Set the display option to the DisplayOptions::BINARY preset.
See Display for examples.
Sourcepub fn decimal(self) -> Self
pub fn decimal(self) -> Self
Set the display option to the DisplayOptions::DECIMAL preset.
See Display for examples.
Sourcepub fn options(self, f: impl FnOnce(DisplayOptions) -> DisplayOptions) -> Self
pub fn options(self, f: impl FnOnce(DisplayOptions) -> DisplayOptions) -> Self
Set the options for display.
The provided closure receives the current options, so customizations can build on the default binary preset or preconfigured options.
§Examples
use bsize::DisplayScale;
let display = bsize::display(1536u64)
.decimal()
.options(|opts| opts.scale(DisplayScale::Kilo));
assert_eq!("1.5 kB", display.to_string());Use |_| options when the current options should be replaced as a whole.
use bsize::DisplayBaseUnit;
use bsize::DisplayOptions;
use bsize::DisplayScale;
let network_units = DisplayOptions::DECIMAL
.base_unit(DisplayBaseUnit::Bit)
.scale(DisplayScale::Mega);
let display = bsize::display(125_000u64).options(|_| network_units);
assert_eq!("1.0 Mbit", display.to_string());Sourcepub fn new(size: f64) -> Self
pub fn new(size: f64) -> Self
Create a Display instance from a byte count.
This constructor is useful when the byte count is already represented as an f64. For
supported integer byte counts, use display or ByteSize::display.
§Examples
use bsize::Display;
assert_eq!("2.5 KiB", Display::new(2560.0).to_string());§Panics
Panics if the size is NaN or negative.