byte-unit

A Rust library for parsing, formatting, and converting byte and bit sizes across decimal and binary units.

Library
Cargo
v5.2.5
62stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
49/100Fair
Development Activity52
Maintenance32
Community40
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
67/100Good
Architecture75
Code Quality68
Innovation60
Learning Curve65

byte-unit gives Rust programs a proper type for “a size in bytes” instead of passing around raw u64/u128 numbers and manually dividing by 1024 or 1000 everywhere. Its Unit enum covers the full decimal (KB, MB, GB…) and binary (KiB, MiB, GiB…) ladders plus bit-based units (Kbit, Mbit…), and the Byte/Bit structs let you construct a size from any unit, parse it from a human-readable string like "1.5 GiB", and format it back out with an adjustable unit and precision.

It’s no_std-friendly by default (with an optional std feature), supports u128-width sizes for the largest units (up to Y) behind a feature flag, and ships optional integrations for serde (serialization), schemars (JSON Schema generation), and rocket (web framework request/response support) — making it a common building block wherever a Rust CLI, web service, or config format needs to accept or display human-friendly byte sizes.

What You Get

  • A Unit enum covering decimal and binary byte units (B, KB…E, KiB…EiB, and Y-scale units with the u128 feature) plus bit units (Kbit, Mbit, …)
  • Byte and Bit structs constructible from any unit or parsed from human-readable strings (e.g. "1.5 GiB"), with configurable strict/loose parsing rules
  • Formatting helpers that render a size back to a string in a chosen or auto-selected unit, with configurable decimal precision
  • Optional feature-gated integrations: serde for (de)serialization, schemars for JSON Schema generation, rocket for web framework parameter support, and rust_decimal for exact decimal arithmetic
  • no_std compatibility by default, with an opt-in std feature and u128 feature for extended unit range

Common Use Cases

  • Parsing user- or config-supplied size strings like "512MB" or "2 GiB" in CLI tools, config file loaders, or web APIs
  • Displaying file sizes, disk usage, or transfer amounts to users in a human-friendly, auto-scaled unit rather than raw byte counts
  • Rocket web applications that need a request-guard-friendly byte-size type for upload limits or query parameters (via the rocket feature)
  • Embedded or no_std Rust projects that need byte-size representation without pulling in the standard library

Under The Hood

Architecture — The crate is organized as parallel unit/, byte/, and bit/ modules (~6,000 lines total across src/), each with its own parse.rs, constants.rs, and built_in_trait(s).rs for arithmetic/comparison operator implementations, plus a byte/adjusted/ submodule for the auto-scaled-unit display type. Each feature integration (serde_traits.rs, schemars_traits.rs, rocket_traits.rs) is isolated in its own file per module and compiled in only when the corresponding Cargo feature is enabled, keeping the dependency-free core small. Tech Stack — Rust 2024 edition (rust-version 1.85+), with a single always-on dependency (utf8-width) and everything else — serde, schemars, rocket, rust_decimal — gated behind optional Cargo features; std itself is feature-gated, making no_std the default build target, and u128 is a separate opt-in feature for extended unit range (up to Y-scale). Code Quality — Only 3 dedicated integration test files are visible, with the bulk of correctness likely covered by doctests embedded in the extensive rustdoc examples shown in the README (unit parsing, byte construction, formatting); commit activity (~4/month) is steady, reflecting active maintenance of a mature, narrowly-scoped crate rather than heavy ongoing feature growth. API Design — The API favors explicit unit-aware construction (Byte::from_u64_with_unit(1500, Unit::KB)) and string parsing (Unit::parse_str("KB", true, true)) with clear strict/loose parsing flags, which keeps behavior predictable across the many ambiguous byte-unit conventions (does “KB” mean 1000 or 1024 bytes, is “Kb” bits or bytes) that trip up hand-rolled parsing.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search