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 defaultstdfeature)- No allocations
- All construction and conversion functions are
const fn - The [
bcd!] macro is usable inconstcontext
Usage
Add const-bcd to your Cargo.toml:
[]
= "0.1"
For a no_std build:
[]
= { = "0.1", = false }
Examples
use ;
// From a raw packed-BCD byte array
let n = from_bcd_bytes.unwrap;
assert_eq!;
// From a primitive integer (fallible)
let n = try_from_u16.unwrap;
assert_eq!;
// Convenience macro (panics on overflow, usable in const context)
const N: = bcd!;
assert_eq!;
// Convert back to a primitive
let value: u16 = n.try_into_u16.unwrap;
assert_eq!;
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.