impl uDisplay for Decimal

This commit is contained in:
2025-07-18 21:19:30 +01:00
parent 39dcd647f2
commit 3cdd512e8b
2 changed files with 28 additions and 3 deletions

View File

@@ -32,8 +32,8 @@ pub struct StackCalc<F: FloatCore, const STACK: usize = 2, const DSIZE: usize =
}
#[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>
impl<F: FloatCore, 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

View File

@@ -7,6 +7,8 @@ use num_traits::float::FloatCore;
use num_traits::{one, zero, PrimInt, Unsigned};
#[cfg(feature = "ufmt")]
use ufmt::derive::uDebug;
#[cfg(feature = "ufmt")]
use ufmt::{uDebug, uDisplay, uwrite};
#[derive(Debug)]
#[cfg_attr(feature = "ufmt", derive(uDebug))]
@@ -33,7 +35,7 @@ pub struct Decimal<const SIZE: usize = 5, EXP = u16> {
}
#[cfg(feature = "ufmt")]
impl<const SIZE: usize, EXP: ufmt::uDebug> ufmt::uDebug for Decimal<SIZE, EXP> {
impl<const SIZE: usize, EXP: uDebug> uDebug for Decimal<SIZE, EXP> {
fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
where
W: ufmt::uWrite + ?Sized,
@@ -172,6 +174,29 @@ impl<const SIZE: usize, EXP: Display> Display for Decimal<SIZE, EXP> {
}
}
#[cfg(feature = "ufmt")]
impl<const SIZE: usize, EXP: Display + uDisplay> uDisplay for Decimal<SIZE, EXP> {
fn fmt<W>(&self, f: &mut ufmt::Formatter<'_, W>) -> Result<(), W::Error>
where
W: ufmt::uWrite + ?Sized,
{
if self.minus {
uwrite!(f, "-")?;
}
for i in 0..SIZE {
uwrite!(f, "{}", self.significant[i])?;
if i == 0 {
uwrite!(f, ".")?;
}
}
uwrite!(f, "e")?;
if self.minus_exponent {
uwrite!(f, "-")?;
}
uwrite!(f, "{}", self.exponent)
}
}
impl<const SIZE: usize, EXP: PrimInt + Unsigned> TryFrom<f32> for Decimal<SIZE, EXP> {
type Error = DecimalError;