diff --git a/Cargo.toml b/Cargo.toml index 81ea432..97e4a91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ malloc_size_of = { version = "0.1", optional = true, default-features = false } arbitrary = { version = "1", optional = true } bincode = { version = "2", optional = true, default-features = false } unty = { version = "0.0.4", optional = true, default-features = false } +defmt = { version = "1", optional = true } [dev-dependencies] bincode1 = { package = "bincode", version = "1.0.1" } diff --git a/src/lib.rs b/src/lib.rs index 3259774..b864af7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -56,6 +56,10 @@ //! For details, see the //! [Rust Reference](https://doc.rust-lang.org/reference/const_eval.html#const-functions). //! +//! ### `defmt` +//! +//! When this feature is enabled, `SmallVec` implements the `defmt::Format` trait. +//! //! ### `drain_filter` //! //! **This feature is unstable.** It may change to match the unstable `drain_filter` method in libstd. @@ -295,6 +299,7 @@ impl ExtendFromSlice for Vec { } /// 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 @@ -364,6 +369,16 @@ where } } +#[cfg(feature = "defmt")] +impl<'a, T: 'a + Array> defmt::Format for Drain<'a, T> +where + T::Item: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "Drain({=[?]})", self.iter.as_slice()); + } +} + unsafe impl<'a, T: Sync + Array> Sync for Drain<'a, T> {} unsafe impl<'a, T: Send + Array> Send for Drain<'a, T> {} @@ -2122,6 +2137,16 @@ where } } +#[cfg(feature = "defmt")] +impl defmt::Format for SmallVec +where + A::Item: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "{=[?]}", self.as_slice()); + } +} + impl Default for SmallVec { #[inline] fn default() -> SmallVec { @@ -2245,6 +2270,16 @@ where } } +#[cfg(feature = "defmt")] +impl defmt::Format for IntoIter +where + A::Item: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter) { + defmt::write!(fmt, "IntoIter({=[?]})", self.as_slice()); + } +} + impl Clone for IntoIter where A::Item: Clone,