Skip to content

Commit 9ab9174

Browse files
committed
add impl Format for std::sync::Mutex
this introduces a new optional feature `std` and an initial `Format` implementation just for `Mutex`. the hope is that over time more and more `Format` impls for `std` can be added for those who need it. the trigger for this is that i broke the `embassy-sync` build for `std` by adding a `Format` derive to something which was using `Mutex` internally and it wasn't covered by CI there (which by now is rectified). in the ensuing conversation on Matrix it was stated that it might be nice to have `defmt` impls for `std` as well, even if only very few users would exist for this (i'm not one of them, which is how i ended up breaking CI). part of #979
1 parent 8e517f8 commit 9ab9174

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ We have several packages which live in this repository. Changes are tracked sepa
5858
* [#956]: Link LICENSE-* in the crate folder
5959
* [#955]: Allow using the `defmt/alloc` feature on bare metal ESP32-S2
6060
* [#972]: Fix logic bug in env_filter
61+
* [#978]: Add `std` feature and add initial `Format` impl for `Mutex` and `MutexGuard`
6162

6263
### [defmt-v1.0.1] (2025-04-01)
6364

defmt/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ homepage = "https://knurling.ferrous-systems.com/"
1818
version = "1.0.1"
1919

2020
[features]
21+
std = ["alloc"]
2122
alloc = []
2223
avoid-default-panic = []
2324
ip_in_core = []

defmt/src/impls/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ mod alloc_;
3333
mod arrays;
3434
mod core_;
3535
mod primitives;
36+
#[cfg(feature = "std")]
37+
mod std_;
3638
mod tuples;
3739

3840
use defmt_macros::internp;

defmt/src/impls/std_.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use super::*;
2+
use std::sync::TryLockError;
3+
4+
impl<T> Format for std::sync::Mutex<T>
5+
where
6+
T: ?Sized + Format,
7+
{
8+
#[inline]
9+
fn format(&self, fmt: Formatter) {
10+
match self.try_lock() {
11+
Ok(guard) => crate::write!(fmt, "Mutex {{ data: {=?} }}", guard),
12+
Err(TryLockError::Poisoned(err)) => {
13+
crate::write!(fmt, "Mutex {{ data: {=?} }}", err.get_ref())
14+
}
15+
Err(TryLockError::WouldBlock) => crate::write!(fmt, "Mutex {{ data: <locked> }}"),
16+
}
17+
}
18+
}
19+
20+
impl<T> Format for std::sync::MutexGuard<'_, T>
21+
where
22+
T: ?Sized + Format,
23+
{
24+
delegate_format!(T, self, &*self);
25+
}
26+
27+
#[cfg(test)]
28+
mod tests {
29+
use crate as defmt;
30+
use std::sync::Mutex;
31+
32+
#[test]
33+
fn mutex() {
34+
let m = Mutex::new(42);
35+
defmt::info!("{}", m);
36+
}
37+
}

0 commit comments

Comments
 (0)