optional ufmt::uDebug impl

This commit is contained in:
2025-07-18 21:19:30 +01:00
parent 33dda0d6ea
commit 39dcd647f2
4 changed files with 103 additions and 0 deletions

View File

@@ -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<F: FloatCore, const STACK: usize = 2, const DSIZE: usize =
phantom: PhantomData<F>,
}
#[cfg(feature = "ufmt")]
impl<F: FloatCore + ufmt::uDebug, const STACK: usize, const DSIZE: usize, EXP: ufmt::uDebug>
ufmt::uDebug for StackCalc<F, STACK, DSIZE, EXP>
{
fn fmt<W>(&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<F: FloatCore, const STACK: usize, const DSIZE: usize, EXP: PrimInt> Default
for StackCalc<F, STACK, DSIZE, EXP>
{

View File

@@ -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<const SIZE: usize = 5, EXP = u16> {
exponent: EXP,
}
#[cfg(feature = "ufmt")]
impl<const SIZE: usize, EXP: ufmt::uDebug> ufmt::uDebug for Decimal<SIZE, EXP> {
fn fmt<W>(&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<const SIZE: usize, EXP: PrimInt + Unsigned> Decimal<SIZE, EXP> {
pub fn new(minus: bool, significant: [u8; SIZE], minus_exponent: bool, exponent: EXP) -> Self {
for s in significant {