Skip to content

Commit 35b0dc3

Browse files
committed
io: add defmt support.
1 parent 428c6a1 commit 35b0dc3

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

embedded-io-async/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ categories = [
1414
[features]
1515
std = ["alloc", "embedded-io/std"]
1616
alloc = ["embedded-io/alloc"]
17+
defmt-03 = ["dep:defmt-03", "embedded-io/defmt-03"]
1718

1819
[dependencies]
1920
embedded-io = { version = "0.5", path = "../embedded-io" }
21+
defmt-03 = { package = "defmt", version = "0.3", optional = true }
2022

2123
[package.metadata.docs.rs]
2224
features = ["std"]

embedded-io/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ categories = [
1515
std = ["alloc"]
1616
alloc = []
1717

18+
[dependencies]
19+
defmt-03 = { package = "defmt", version = "0.3", optional = true }
20+
1821
[package.metadata.docs.rs]
1922
features = ["std"]
2023
rustdoc-args = ["--cfg", "docsrs"]

embedded-io/src/lib.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55

66
use core::fmt;
77

8+
// needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`.
9+
#[cfg(feature = "defmt-03")]
10+
use defmt_03 as defmt;
11+
812
#[cfg(feature = "alloc")]
913
extern crate alloc;
1014

@@ -14,6 +18,7 @@ mod impls;
1418
///
1519
/// This is the `embedded-io` equivalent of [`std::io::SeekFrom`].
1620
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
21+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
1722
pub enum SeekFrom {
1823
/// Sets the offset to the provided number of bytes.
1924
Start(u64),
@@ -47,8 +52,6 @@ impl From<std::io::SeekFrom> for SeekFrom {
4752
}
4853
}
4954

50-
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
51-
#[non_exhaustive]
5255
/// Possible kinds of errors.
5356
///
5457
/// This list is intended to grow over time and it is not recommended to
@@ -59,6 +62,9 @@ impl From<std::io::SeekFrom> for SeekFrom {
5962
///
6063
/// - `WouldBlock` is removed, since `embedded-io` traits are always blocking. See the [crate-level documentation](crate) for details.
6164
/// - `WriteZero` is removed, since it is a separate variant in [`WriteAllError`] and [`WriteFmtError`].
65+
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
66+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
67+
#[non_exhaustive]
6268
pub enum ErrorKind {
6369
/// Unspecified error kind.
6470
Other,
@@ -214,6 +220,7 @@ impl<T: ?Sized + ErrorType> ErrorType for &mut T {
214220

215221
/// Error returned by [`Read::read_exact`]
216222
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
223+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
217224
pub enum ReadExactError<E> {
218225
/// An EOF error was encountered before reading the exact amount of requested bytes.
219226
UnexpectedEof,
@@ -266,6 +273,7 @@ impl<E: fmt::Debug> std::error::Error for ReadExactError<E> {}
266273

267274
/// Error returned by [`Write::write_fmt`]
268275
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
276+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
269277
pub enum WriteFmtError<E> {
270278
/// [`Write::write`] wrote zero bytes
271279
WriteZero,
@@ -293,6 +301,7 @@ impl<E: fmt::Debug> std::error::Error for WriteFmtError<E> {}
293301

294302
/// Error returned by [`Write::write_all`]
295303
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
304+
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
296305
pub enum WriteAllError<E> {
297306
/// [`Write::write`] wrote zero bytes
298307
WriteZero,

0 commit comments

Comments
 (0)