Skip to content

Commit 2e19d4a

Browse files
committed
Add defmt::Format bound to error types
1 parent b8fd20d commit 2e19d4a

10 files changed

Lines changed: 44 additions & 5 deletions

File tree

embedded-can/src/blocking.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ pub trait Can {
66
type Frame: crate::Frame;
77

88
/// Associated error type.
9+
#[cfg(feature = "defmt")]
10+
type Error: crate::Error + defmt::Format;
11+
/// Associated error type.
12+
#[cfg(not(feature = "defmt"))]
913
type Error: crate::Error;
1014

1115
/// Puts a frame in the transmit buffer. Blocks until space is available in

embedded-can/src/nb.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ pub trait Can {
66
type Frame: crate::Frame;
77

88
/// Associated error type.
9+
#[cfg(feature = "defmt")]
10+
type Error: crate::Error + defmt::Format;
11+
/// Associated error type.
12+
#[cfg(not(feature = "defmt"))]
913
type Error: crate::Error;
1014

1115
/// Puts a frame in the transmit buffer to be sent on the bus.

embedded-hal-bus/src/i2c/atomic.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use embedded_hal::i2c::{Error, ErrorKind, ErrorType, I2c};
22

3+
#[cfg(feature = "defmt-03")]
4+
use crate::defmt;
35
use crate::util::AtomicCell;
46

57
/// Atomics-based shared bus [`I2c`] implementation.
@@ -30,6 +32,8 @@ use crate::util::AtomicCell;
3032
/// use embedded_hal_bus::i2c;
3133
/// use embedded_hal_bus::util::AtomicCell;
3234
/// # use embedded_hal::i2c::{self as hali2c, SevenBitAddress, TenBitAddress, I2c, Operation, ErrorKind};
35+
/// # #[cfg(feature = "defmt-03")]
36+
/// # use embedded_hal::defmt;
3337
/// # pub struct Sensor<I2C> {
3438
/// # i2c: I2C,
3539
/// # address: u8,
@@ -42,6 +46,7 @@ use crate::util::AtomicCell;
4246
/// # type PressureSensor<I2C> = Sensor<I2C>;
4347
/// # type TemperatureSensor<I2C> = Sensor<I2C>;
4448
/// # pub struct I2c0;
49+
/// # #[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
4550
/// # #[derive(Debug, Copy, Clone, Eq, PartialEq)]
4651
/// # pub enum Error { }
4752
/// # impl hali2c::Error for Error {
@@ -80,6 +85,7 @@ pub struct AtomicDevice<'a, T> {
8085
bus: &'a AtomicCell<T>,
8186
}
8287

88+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
8389
#[derive(Debug, Copy, Clone)]
8490
/// Wrapper type for errors originating from the atomically-checked I2C bus manager.
8591
pub enum AtomicError<T: Error> {

embedded-hal-bus/src/i2c/refcell.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use embedded_hal::i2c::{ErrorType, I2c};
1717
/// use embedded_hal_bus::i2c;
1818
/// use core::cell::RefCell;
1919
/// # use embedded_hal::i2c::{self as hali2c, SevenBitAddress, TenBitAddress, I2c, Operation, ErrorKind};
20+
/// # #[cfg(feature = "defmt-03")]
21+
/// # use embedded_hal::defmt;
2022
/// # pub struct Sensor<I2C> {
2123
/// # i2c: I2C,
2224
/// # address: u8,
@@ -29,6 +31,7 @@ use embedded_hal::i2c::{ErrorType, I2c};
2931
/// # type PressureSensor<I2C> = Sensor<I2C>;
3032
/// # type TemperatureSensor<I2C> = Sensor<I2C>;
3133
/// # pub struct I2c0;
34+
/// # #[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
3235
/// # #[derive(Debug, Copy, Clone, Eq, PartialEq)]
3336
/// # pub enum Error { }
3437
/// # impl hali2c::Error for Error {

embedded-hal-bus/src/spi/atomic.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use embedded_hal::digital::OutputPin;
33
use embedded_hal::spi::{Error, ErrorKind, ErrorType, Operation, SpiBus, SpiDevice};
44

55
use super::DeviceError;
6+
#[cfg(feature = "defmt-03")]
7+
use crate::defmt;
68
use crate::spi::shared::transaction;
79
use crate::util::AtomicCell;
810

@@ -36,12 +38,13 @@ pub struct AtomicDevice<'a, BUS, CS, D> {
3638
delay: D,
3739
}
3840

39-
#[derive(Debug, Copy, Clone)]
4041
/// Wrapper type for errors returned by [`AtomicDevice`].
42+
#[derive(Debug, Copy, Clone)]
4143
#[cfg_attr(
4244
docsrs,
4345
doc(cfg(any(feature = "portable-atomic", target_has_atomic = "8")))
4446
)]
47+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
4548
pub enum AtomicError<T: Error> {
4649
/// This error is returned if the SPI bus was already in use when an operation was attempted,
4750
/// which indicates that the driver requirements are not being met with regard to

embedded-hal/src/digital.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ impl core::fmt::Display for ErrorKind {
5959
///
6060
/// This just defines the error type, to be used by the other traits.
6161
pub trait ErrorType {
62-
/// Error type
62+
/// Error type.
63+
#[cfg(feature = "defmt-03")]
64+
type Error: Error + defmt::Format;
65+
/// Error type.
66+
#[cfg(not(feature = "defmt-03"))]
6367
type Error: Error;
6468
}
6569

embedded-hal/src/i2c.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,13 @@
125125
//!
126126
//! ```
127127
//! use embedded_hal::i2c::{self, SevenBitAddress, TenBitAddress, I2c, Operation};
128+
//! #[cfg(feature = "defmt-03")]
129+
//! use embedded_hal::defmt;
128130
//!
129131
//! /// I2C0 hardware peripheral which supports both 7-bit and 10-bit addressing.
130132
//! pub struct I2c0;
131133
//!
134+
//! #[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
132135
//! #[derive(Debug, Copy, Clone, Eq, PartialEq)]
133136
//! pub enum Error {
134137
//! // ...
@@ -265,7 +268,11 @@ impl core::fmt::Display for NoAcknowledgeSource {
265268
///
266269
/// This just defines the error type, to be used by the other traits.
267270
pub trait ErrorType {
268-
/// Error type
271+
/// Error type.
272+
#[cfg(feature = "defmt-03")]
273+
type Error: Error + defmt::Format;
274+
/// Error type.
275+
#[cfg(not(feature = "defmt-03"))]
269276
type Error: Error;
270277
}
271278

embedded-hal/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ mod private {
1818

1919
// needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`.
2020
#[cfg(feature = "defmt-03")]
21-
use defmt_03 as defmt;
21+
pub use defmt_03 as defmt;

embedded-hal/src/pwm.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,11 @@ impl core::fmt::Display for ErrorKind {
5858
///
5959
/// This just defines the error type, to be used by the other traits.
6060
pub trait ErrorType {
61-
/// Error type
61+
/// Error type.
62+
#[cfg(feature = "defmt-03")]
63+
type Error: Error + defmt::Format;
64+
/// Error type.
65+
#[cfg(not(feature = "defmt-03"))]
6266
type Error: Error;
6367
}
6468

embedded-hal/src/spi.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,10 @@ impl core::fmt::Display for ErrorKind {
307307
/// This just defines the error type, to be used by the other SPI traits.
308308
pub trait ErrorType {
309309
/// Error type.
310+
#[cfg(feature = "defmt-03")]
311+
type Error: Error + defmt::Format;
312+
/// Error type.
313+
#[cfg(not(feature = "defmt-03"))]
310314
type Error: Error;
311315
}
312316

0 commit comments

Comments
 (0)