From 2e19d4ab919428d7f44c1b8d265c0e51dc0272a8 Mon Sep 17 00:00:00 2001 From: Jalon Wong Date: Sat, 17 Jan 2026 17:46:11 -0600 Subject: [PATCH 1/2] Add `defmt::Format` bound to error types --- embedded-can/src/blocking.rs | 4 ++++ embedded-can/src/nb.rs | 4 ++++ embedded-hal-bus/src/i2c/atomic.rs | 6 ++++++ embedded-hal-bus/src/i2c/refcell.rs | 3 +++ embedded-hal-bus/src/spi/atomic.rs | 5 ++++- embedded-hal/src/digital.rs | 6 +++++- embedded-hal/src/i2c.rs | 9 ++++++++- embedded-hal/src/lib.rs | 2 +- embedded-hal/src/pwm.rs | 6 +++++- embedded-hal/src/spi.rs | 4 ++++ 10 files changed, 44 insertions(+), 5 deletions(-) diff --git a/embedded-can/src/blocking.rs b/embedded-can/src/blocking.rs index d99983690..fa3611280 100644 --- a/embedded-can/src/blocking.rs +++ b/embedded-can/src/blocking.rs @@ -6,6 +6,10 @@ pub trait Can { type Frame: crate::Frame; /// Associated error type. + #[cfg(feature = "defmt")] + type Error: crate::Error + defmt::Format; + /// Associated error type. + #[cfg(not(feature = "defmt"))] type Error: crate::Error; /// Puts a frame in the transmit buffer. Blocks until space is available in diff --git a/embedded-can/src/nb.rs b/embedded-can/src/nb.rs index 5010dc03e..b63911a91 100644 --- a/embedded-can/src/nb.rs +++ b/embedded-can/src/nb.rs @@ -6,6 +6,10 @@ pub trait Can { type Frame: crate::Frame; /// Associated error type. + #[cfg(feature = "defmt")] + type Error: crate::Error + defmt::Format; + /// Associated error type. + #[cfg(not(feature = "defmt"))] type Error: crate::Error; /// Puts a frame in the transmit buffer to be sent on the bus. diff --git a/embedded-hal-bus/src/i2c/atomic.rs b/embedded-hal-bus/src/i2c/atomic.rs index b47b8c5e0..544b032f0 100644 --- a/embedded-hal-bus/src/i2c/atomic.rs +++ b/embedded-hal-bus/src/i2c/atomic.rs @@ -1,5 +1,7 @@ use embedded_hal::i2c::{Error, ErrorKind, ErrorType, I2c}; +#[cfg(feature = "defmt-03")] +use crate::defmt; use crate::util::AtomicCell; /// Atomics-based shared bus [`I2c`] implementation. @@ -30,6 +32,8 @@ use crate::util::AtomicCell; /// use embedded_hal_bus::i2c; /// use embedded_hal_bus::util::AtomicCell; /// # use embedded_hal::i2c::{self as hali2c, SevenBitAddress, TenBitAddress, I2c, Operation, ErrorKind}; +/// # #[cfg(feature = "defmt-03")] +/// # use embedded_hal::defmt; /// # pub struct Sensor { /// # i2c: I2C, /// # address: u8, @@ -42,6 +46,7 @@ use crate::util::AtomicCell; /// # type PressureSensor = Sensor; /// # type TemperatureSensor = Sensor; /// # pub struct I2c0; +/// # #[cfg_attr(feature = "defmt-03", derive(defmt::Format))] /// # #[derive(Debug, Copy, Clone, Eq, PartialEq)] /// # pub enum Error { } /// # impl hali2c::Error for Error { @@ -80,6 +85,7 @@ pub struct AtomicDevice<'a, T> { bus: &'a AtomicCell, } +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] #[derive(Debug, Copy, Clone)] /// Wrapper type for errors originating from the atomically-checked I2C bus manager. pub enum AtomicError { diff --git a/embedded-hal-bus/src/i2c/refcell.rs b/embedded-hal-bus/src/i2c/refcell.rs index 445b6b020..73dc1725a 100644 --- a/embedded-hal-bus/src/i2c/refcell.rs +++ b/embedded-hal-bus/src/i2c/refcell.rs @@ -17,6 +17,8 @@ use embedded_hal::i2c::{ErrorType, I2c}; /// use embedded_hal_bus::i2c; /// use core::cell::RefCell; /// # use embedded_hal::i2c::{self as hali2c, SevenBitAddress, TenBitAddress, I2c, Operation, ErrorKind}; +/// # #[cfg(feature = "defmt-03")] +/// # use embedded_hal::defmt; /// # pub struct Sensor { /// # i2c: I2C, /// # address: u8, @@ -29,6 +31,7 @@ use embedded_hal::i2c::{ErrorType, I2c}; /// # type PressureSensor = Sensor; /// # type TemperatureSensor = Sensor; /// # pub struct I2c0; +/// # #[cfg_attr(feature = "defmt-03", derive(defmt::Format))] /// # #[derive(Debug, Copy, Clone, Eq, PartialEq)] /// # pub enum Error { } /// # impl hali2c::Error for Error { diff --git a/embedded-hal-bus/src/spi/atomic.rs b/embedded-hal-bus/src/spi/atomic.rs index b4a4208a2..2f1a15945 100644 --- a/embedded-hal-bus/src/spi/atomic.rs +++ b/embedded-hal-bus/src/spi/atomic.rs @@ -3,6 +3,8 @@ use embedded_hal::digital::OutputPin; use embedded_hal::spi::{Error, ErrorKind, ErrorType, Operation, SpiBus, SpiDevice}; use super::DeviceError; +#[cfg(feature = "defmt-03")] +use crate::defmt; use crate::spi::shared::transaction; use crate::util::AtomicCell; @@ -36,12 +38,13 @@ pub struct AtomicDevice<'a, BUS, CS, D> { delay: D, } -#[derive(Debug, Copy, Clone)] /// Wrapper type for errors returned by [`AtomicDevice`]. +#[derive(Debug, Copy, Clone)] #[cfg_attr( docsrs, doc(cfg(any(feature = "portable-atomic", target_has_atomic = "8"))) )] +#[cfg_attr(feature = "defmt-03", derive(defmt::Format))] pub enum AtomicError { /// This error is returned if the SPI bus was already in use when an operation was attempted, /// which indicates that the driver requirements are not being met with regard to diff --git a/embedded-hal/src/digital.rs b/embedded-hal/src/digital.rs index 581f2ca38..849b49609 100644 --- a/embedded-hal/src/digital.rs +++ b/embedded-hal/src/digital.rs @@ -59,7 +59,11 @@ impl core::fmt::Display for ErrorKind { /// /// This just defines the error type, to be used by the other traits. pub trait ErrorType { - /// Error type + /// Error type. + #[cfg(feature = "defmt-03")] + type Error: Error + defmt::Format; + /// Error type. + #[cfg(not(feature = "defmt-03"))] type Error: Error; } diff --git a/embedded-hal/src/i2c.rs b/embedded-hal/src/i2c.rs index 8b99197ef..45e04ac20 100644 --- a/embedded-hal/src/i2c.rs +++ b/embedded-hal/src/i2c.rs @@ -125,10 +125,13 @@ //! //! ``` //! use embedded_hal::i2c::{self, SevenBitAddress, TenBitAddress, I2c, Operation}; +//! #[cfg(feature = "defmt-03")] +//! use embedded_hal::defmt; //! //! /// I2C0 hardware peripheral which supports both 7-bit and 10-bit addressing. //! pub struct I2c0; //! +//! #[cfg_attr(feature = "defmt-03", derive(defmt::Format))] //! #[derive(Debug, Copy, Clone, Eq, PartialEq)] //! pub enum Error { //! // ... @@ -265,7 +268,11 @@ impl core::fmt::Display for NoAcknowledgeSource { /// /// This just defines the error type, to be used by the other traits. pub trait ErrorType { - /// Error type + /// Error type. + #[cfg(feature = "defmt-03")] + type Error: Error + defmt::Format; + /// Error type. + #[cfg(not(feature = "defmt-03"))] type Error: Error; } diff --git a/embedded-hal/src/lib.rs b/embedded-hal/src/lib.rs index f5eb76c32..3a025b24e 100644 --- a/embedded-hal/src/lib.rs +++ b/embedded-hal/src/lib.rs @@ -18,4 +18,4 @@ mod private { // needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`. #[cfg(feature = "defmt-03")] -use defmt_03 as defmt; +pub use defmt_03 as defmt; diff --git a/embedded-hal/src/pwm.rs b/embedded-hal/src/pwm.rs index 4d4ff1508..dc124cbde 100644 --- a/embedded-hal/src/pwm.rs +++ b/embedded-hal/src/pwm.rs @@ -58,7 +58,11 @@ impl core::fmt::Display for ErrorKind { /// /// This just defines the error type, to be used by the other traits. pub trait ErrorType { - /// Error type + /// Error type. + #[cfg(feature = "defmt-03")] + type Error: Error + defmt::Format; + /// Error type. + #[cfg(not(feature = "defmt-03"))] type Error: Error; } diff --git a/embedded-hal/src/spi.rs b/embedded-hal/src/spi.rs index 55fc7e9fb..aa5e9a701 100644 --- a/embedded-hal/src/spi.rs +++ b/embedded-hal/src/spi.rs @@ -307,6 +307,10 @@ impl core::fmt::Display for ErrorKind { /// This just defines the error type, to be used by the other SPI traits. pub trait ErrorType { /// Error type. + #[cfg(feature = "defmt-03")] + type Error: Error + defmt::Format; + /// Error type. + #[cfg(not(feature = "defmt-03"))] type Error: Error; } From c6015a003393e2e90a45e0fd5c519c92144d9139 Mon Sep 17 00:00:00 2001 From: Jalon Wong Date: Sat, 17 Jan 2026 23:08:13 -0600 Subject: [PATCH 2/2] Add `defmt::Format` bound to error types in embedded-io --- .github/workflows/clippy.yml | 3 ++- .github/workflows/rustdoc.yml | 3 ++- .github/workflows/test.yml | 8 +++++--- embedded-io/src/lib.rs | 4 ++++ 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.github/workflows/clippy.yml b/.github/workflows/clippy.yml index 422eab62f..8543bf608 100644 --- a/.github/workflows/clippy.yml +++ b/.github/workflows/clippy.yml @@ -14,4 +14,5 @@ jobs: - uses: dtolnay/rust-toolchain@stable with: components: clippy - - run: cargo clippy --all-features -- --deny=warnings + - run: cargo clippy --workspace --exclude=embedded-io-adapters --all-features -- --deny=warnings + - run: cargo clippy --package=embedded-io-adapters --all-features -- --deny=warnings diff --git a/.github/workflows/rustdoc.yml b/.github/workflows/rustdoc.yml index 1a99f8d92..6f3a19412 100644 --- a/.github/workflows/rustdoc.yml +++ b/.github/workflows/rustdoc.yml @@ -15,4 +15,5 @@ jobs: with: toolchain: nightly-2024-07-26 # tokio/net required to workaround https://github.com/tokio-rs/tokio/issues/6165 - - run: RUSTDOCFLAGS="--deny=warnings --cfg=docsrs" cargo doc --all-features --features tokio/net + - run: RUSTDOCFLAGS="--deny=warnings --cfg=docsrs" cargo doc --workspace --exclude=embedded-io-adapters --all-features + - run: RUSTDOCFLAGS="--deny=warnings --cfg=docsrs" cargo doc --package=embedded-io-adapters --all-features --features tokio/net diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5b5ee297d..3e637d979 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,13 +17,14 @@ jobs: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - run: cargo test --workspace - + test-all-features: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - - run: cargo test --workspace --all-features + - run: cargo test --workspace --exclude=embedded-io-adapters --all-features + - run: cargo test --package=embedded-io-adapters --all-features build-nostd: runs-on: ubuntu-latest @@ -43,4 +44,5 @@ jobs: steps: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@1.81 - - run: cargo test --workspace --all-features + - run: cargo test --workspace --exclude=embedded-io-adapters --all-features + - run: cargo test --package=embedded-io-adapters --all-features diff --git a/embedded-io/src/lib.rs b/embedded-io/src/lib.rs index 7f56be6f7..7529a166a 100644 --- a/embedded-io/src/lib.rs +++ b/embedded-io/src/lib.rs @@ -218,6 +218,10 @@ impl Error for std::io::Error { /// which might be different types. pub trait ErrorType { /// Error type of all the IO operations on this type. + #[cfg(feature = "defmt")] + type Error: Error + defmt::Format; + /// Error type of all the IO operations on this type. + #[cfg(not(feature = "defmt"))] type Error: Error; }