Skip to main content

Crate bsize

Crate bsize 

Source
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 ByteSize wrappers over supported unsigned integer base types, with BSize as the usize alias and BSize8, BSize16, BSize32, and BSize64 as shorter aliases for fixed-width base types.
  • FromStr impl for ByteSize, allowing for parsing string size representations like “1.5 KiB” and “521 TB”.
  • Display impl for ByteSize, 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 serde support for binary and human-readable format.
  • Optional nightly support 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§

ByteSize
Byte size representation.
Display
Display wrapper for formatting byte sizes as human-readable strings.
DisplayOptions
Formatting options for Display.

Enums§

DisplayBaseUnit
Base unit used by DisplayOptions.
DisplayScale
Scale used by DisplayOptions.
DisplayUnitSystem
Unit system used by DisplayOptions.
ParseError
The error returned when parsing a byte size fails.

Traits§

BaseByteSizenightly
A sealed trait for integer types that can back ByteSize.
ExaByteSizenightly
Provides exabyte-scale unit constants for supported backing integer types.
GigaByteSizenightly
Provides gigabyte-scale unit constants for supported backing integer types.
KiloByteSizenightly
Provides kilobyte-scale unit constants for supported backing integer types.
MegaByteSizenightly
Provides megabyte-scale unit constants for supported backing integer types.
PetaByteSizenightly
Provides petabyte-scale unit constants for supported backing integer types.
TeraByteSizenightly
Provides terabyte-scale unit constants for supported backing integer types.

Functions§

display
Create a Display instance for displaying a byte size.

Type Aliases§

BSize
Byte size representation backed by usize.
BSize8
Byte size representation backed by u8.
BSize16
Byte size representation backed by u16.
BSize32
Byte size representation backed by u32.
BSize64
Byte size representation backed by u64.