macro_rules! impl_fixed_defmt {
($($Base:ident),+ $(,)?) => {
$(
impl<Frac> defmt::Format for crate::$Base<Frac>
where
crate::$Base<Frac>: core::fmt::Display,
{
#[inline]
fn format(&self, f: defmt::Formatter<'_>) {
defmt::write!(f, "{}", defmt::Display2Format(self));
}
}
impl<Frac> defmt::Format for crate::Wrapping<crate::$Base<Frac>>
where
crate::Wrapping<crate::$Base<Frac>>: core::fmt::Display,
{
#[inline]
fn format(&self, f: defmt::Formatter<'_>) {
defmt::write!(f, "{}", defmt::Display2Format(self));
}
}
impl<Frac> defmt::Format for crate::Strict<crate::$Base<Frac>>
where
crate::Strict<crate::$Base<Frac>>: core::fmt::Display,
{
#[inline]
fn format(&self, f: defmt::Formatter<'_>) {
defmt::write!(f, "{}", defmt::Display2Format(self));
}
}
impl<Frac> defmt::Format for crate::Saturating<crate::$Base<Frac>>
where
crate::Saturating<crate::$Base<Frac>>: core::fmt::Display,
{
#[inline]
fn format(&self, f: defmt::Formatter<'_>) {
defmt::write!(f, "{}", defmt::Display2Format(self));
}
}
)+
};
}
impl_fixed_defmt!(
FixedI8, FixedI16, FixedI32, FixedI64, FixedI128, FixedU8, FixedU16, FixedU32, FixedU64,
FixedU128,
);
#[cfg(test)]
mod tests {
extern crate std;
use crate::types::{I8F0, I8F8, I16F16, I32F32, I64F64, U8F8, U16F16, U32F32, U64F64};
use defmt::Format;
use std::string::ToString;
fn assert_format<T: Format>() {}
#[test]
fn every_public_alias_implements_format() {
assert_format::<I8F0>();
assert_format::<I8F8>();
assert_format::<I16F16>();
assert_format::<I32F32>();
assert_format::<I64F64>();
assert_format::<U8F8>();
assert_format::<U16F16>();
assert_format::<U32F32>();
assert_format::<U64F64>();
}
macro_rules! check_values {
($Alias:ty, [$(($val:expr, $expect:expr)),+ $(,)?]) => {
$(
assert_eq!(<$Alias>::from_num($val).to_string(), $expect);
)+
};
}
#[test]
fn i8f0_display() {
check_values!(I8F0, [(0.0, "0"), (-1.0, "-1"), (7.0, "7"), (-8.0, "-8")]);
}
#[test]
fn i8f8_display() {
check_values!(
I8F8,
[
(0.0, "0"),
(-0.5, "-0.5"),
(42.25, "42.25"),
(-42.25, "-42.25")
]
);
}
#[test]
fn i16f16_display() {
check_values!(
I16F16,
[(0.0, "0"), (-3.125, "-3.125"), (32767.5, "32767.5")]
);
}
#[test]
fn i32f32_display() {
check_values!(I32F32, [(1.5, "1.5"), (-1.5, "-1.5")]);
}
#[test]
fn i64f64_display() {
check_values!(I64F64, [(1.0, "1"), (1.25, "1.25")]);
}
#[test]
fn u8f8_display() {
check_values!(U8F8, [(0.0, "0"), (0.75, "0.75"), (255.5, "255.5")]);
}
#[test]
fn u16f16_display() {
check_values!(U16F16, [(1.5, "1.5"), (65535.9961, "65535.9961")]);
}
#[test]
fn u32f32_display() {
check_values!(U32F32, [(1.5, "1.5"), (65535.75_f64, "65535.75")]);
}
#[test]
fn u64f64_display() {
check_values!(U64F64, [(1.0, "1")]);
}
#[test]
fn ensure_defmt_impl_not_recursive() {
let val = I16F16::from_num(1.0);
defmt::error!("val = {}", val);
}
#[defmt::global_logger]
struct Logger;
defmt::timestamp!("{=u64:us}", 0);
unsafe impl defmt::Logger for Logger {
fn acquire() {}
unsafe fn flush() {}
unsafe fn release() {}
unsafe fn write(_bytes: &[u8]) {}
}
}