byte-unit
A Rust library for parsing, formatting, and converting byte and bit sizes across decimal and binary units.
Repository Health
Technical Analysis
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
Unitenum covering decimal and binary byte units (B, KB…E, KiB…EiB, and Y-scale units with theu128feature) plus bit units (Kbit, Mbit, …) ByteandBitstructs 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:
serdefor (de)serialization,schemarsfor JSON Schema generation,rocketfor web framework parameter support, andrust_decimalfor exact decimal arithmetic no_stdcompatibility by default, with an opt-instdfeature andu128feature 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
rocketfeature) - Embedded or
no_stdRust 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.
Used by 2 apps in this directory
Cryptgeon
File Storage · Security
Self-destructing encrypted notes and files that vanish after viewing — the server never sees your keys.
Meilisearch
Search
Lightning-fast hybrid search engine with AI-powered semantic and full-text retrieval for modern applications.