From 39dcd647f23b3b626711981c3800e17057795a79 Mon Sep 17 00:00:00 2001 From: Hexa Dust Date: Fri, 18 Jul 2025 21:19:30 +0100 Subject: [PATCH] optional ufmt::uDebug impl --- Cargo.lock | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 4 ++++ src/calc.rs | 18 +++++++++++++++ src/lib.rs | 18 +++++++++++++++ 4 files changed, 103 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 42484f5..a4ef948 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -20,6 +20,7 @@ version = "0.1.0" dependencies = [ "arrayvec", "num-traits", + "ufmt", ] [[package]] @@ -30,3 +31,65 @@ checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" dependencies = [ "autocfg", ] + +[[package]] +name = "proc-macro2" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02b3e5e68a3a1a02aad3ec490a98007cbc13c37cbe84a3cd7b8e406d76e7f778" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "ufmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1a64846ec02b57e9108d6469d98d1648782ad6bb150a95a9baac26900bbeab9d" +dependencies = [ + "ufmt-macros", + "ufmt-write", +] + +[[package]] +name = "ufmt-macros" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d337d3be617449165cb4633c8dece429afd83f84051024079f97ad32a9663716" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "ufmt-write" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e87a2ed6b42ec5e28cc3b94c09982969e9227600b2e3dcbc1db927a84c06bd69" + +[[package]] +name = "unicode-ident" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" diff --git a/Cargo.toml b/Cargo.toml index cabf607..3bda35a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,10 @@ edition = "2021" [dependencies] num-traits = { version = "0.2.19", default-features = false } +ufmt = { version = "0.2.0", optional = true } [dev-dependencies] arrayvec = { version = "0.7.6", default-features = false } + +[features] +ufmt = ["dep:ufmt"] diff --git a/src/calc.rs b/src/calc.rs index d224178..b80a724 100644 --- a/src/calc.rs +++ b/src/calc.rs @@ -1,10 +1,13 @@ use core::{convert::Infallible, fmt::Display, marker::PhantomData}; use num_traits::{float::FloatCore, PrimInt, Unsigned}; +#[cfg(feature = "ufmt")] +use ufmt::derive::uDebug; use crate::{Decimal, DecimalError}; #[derive(Debug)] +#[cfg_attr(feature = "ufmt", derive(uDebug))] pub enum StackCalcError { StackOverflow, StackUnderflow, @@ -28,6 +31,21 @@ pub struct StackCalc, } +#[cfg(feature = "ufmt")] +impl + ufmt::uDebug for StackCalc +{ + fn fmt(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error> + where + W: ufmt::uWrite + ?Sized, + { + f.debug_struct("StackCalc")? + .field("stack", &self.stack.as_slice())? + .field("index", &self.index)? + .finish() + } +} + impl Default for StackCalc { diff --git a/src/lib.rs b/src/lib.rs index de2e8c4..9d263bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,8 +5,11 @@ pub mod calc; use core::fmt::Display; use num_traits::float::FloatCore; use num_traits::{one, zero, PrimInt, Unsigned}; +#[cfg(feature = "ufmt")] +use ufmt::derive::uDebug; #[derive(Debug)] +#[cfg_attr(feature = "ufmt", derive(uDebug))] pub enum DecimalError { ExponentOverflow, NotANumber, @@ -29,6 +32,21 @@ pub struct Decimal { exponent: EXP, } +#[cfg(feature = "ufmt")] +impl ufmt::uDebug for Decimal { + fn fmt(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error> + where + W: ufmt::uWrite + ?Sized, + { + f.debug_struct("Decimal")? + .field("minus", &self.minus)? + .field("significant", &self.significant.as_slice())? + .field("minus_exponent", &self.minus_exponent)? + .field("exponent", &self.exponent)? + .finish() + } +} + impl Decimal { pub fn new(minus: bool, significant: [u8; SIZE], minus_exponent: bool, exponent: EXP) -> Self { for s in significant {