From dfc02ad209bd5d8ec8339a92b44eadf27130f3a3 Mon Sep 17 00:00:00 2001 From: darkwater Date: Wed, 10 Sep 2025 16:23:43 +0200 Subject: [PATCH] Add support for defmt --- Cargo.toml | 1 + src/lib.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index d90e58f..78762b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ extract_if = [] bytes = { version = "1", optional = true, default-features = false } serde = { version = "1", optional = true, default-features = false } malloc_size_of = { version = "0.1.1", optional = true, default-features = false } +defmt = { version = "1", optional = true } [dev-dependencies] bincode = "1.0.1" diff --git a/src/lib.rs b/src/lib.rs index 42a60ef..86e2ae4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,6 +105,7 @@ use serde::{ use std::io; /// Error type for APIs with fallible heap allocation +#[cfg_attr(feature = "defmt", derive(defmt::Format))] #[derive(Debug)] pub enum CollectionAllocErr { /// Overflow `usize::MAX` or other error during size computation @@ -562,6 +563,18 @@ where } } +#[cfg(all(feature = "extract_if", feature = "defmt"))] +#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))] +impl defmt::Format for ExtractIf<'_, T, N, F> +where + F: FnMut(&mut T) -> bool, + T: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "ExtractIf({:?})", self.vec.as_slice()); + } +} + #[cfg(feature = "extract_if")] impl Iterator for ExtractIf<'_, T, N, F> where @@ -638,6 +651,17 @@ where } } +#[cfg(feature = "defmt")] +impl<'a, I, const N: usize> defmt::Format for Splice<'a, I, N> +where + I: Iterator + 'a, + ::Item: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "Splice({:?})", self.drain); + } +} + impl Iterator for Splice<'_, I, N> { type Item = I::Item; @@ -2851,6 +2875,33 @@ impl Debug for Drain<'_, T, N> { } } +#[cfg(feature = "defmt")] +#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))] +impl defmt::Format for SmallVec { + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "{=[?]}", self.as_slice()); + } +} + +#[cfg(feature = "defmt")] +#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))] +impl defmt::Format for IntoIter { + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "IntoIter({=[?]})", self.as_slice()); + } +} + +#[cfg(feature = "defmt")] +#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))] +impl defmt::Format for Drain<'_, T, N> +where + T: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "Drain({=[?]})", self.iter.as_slice()); + } +} + #[cfg(feature = "serde")] #[cfg_attr(docsrs, doc(cfg(feature = "serde")))] impl Serialize for SmallVec