Skip to content

Commit 01b7a9f

Browse files
Add defmt support (#216)
1 parent bb18ff1 commit 01b7a9f

6 files changed

Lines changed: 67 additions & 4 deletions

File tree

.github/workflows/rust.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
matrix:
1212
os: [ubuntu-latest]
1313
rust:
14-
- 1.55.0 # approximate MSRV is Stable -30
14+
- 1.60.0 # approximate MSRV is Stable -30
1515
- stable
1616
- beta
1717
- nightly
@@ -28,11 +28,11 @@ jobs:
2828
toolchain: ${{ matrix.rust }}
2929
# On MSRV some dev-dependencies don't build so we can't run tests.
3030
- name: Check MSRV
31-
if: matrix.rust == '1.55.0'
31+
if: matrix.rust == '1.60.0'
3232
run: |
3333
cargo check --features=alloc,std,grab_spare_slice
3434
- name: Test non nightly
35-
if: matrix.rust != '1.55.0' && matrix.rust != 'nightly'
35+
if: matrix.rust != '1.60.0' && matrix.rust != 'nightly'
3636
run: |
3737
cargo test --features=alloc,std,grab_spare_slice,latest_stable_rust
3838
- name: Test on Nightly with All Features

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ arbitrary = { version = "1", optional = true }
2020
borsh = { version = "1.2.0", optional = true, default-features = false }
2121
# Implements the trait `Array` for `GenericArray` struct.
2222
generic-array = { version = "1.1.1", optional = true, default-features = false }
23+
# Provides `Format` implementations
24+
defmt = { version = "1.0", optional = true }
2325

2426

2527
[features]
@@ -79,12 +81,15 @@ experimental_write_impl = []
7981
real_blackbox = ["criterion/real_blackbox"]
8082

8183
[package.metadata.docs.rs]
82-
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh"]
84+
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh", "defmt"]
8385
rustdoc-args = ["--cfg","docs_rs"]
8486

8587
[package.metadata.playground]
8688
features = ["alloc", "std", "grab_spare_slice", "latest_stable_rust", "serde", "borsh"]
8789

90+
[profile.dev]
91+
lto = "thin"
92+
8893
[profile.bench]
8994
debug = 2
9095

src/arrayvec.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,7 @@ impl<A: Array> From<A> for ArrayVec<A> {
14401440
/// The error type returned when a conversion from a slice to an [`ArrayVec`]
14411441
/// fails.
14421442
#[derive(Debug, Copy, Clone)]
1443+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
14431444
pub struct TryFromSliceError(());
14441445

14451446
impl core::fmt::Display for TryFromSliceError {
@@ -1594,6 +1595,17 @@ where
15941595
}
15951596
}
15961597

1598+
#[cfg(feature = "defmt")]
1599+
#[cfg_attr(docs_rs, doc(cfg(feature = "defmt")))]
1600+
impl<A: Array> defmt::Format for ArrayVecIterator<A>
1601+
where
1602+
A::Item: defmt::Format,
1603+
{
1604+
fn format(&self, fmt: defmt::Formatter<'_>) {
1605+
defmt::write!(fmt, "ArrayVecIterator({:?})", self.as_slice())
1606+
}
1607+
}
1608+
15971609
impl<A: Array> IntoIterator for ArrayVec<A> {
15981610
type Item = A::Item;
15991611
type IntoIter = ArrayVecIterator<A>;
@@ -1734,6 +1746,17 @@ where
17341746
}
17351747
}
17361748

1749+
#[cfg(feature = "defmt")]
1750+
#[cfg_attr(docs_rs, doc(cfg(feature = "defmt")))]
1751+
impl<A: Array> defmt::Format for ArrayVec<A>
1752+
where
1753+
A::Item: defmt::Format,
1754+
{
1755+
fn format(&self, fmt: defmt::Formatter<'_>) {
1756+
defmt::Format::format(self.as_slice(), fmt)
1757+
}
1758+
}
1759+
17371760
impl<A: Array> Display for ArrayVec<A>
17381761
where
17391762
A::Item: Display,

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@
5252
//! * `borsh` provides a `BorshSerialize` and `BorshDeserialize` implementation
5353
//! for [`TinyVec`] and [`ArrayVec`] types, provided the inner item also has
5454
//! an implementation.
55+
//! * `defmt` provides a `Format` implementation for all types, provided the
56+
//! inner item also has an implementation.
5557
//!
5658
//! ## API
5759
//! The general goal of the crate is that, as much as possible, the vecs here

src/slicevec.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,17 @@ where
924924
}
925925
}
926926

927+
#[cfg(feature = "defmt")]
928+
#[cfg_attr(docs_rs, doc(cfg(feature = "defmt")))]
929+
impl<'s, T> defmt::Format for SliceVec<'s, T>
930+
where
931+
T: defmt::Format,
932+
{
933+
fn format(&self, fmt: defmt::Formatter<'_>) {
934+
defmt::Format::format(self.as_slice(), fmt)
935+
}
936+
}
937+
927938
impl<'s, T> Display for SliceVec<'s, T>
928939
where
929940
T: Display,

src/tinyvec.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,17 @@ where
15531553
}
15541554
}
15551555

1556+
#[cfg(feature = "defmt")]
1557+
#[cfg_attr(docs_rs, doc(cfg(feature = "defmt")))]
1558+
impl<A: Array> defmt::Format for TinyVecIterator<A>
1559+
where
1560+
A::Item: defmt::Format,
1561+
{
1562+
fn format(&self, fmt: defmt::Formatter<'_>) {
1563+
defmt::write!(fmt, "TinyVecIterator({:?})", self.as_slice())
1564+
}
1565+
}
1566+
15561567
impl<A: Array> IntoIterator for TinyVec<A> {
15571568
type Item = A::Item;
15581569
type IntoIter = TinyVecIterator<A>;
@@ -1680,6 +1691,17 @@ where
16801691
}
16811692
}
16821693

1694+
#[cfg(feature = "defmt")]
1695+
#[cfg_attr(docs_rs, doc(cfg(feature = "defmt")))]
1696+
impl<A: Array> defmt::Format for TinyVec<A>
1697+
where
1698+
A::Item: defmt::Format,
1699+
{
1700+
fn format(&self, fmt: defmt::Formatter<'_>) {
1701+
defmt::Format::format(self.as_slice(), fmt)
1702+
}
1703+
}
1704+
16831705
impl<A: Array> Display for TinyVec<A>
16841706
where
16851707
A::Item: Display,

0 commit comments

Comments
 (0)