From 170121cdf6fb1b677be676ee24643437ecfb064f Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Sat, 21 Aug 2021 22:36:23 +0100 Subject: [PATCH 1/3] Add defmt support for mpsc errors. --- embassy/src/util/mpsc.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/embassy/src/util/mpsc.rs b/embassy/src/util/mpsc.rs index bad1058a..b9a95d6a 100644 --- a/embassy/src/util/mpsc.rs +++ b/embassy/src/util/mpsc.rs @@ -386,6 +386,7 @@ where /// /// [`try_recv`]: super::Receiver::try_recv #[derive(PartialEq, Eq, Clone, Copy, Debug)] +#[cfg_attr(feature = "defmt", derive(defmt::Format))] pub enum TryRecvError { /// A message could not be received because the channel is empty. Empty, @@ -404,6 +405,14 @@ impl fmt::Display for SendError { } } +#[cfg(feature = "defmt")] +impl defmt::Format for SendError { + fn format(&self, fmt: defmt::Formatter<'_>) { + defmt::write!(fmt, "channel closed") + } +} + + /// This enumeration is the list of the possible error outcomes for the /// [try_send](super::Sender::try_send) method. #[derive(Debug)] @@ -430,6 +439,20 @@ impl fmt::Display for TrySendError { } } +#[cfg(feature = "defmt")] +impl defmt::Format for TrySendError { + fn format(&self, fmt: defmt::Formatter<'_>) { + defmt::write!( + fmt, + "{}", + match self { + TrySendError::Full(..) => "no available capacity", + TrySendError::Closed(..) => "channel closed", + } + ) + } +} + struct ChannelState { buf: [MaybeUninit>; N], read_pos: usize, From 4d3c07f71b9402bf1735b7f81035d90e6de7a28e Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Sat, 21 Aug 2021 22:38:02 +0100 Subject: [PATCH 2/3] Fix fmt --- embassy/src/util/mpsc.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/embassy/src/util/mpsc.rs b/embassy/src/util/mpsc.rs index b9a95d6a..364b98aa 100644 --- a/embassy/src/util/mpsc.rs +++ b/embassy/src/util/mpsc.rs @@ -412,7 +412,6 @@ impl defmt::Format for SendError { } } - /// This enumeration is the list of the possible error outcomes for the /// [try_send](super::Sender::try_send) method. #[derive(Debug)] From 7ca745a5c0bf8cdee578ee1108e78b73b7161f4e Mon Sep 17 00:00:00 2001 From: Richard Dodd Date: Sun, 22 Aug 2021 00:11:19 +0100 Subject: [PATCH 3/3] Refactor to allow defmt to intern strings. --- embassy/src/util/mpsc.rs | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/embassy/src/util/mpsc.rs b/embassy/src/util/mpsc.rs index 364b98aa..4257ecd4 100644 --- a/embassy/src/util/mpsc.rs +++ b/embassy/src/util/mpsc.rs @@ -441,14 +441,10 @@ impl fmt::Display for TrySendError { #[cfg(feature = "defmt")] impl defmt::Format for TrySendError { fn format(&self, fmt: defmt::Formatter<'_>) { - defmt::write!( - fmt, - "{}", - match self { - TrySendError::Full(..) => "no available capacity", - TrySendError::Closed(..) => "channel closed", - } - ) + match self { + TrySendError::Full(..) => defmt::write!(fmt, "no available capacity"), + TrySendError::Closed(..) => defmt::write!(fmt, "channel closed"), + } } }