Skip to content

Commit 5fe816a

Browse files
committed
Add support for defmt
1 parent d0f47a3 commit 5fe816a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ malloc_size_of = { version = "0.1", optional = true, default-features = false }
3232
arbitrary = { version = "1", optional = true }
3333
bincode = { version = "2", optional = true, default-features = false }
3434
unty = { version = "0.0.4", optional = true, default-features = false }
35+
defmt = { version = "1", optional = true }
3536

3637
[dev-dependencies]
3738
bincode1 = { package = "bincode", version = "1.0.1" }

src/lib.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@
5656
//! For details, see the
5757
//! [Rust Reference](https://doc.rust-lang.org/reference/const_eval.html#const-functions).
5858
//!
59+
//! ### `defmt`
60+
//!
61+
//! When this feature is enabled, `SmallVec` implements the `defmt::Format` trait.
62+
//!
5963
//! ### `drain_filter`
6064
//!
6165
//! **This feature is unstable.** It may change to match the unstable `drain_filter` method in libstd.
@@ -295,6 +299,7 @@ impl<T: Clone> ExtendFromSlice<T> for Vec<T> {
295299
}
296300

297301
/// Error type for APIs with fallible heap allocation
302+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
298303
#[derive(Debug)]
299304
pub enum CollectionAllocErr {
300305
/// Overflow `usize::MAX` or other error during size computation
@@ -364,6 +369,16 @@ where
364369
}
365370
}
366371

372+
#[cfg(feature = "defmt")]
373+
impl<'a, T: 'a + Array> defmt::Format for Drain<'a, T>
374+
where
375+
T::Item: defmt::Format,
376+
{
377+
fn format(&self, fmt: defmt::Formatter) {
378+
defmt::write!(fmt, "Drain({=[?]})", self.iter.as_slice());
379+
}
380+
}
381+
367382
unsafe impl<'a, T: Sync + Array> Sync for Drain<'a, T> {}
368383
unsafe impl<'a, T: Send + Array> Send for Drain<'a, T> {}
369384

@@ -2122,6 +2137,16 @@ where
21222137
}
21232138
}
21242139

2140+
#[cfg(feature = "defmt")]
2141+
impl<A: Array> defmt::Format for SmallVec<A>
2142+
where
2143+
A::Item: defmt::Format,
2144+
{
2145+
fn format(&self, fmt: defmt::Formatter) {
2146+
defmt::write!(fmt, "{=[?]}", self.as_slice());
2147+
}
2148+
}
2149+
21252150
impl<A: Array> Default for SmallVec<A> {
21262151
#[inline]
21272152
fn default() -> SmallVec<A> {
@@ -2245,6 +2270,16 @@ where
22452270
}
22462271
}
22472272

2273+
#[cfg(feature = "defmt")]
2274+
impl<A: Array> defmt::Format for IntoIter<A>
2275+
where
2276+
A::Item: defmt::Format,
2277+
{
2278+
fn format(&self, fmt: defmt::Formatter) {
2279+
defmt::write!(fmt, "IntoIter({=[?]})", self.as_slice());
2280+
}
2281+
}
2282+
22482283
impl<A: Array + Clone> Clone for IntoIter<A>
22492284
where
22502285
A::Item: Clone,

0 commit comments

Comments
 (0)