const-bcd 0.1.0

Packed Binary-Coded Decimal (BCD) values with const-generic storage and no_std support
Documentation
  • Coverage
  • 100%
    61 out of 61 items documented18 out of 61 items with examples
  • Size
  • Source code size: 49.7 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 384.8 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 5s Average build duration of successful builds.
  • all releases: 5s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • Eclypsed/ChromaDisc
    2 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • Eclypsed

const-bcd

Packed Binary-Coded Decimal (BCD) values with const-generic storage.

const-bcd provides [Bcd<BYTES>], a no_std-friendly type for storing unsigned integers in packed BCD format, where each byte holds two decimal digits (one per nibble). The BYTES const parameter selects the storage size at compile time and therefore the maximum representable value.

  • #![no_std] compatible (disable the default std feature)
  • No allocations
  • All construction and conversion functions are const fn
  • The [bcd!] macro is usable in const context

Usage

Add const-bcd to your Cargo.toml:

[dependencies]
const-bcd = "0.1"

For a no_std build:

[dependencies]
const-bcd = { version = "0.1", default-features = false }

Examples

use const_bcd::{Bcd, bcd};

// From a raw packed-BCD byte array
let n = Bcd::<2>::from_bcd_bytes([0x12, 0x34]).unwrap();
assert_eq!(n.to_string(), "1234");

// From a primitive integer (fallible)
let n = Bcd::<2>::try_from_u16(1234).unwrap();
assert_eq!(n.to_string(), "1234");

// Convenience macro (panics on overflow, usable in const context)
const N: Bcd<2> = bcd!(1234u16);
assert_eq!(N.to_string(), "1234");

// Convert back to a primitive
let value: u16 = n.try_into_u16().unwrap();
assert_eq!(value, 1234);

License

Licensed under either of

at your option.