Skip to main content

Display

Struct Display 

Source
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

Source

pub fn binary(self) -> Self

Set the display option to the DisplayOptions::BINARY preset.

See Display for examples.

Source

pub fn decimal(self) -> Self

Set the display option to the DisplayOptions::DECIMAL preset.

See Display for examples.

Source

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());
Source

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.

Trait Implementations§

Source§

impl Clone for Display

Source§

fn clone(&self) -> Display

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Display

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Display

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.