From 6194e56e13fdc77e46e0bb6cae486ba56a678939 Mon Sep 17 00:00:00 2001 From: Wojciech Graj Date: Wed, 18 Feb 2026 19:34:31 +0100 Subject: [PATCH] Add defmt support --- .github/workflows/rust.yml | 6 +++--- Cargo.toml | 7 ++++++- src/arrayvec.rs | 23 +++++++++++++++++++++++ src/lib.rs | 2 ++ src/slicevec.rs | 11 +++++++++++ src/tinyvec.rs | 22 ++++++++++++++++++++++ 6 files changed, 67 insertions(+), 4 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 4ec05e6..7fd1da7 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -11,7 +11,7 @@ jobs: matrix: os: [ubuntu-latest] rust: - - 1.55.0 # approximate MSRV is Stable -30 + - 1.60.0 # approximate MSRV is Stable -30 - stable - beta - nightly @@ -28,11 +28,11 @@ jobs: toolchain: ${{ matrix.rust }} # On MSRV some dev-dependencies don't build so we can't run tests. - name: Check MSRV - if: matrix.rust == '1.55.0' + if: matrix.rust == '1.60.0' run: | cargo check --features=alloc,std,grab_spare_slice - name: Test non nightly - if: matrix.rust != '1.55.0' && matrix.rust != 'nightly' + if: matrix.rust != '1.60.0' && matrix.rust != 'nightly' run: | cargo test --features=alloc,std,grab_spare_slice,latest_stable_rust - name: Test on Nightly with All Features diff --git a/Cargo.toml b/Cargo.toml index 819fd44..5984ecd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,8 @@ arbitrary = { version = "1", optional = true } borsh = { version = "1.2.0", optional = true, default-features = false } # Implements the trait `Array` for `GenericArray` struct. generic-array = { version = "1.1.1", optional = true, default-features = false } +# Provides `Format` implementations +defmt = { version = "1.0", optional = true } [features] @@ -76,12 +78,15 @@ experimental_write_impl = [] real_blackbox = ["criterion/real_blackbox"] [package.metadata.docs.rs] -features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh"] +features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "defmt"] rustdoc-args = ["--cfg","docs_rs"] [package.metadata.playground] features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh"] +[profile.dev] +lto = "thin" + [profile.bench] debug = 2 diff --git a/src/arrayvec.rs b/src/arrayvec.rs index c55796d..1fbcdc5 100644 --- a/src/arrayvec.rs +++ b/src/arrayvec.rs @@ -1422,6 +1422,7 @@ impl From for ArrayVec { /// The error type returned when a conversion from a slice to an [`ArrayVec`] /// fails. #[derive(Debug, Copy, Clone)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub struct TryFromSliceError(()); impl core::fmt::Display for TryFromSliceError { @@ -1576,6 +1577,17 @@ where } } +#[cfg(feature = "defmt")] +#[cfg_attr(docs_rs, doc(cfg(feature = "defmt")))] +impl defmt::Format for ArrayVecIterator +where + A::Item: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter<'_>) { + defmt::write!(fmt, "ArrayVecIterator({:?})", self.as_slice()) + } +} + impl IntoIterator for ArrayVec { type Item = A::Item; type IntoIter = ArrayVecIterator; @@ -1716,6 +1728,17 @@ where } } +#[cfg(feature = "defmt")] +#[cfg_attr(docs_rs, doc(cfg(feature = "defmt")))] +impl defmt::Format for ArrayVec +where + A::Item: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter<'_>) { + defmt::Format::format(self.as_slice(), fmt) + } +} + impl Display for ArrayVec where A::Item: Display, diff --git a/src/lib.rs b/src/lib.rs index 112853c..22f4293 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -52,6 +52,8 @@ //! * `borsh` provides a `BorshSerialize` and `BorshDeserialize` implementation //! for [`TinyVec`] and [`ArrayVec`] types, provided the inner item also has //! an implementation. +//! * `defmt` provides a `Format` implementation for all types, provided the +//! inner item also has an implementation. //! //! ## API //! The general goal of the crate is that, as much as possible, the vecs here diff --git a/src/slicevec.rs b/src/slicevec.rs index bb6b744..d7635d4 100644 --- a/src/slicevec.rs +++ b/src/slicevec.rs @@ -894,6 +894,17 @@ where } } +#[cfg(feature = "defmt")] +#[cfg_attr(docs_rs, doc(cfg(feature = "defmt")))] +impl<'s, T> defmt::Format for SliceVec<'s, T> +where + T: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter<'_>) { + defmt::Format::format(self.as_slice(), fmt) + } +} + impl<'s, T> Display for SliceVec<'s, T> where T: Display, diff --git a/src/tinyvec.rs b/src/tinyvec.rs index d2bf218..7fff545 100644 --- a/src/tinyvec.rs +++ b/src/tinyvec.rs @@ -1553,6 +1553,17 @@ where } } +#[cfg(feature = "defmt")] +#[cfg_attr(docs_rs, doc(cfg(feature = "defmt")))] +impl defmt::Format for TinyVecIterator +where + A::Item: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter<'_>) { + defmt::write!(fmt, "TinyVecIterator({:?})", self.as_slice()) + } +} + impl IntoIterator for TinyVec { type Item = A::Item; type IntoIter = TinyVecIterator; @@ -1680,6 +1691,17 @@ where } } +#[cfg(feature = "defmt")] +#[cfg_attr(docs_rs, doc(cfg(feature = "defmt")))] +impl defmt::Format for TinyVec +where + A::Item: defmt::Format, +{ + fn format(&self, fmt: defmt::Formatter<'_>) { + defmt::Format::format(self.as_slice(), fmt) + } +} + impl Display for TinyVec where A::Item: Display,