optional ufmt::uDebug impl
This commit is contained in:
63
Cargo.lock
generated
63
Cargo.lock
generated
@@ -20,6 +20,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"arrayvec",
|
"arrayvec",
|
||||||
"num-traits",
|
"num-traits",
|
||||||
|
"ufmt",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
@@ -30,3 +31,65 @@ checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"autocfg",
|
"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"
|
||||||
|
|||||||
@@ -5,6 +5,10 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
num-traits = { version = "0.2.19", default-features = false }
|
num-traits = { version = "0.2.19", default-features = false }
|
||||||
|
ufmt = { version = "0.2.0", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
arrayvec = { version = "0.7.6", default-features = false }
|
arrayvec = { version = "0.7.6", default-features = false }
|
||||||
|
|
||||||
|
[features]
|
||||||
|
ufmt = ["dep:ufmt"]
|
||||||
|
|||||||
18
src/calc.rs
18
src/calc.rs
@@ -1,10 +1,13 @@
|
|||||||
use core::{convert::Infallible, fmt::Display, marker::PhantomData};
|
use core::{convert::Infallible, fmt::Display, marker::PhantomData};
|
||||||
|
|
||||||
use num_traits::{float::FloatCore, PrimInt, Unsigned};
|
use num_traits::{float::FloatCore, PrimInt, Unsigned};
|
||||||
|
#[cfg(feature = "ufmt")]
|
||||||
|
use ufmt::derive::uDebug;
|
||||||
|
|
||||||
use crate::{Decimal, DecimalError};
|
use crate::{Decimal, DecimalError};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "ufmt", derive(uDebug))]
|
||||||
pub enum StackCalcError {
|
pub enum StackCalcError {
|
||||||
StackOverflow,
|
StackOverflow,
|
||||||
StackUnderflow,
|
StackUnderflow,
|
||||||
@@ -28,6 +31,21 @@ pub struct StackCalc<F: FloatCore, const STACK: usize = 2, const DSIZE: usize =
|
|||||||
phantom: PhantomData<F>,
|
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
|
impl<F: FloatCore, const STACK: usize, const DSIZE: usize, EXP: PrimInt> Default
|
||||||
for StackCalc<F, STACK, DSIZE, EXP>
|
for StackCalc<F, STACK, DSIZE, EXP>
|
||||||
{
|
{
|
||||||
|
|||||||
18
src/lib.rs
18
src/lib.rs
@@ -5,8 +5,11 @@ pub mod calc;
|
|||||||
use core::fmt::Display;
|
use core::fmt::Display;
|
||||||
use num_traits::float::FloatCore;
|
use num_traits::float::FloatCore;
|
||||||
use num_traits::{one, zero, PrimInt, Unsigned};
|
use num_traits::{one, zero, PrimInt, Unsigned};
|
||||||
|
#[cfg(feature = "ufmt")]
|
||||||
|
use ufmt::derive::uDebug;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
#[cfg_attr(feature = "ufmt", derive(uDebug))]
|
||||||
pub enum DecimalError {
|
pub enum DecimalError {
|
||||||
ExponentOverflow,
|
ExponentOverflow,
|
||||||
NotANumber,
|
NotANumber,
|
||||||
@@ -29,6 +32,21 @@ pub struct Decimal<const SIZE: usize = 5, EXP = u16> {
|
|||||||
exponent: EXP,
|
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> {
|
impl<const SIZE: usize, EXP: PrimInt + Unsigned> Decimal<SIZE, EXP> {
|
||||||
pub fn new(minus: bool, significant: [u8; SIZE], minus_exponent: bool, exponent: EXP) -> Self {
|
pub fn new(minus: bool, significant: [u8; SIZE], minus_exponent: bool, exponent: EXP) -> Self {
|
||||||
for s in significant {
|
for s in significant {
|
||||||
|
|||||||
Reference in New Issue
Block a user