Expand description
This crate provides multiple semantic wrappers and utilities for byte size representations.
§Features
#![no_std]-capable, no heap allocation, and no runtime dependencies by default.- Generic
ByteSizewrappers over supported unsigned integer base types, withBSizeas theusizealias andBSize8,BSize16,BSize32, andBSize64as shorter aliases for fixed-width base types. FromStrimpl forByteSize, allowing for parsing string size representations like “1.5 KiB” and “521 TB”.Displayimpl forByteSize, allowing for formatting byte sizes as human-readable strings in both binary (e.g., “1.5 MiB”) and decimal (e.g., “1.5 MB”) styles.- Optional
serdesupport for binary and human-readable format. - Optional
nightlysupport for a broader const-friendly API surface powered by nightly-only Rust features.
§Nightly
With the nightly feature enabled on a nightly compiler, this crate can use unstable Rust
capabilities such as const trait support. The visible effect is a broader const surface for
generic byte-size expressions, including unit helpers and simple transformations over the
underlying byte count. Because this follows Rust nightly, exact capabilities may evolve with
upstream language features.
§Examples
Construction using the binary or decimal constant helpers.
use bsize::BSize;
assert!(BSize::kib(4) > BSize::kb(4));
let size: BSize = BSize::b(4_096);
assert_eq!(size.bytes(), 4_096);Parse byte sizes from strings.
use bsize::BSize64;
let size: BSize64 = "1.5 MiB".parse().unwrap();
assert_eq!(BSize64::mib(1).map(|bytes| bytes + 512 * 1024), size);Display as human-readable string.
use bsize::BSize;
use bsize::DisplayBaseUnit;
use bsize::DisplayOptions;
use bsize::DisplayScale;
assert_eq!("518.0 GiB", BSize::gib(518).display().binary().to_string());
assert_eq!("556.2 GB", BSize::gib(518).display().decimal().to_string());
let network_units = DisplayOptions::DECIMAL
.base_unit(DisplayBaseUnit::Bit)
.scale(DisplayScale::Mega);
let display = bsize::display(125_000u64).options(|_opts| network_units);
assert_eq!("1.0 Mbit", display.to_string());Arithmetic operations are supported.
use bsize::BSize;
let plus = BSize::mb(1) + BSize::kb(100);
println!("{plus}");
let minus = BSize::tb(1) - BSize::gb(4);
assert_eq!(BSize::gb(996), minus);Arithmetic operations over the underlying types are supported.
use bsize::BSize;
let size = BSize::mb(1);
let size = size.map(|b| b * 4); // 4x scale
println!("{size}");Structs§
- Byte
Size - Byte size representation.
- Display
- Display wrapper for formatting byte sizes as human-readable strings.
- Display
Options - Formatting options for
Display.
Enums§
- Display
Base Unit - Base unit used by
DisplayOptions. - Display
Scale - Scale used by
DisplayOptions. - Display
Unit System - Unit system used by
DisplayOptions. - Parse
Error - The error returned when parsing a byte size fails.
Traits§
- Base
Byte Size nightly - A sealed trait for integer types that can back
ByteSize. - ExaByte
Size nightly - Provides exabyte-scale unit constants for supported backing integer types.
- Giga
Byte Size nightly - Provides gigabyte-scale unit constants for supported backing integer types.
- Kilo
Byte Size nightly - Provides kilobyte-scale unit constants for supported backing integer types.
- Mega
Byte Size nightly - Provides megabyte-scale unit constants for supported backing integer types.
- Peta
Byte Size nightly - Provides petabyte-scale unit constants for supported backing integer types.
- Tera
Byte Size nightly - Provides terabyte-scale unit constants for supported backing integer types.