Skip to content

Commit d09d674

Browse files
committed
Add support for defmt
1 parent ae2f943 commit d09d674

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ extract_if = []
2222
bytes = { version = "1", optional = true, default-features = false }
2323
serde = { version = "1", optional = true, default-features = false }
2424
malloc_size_of = { version = "0.1.1", optional = true, default-features = false }
25+
defmt = { version = "1", optional = true }
2526

2627
[dev-dependencies]
2728
bincode = "1.0.1"

src/lib.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ use serde::{
105105
use std::io;
106106

107107
/// Error type for APIs with fallible heap allocation
108+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
108109
#[derive(Debug)]
109110
pub enum CollectionAllocErr {
110111
/// Overflow `usize::MAX` or other error during size computation
@@ -562,6 +563,18 @@ where
562563
}
563564
}
564565

566+
#[cfg(all(feature = "extract_if", feature = "defmt"))]
567+
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
568+
impl<T, const N: usize, F> defmt::Format for ExtractIf<'_, T, N, F>
569+
where
570+
F: FnMut(&mut T) -> bool,
571+
T: defmt::Format,
572+
{
573+
fn format(&self, fmt: defmt::Formatter) {
574+
defmt::write!(fmt, "ExtractIf({:?})", self.vec.as_slice());
575+
}
576+
}
577+
565578
#[cfg(feature = "extract_if")]
566579
impl<T, F, const N: usize> Iterator for ExtractIf<'_, T, N, F>
567580
where
@@ -638,6 +651,17 @@ where
638651
}
639652
}
640653

654+
#[cfg(feature = "defmt")]
655+
impl<'a, I, const N: usize> defmt::Format for Splice<'a, I, N>
656+
where
657+
I: Iterator + 'a,
658+
<I as Iterator>::Item: defmt::Format,
659+
{
660+
fn format(&self, fmt: defmt::Formatter) {
661+
defmt::write!(fmt, "Splice({:?})", self.drain);
662+
}
663+
}
664+
641665
impl<I: Iterator, const N: usize> Iterator for Splice<'_, I, N> {
642666
type Item = I::Item;
643667

@@ -2851,6 +2875,33 @@ impl<T: Debug, const N: usize> Debug for Drain<'_, T, N> {
28512875
}
28522876
}
28532877

2878+
#[cfg(feature = "defmt")]
2879+
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
2880+
impl<T: defmt::Format, const N: usize> defmt::Format for SmallVec<T, N> {
2881+
fn format(&self, fmt: defmt::Formatter) {
2882+
defmt::write!(fmt, "{=[?]}", self.as_slice());
2883+
}
2884+
}
2885+
2886+
#[cfg(feature = "defmt")]
2887+
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
2888+
impl<T: defmt::Format, const N: usize> defmt::Format for IntoIter<T, N> {
2889+
fn format(&self, fmt: defmt::Formatter) {
2890+
defmt::write!(fmt, "IntoIter({=[?]})", self.as_slice());
2891+
}
2892+
}
2893+
2894+
#[cfg(feature = "defmt")]
2895+
#[cfg_attr(docsrs, doc(cfg(feature = "defmt")))]
2896+
impl<T: defmt::Format, const N: usize> defmt::Format for Drain
2897+
where
2898+
T: defmt::Format,
2899+
{
2900+
fn format(&self, fmt: defmt::Formatter) {
2901+
defmt::write!(fmt, "Drain({=[?]})", self.iter.as_slice());
2902+
}
2903+
}
2904+
28542905
#[cfg(feature = "serde")]
28552906
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
28562907
impl<T, const N: usize> Serialize for SmallVec<T, N>

0 commit comments

Comments
 (0)