#size #unit #format #prefix #no-std

no-std size_format

Allows for easier formatting of sizes

2 stable releases

Uses old Rust 2015

1.0.2 Oct 7, 2018

#151 in Value formatting

Download history 2912/week @ 2023-11-26 3560/week @ 2023-12-03 3062/week @ 2023-12-10 4337/week @ 2023-12-17 1795/week @ 2023-12-24 2901/week @ 2023-12-31 2983/week @ 2024-01-07 3095/week @ 2024-01-14 3482/week @ 2024-01-21 2482/week @ 2024-01-28 2407/week @ 2024-02-04 3089/week @ 2024-02-11 2110/week @ 2024-02-18 2943/week @ 2024-02-25 3478/week @ 2024-03-03 1201/week @ 2024-03-10

9,838 downloads per month
Used in 13 crates (12 directly)

MIT/Apache

20KB
354 lines

size_format

This is a rust crate providing formatting for sizes using prefixes.

For example 4000 bytes could be formatted as 4.0kB.

The main goal is to provide easy formatters for data sizes.

It provides both binary and SI unit prefixes per default, though more could be added.

use size_format::{SizeFormatterBinary, SizeFormatterSI};

assert_eq!(
    format!("{}B", SizeFormatterBinary::new(42 * 1024 * 1024)),
    "42.0MiB".to_string()
);
assert_eq!(
    format!("{}B", SizeFormatterSI::new(42_000_000)),
    "42.0MB".to_string()
);

The precision can also be specified. Please note that values are always rounded down.

use size_format::SizeFormatterSI;

assert_eq!(
    format!("{:.4}B", SizeFormatterSI::new(1_999_999_999)),
    "1.9999GB".to_string()
);
assert_eq!(
    format!("{:.0}B", SizeFormatterSI::new(1_999_999_999)),
    "1GB".to_string()
);

The presented precision will also never exceed the available precision.

use size_format::SizeFormatterSI;

assert_eq!(
    format!("{:.10}B", SizeFormatterSI::new(678)),
    "678B".to_string()
);
assert_eq!(
    format!("{:.10}B", SizeFormatterSI::new(1_999)),
    "1.999kB".to_string()
);

For more flexibility, use the SizeFormatter type directly with the correct type parameters. For example the following code formats a u16 using binary prefixes and uses a comma as a separator.

use size_format::{BinaryPrefixes, CommaSeparated, SizeFormatter};

assert_eq!(
    format!("{:.2}B", SizeFormatter::<u16, BinaryPrefixes, CommaSeparated>::from(65_535u16)),
    "63,99KiB".to_string()
);

Although this crate was mainly intended for data sizes, it can also be used for other units.

It is also possible to implement the PrefixType trait to make your own prefix system.

use size_format::{PointSeparated, PrefixType, SizeFormatter};
use generic_array::{typenum::U3, GenericArray};

struct Millimeter;

impl PrefixType for Millimeter {
    type N = U3;

    const PREFIX_SIZE: u32 = 1000;

    fn prefixes() -> GenericArray<&'static str, Self::N> {
        ["m", "", "k"].into()
    }
}

assert_eq!(
    format!("{}m", SizeFormatter::<u32, Millimeter, PointSeparated>::new(1)),
    "1mm".to_string()
);
assert_eq!(
    format!("{}m", SizeFormatter::<u32, Millimeter, PointSeparated>::new(1_000)),
    "1.0m".to_string()
);
assert_eq!(
    format!("{}m", SizeFormatter::<u32, Millimeter, PointSeparated>::new(1_000_000)),
    "1.0km".to_string()
);
assert_eq!(
    format!("{}m", SizeFormatter::<u64, Millimeter, PointSeparated>::new(10_000_000_000)),
    "10000.0km".to_string()
);

Dependencies

~725KB
~16K SLoC