Skip to content

Commit 63ade38

Browse files
authored
Merge pull request #4570 from U007D/sync-debug
`embassy-sync` `Debug` impls
2 parents b5887b2 + 368738b commit 63ade38

19 files changed

+65
-0
lines changed

embassy-sync/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## Unreleased
99

1010
- Add `get_mut` to `LazyLock`
11+
- Add more `Debug` impls to `embassy-sync`, particularly on `OnceLock`
1112

1213
## 0.7.0 - 2025-05-28
1314

embassy-sync/src/blocking_mutex/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use self::raw::RawMutex;
2222
///
2323
/// In all cases, the blocking mutex is intended to be short lived and not held across await points.
2424
/// Use the async [`Mutex`](crate::mutex::Mutex) if you need a lock that is held across await points.
25+
#[derive(Debug)]
2526
pub struct Mutex<R, T: ?Sized> {
2627
// NOTE: `raw` must be FIRST, so when using ThreadModeMutex the "can't drop in non-thread-mode" gets
2728
// to run BEFORE dropping `data`.

embassy-sync/src/blocking_mutex/raw.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub unsafe trait RawMutex {
3737
/// # Safety
3838
///
3939
/// This mutex is safe to share between different executors and interrupts.
40+
#[derive(Debug)]
4041
pub struct CriticalSectionRawMutex {
4142
_phantom: PhantomData<()>,
4243
}
@@ -65,6 +66,7 @@ unsafe impl RawMutex for CriticalSectionRawMutex {
6566
/// # Safety
6667
///
6768
/// **This Mutex is only safe within a single executor.**
69+
#[derive(Debug)]
6870
pub struct NoopRawMutex {
6971
_phantom: PhantomData<*mut ()>,
7072
}

embassy-sync/src/channel.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use crate::blocking_mutex::Mutex;
5555
use crate::waitqueue::WakerRegistration;
5656

5757
/// Send-only access to a [`Channel`].
58+
#[derive(Debug)]
5859
pub struct Sender<'ch, M, T, const N: usize>
5960
where
6061
M: RawMutex,
@@ -241,6 +242,7 @@ impl<'ch, T> SendDynamicSender<'ch, T> {
241242
}
242243

243244
/// Receive-only access to a [`Channel`].
245+
#[derive(Debug)]
244246
pub struct Receiver<'ch, M, T, const N: usize>
245247
where
246248
M: RawMutex,
@@ -486,6 +488,7 @@ where
486488

487489
/// Future returned by [`Channel::receive`] and [`Receiver::receive`].
488490
#[must_use = "futures do nothing unless you `.await` or poll them"]
491+
#[derive(Debug)]
489492
pub struct ReceiveFuture<'ch, M, T, const N: usize>
490493
where
491494
M: RawMutex,
@@ -506,6 +509,7 @@ where
506509

507510
/// Future returned by [`Channel::ready_to_receive`] and [`Receiver::ready_to_receive`].
508511
#[must_use = "futures do nothing unless you `.await` or poll them"]
512+
#[derive(Debug)]
509513
pub struct ReceiveReadyFuture<'ch, M, T, const N: usize>
510514
where
511515
M: RawMutex,
@@ -549,6 +553,7 @@ impl<'ch, M: RawMutex, T, const N: usize> From<ReceiveFuture<'ch, M, T, N>> for
549553

550554
/// Future returned by [`Channel::send`] and [`Sender::send`].
551555
#[must_use = "futures do nothing unless you `.await` or poll them"]
556+
#[derive(Debug)]
552557
pub struct SendFuture<'ch, M, T, const N: usize>
553558
where
554559
M: RawMutex,
@@ -646,6 +651,7 @@ pub enum TrySendError<T> {
646651
Full(T),
647652
}
648653

654+
#[derive(Debug)]
649655
struct ChannelState<T, const N: usize> {
650656
queue: Deque<T, N>,
651657
receiver_waker: WakerRegistration,
@@ -785,6 +791,7 @@ impl<T, const N: usize> ChannelState<T, N> {
785791
/// received from the channel.
786792
///
787793
/// All data sent will become available in the same order as it was sent.
794+
#[derive(Debug)]
788795
pub struct Channel<M, T, const N: usize>
789796
where
790797
M: RawMutex,

embassy-sync/src/lazy_lock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use core::sync::atomic::{AtomicBool, Ordering};
2121
/// let reference = VALUE.get();
2222
/// assert_eq!(reference, &20);
2323
/// ```
24+
#[derive(Debug)]
2425
pub struct LazyLock<T, F = fn() -> T> {
2526
init: AtomicBool,
2627
data: UnsafeCell<Data<T, F>>,
@@ -144,6 +145,7 @@ mod tests {
144145
}
145146

146147
static DROP_CHECKER: AtomicU32 = AtomicU32::new(0);
148+
#[derive(Debug)]
147149
struct DropCheck;
148150

149151
impl Drop for DropCheck {

embassy-sync/src/mutex.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use crate::waitqueue::WakerRegistration;
1616
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1717
pub struct TryLockError;
1818

19+
#[derive(Debug)]
1920
struct State {
2021
locked: bool,
2122
waker: WakerRegistration,

embassy-sync/src/once_lock.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Synchronization primitive for initializing a value once, allowing others to await a reference to the value.
22
33
use core::cell::Cell;
4+
use core::fmt::{Debug, Formatter};
45
use core::future::{poll_fn, Future};
56
use core::mem::MaybeUninit;
67
use core::sync::atomic::{AtomicBool, Ordering};
@@ -42,6 +43,15 @@ pub struct OnceLock<T> {
4243
data: Cell<MaybeUninit<T>>,
4344
}
4445

46+
impl<T> Debug for OnceLock<T> {
47+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
48+
f.debug_struct("OnceLock")
49+
.field("init", &self.init)
50+
.field("data", &"Cell<MaybeUninit<{unprintable}>>")
51+
.finish()
52+
}
53+
}
54+
4555
unsafe impl<T> Sync for OnceLock<T> where T: Sync {}
4656

4757
impl<T> OnceLock<T> {

embassy-sync/src/pipe.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::ring_buffer::RingBuffer;
1313
use crate::waitqueue::WakerRegistration;
1414

1515
/// Write-only access to a [`Pipe`].
16+
#[derive(Debug)]
1617
pub struct Writer<'p, M, const N: usize>
1718
where
1819
M: RawMutex,
@@ -52,6 +53,7 @@ where
5253

5354
/// Future returned by [`Pipe::write`] and [`Writer::write`].
5455
#[must_use = "futures do nothing unless you `.await` or poll them"]
56+
#[derive(Debug)]
5557
pub struct WriteFuture<'p, M, const N: usize>
5658
where
5759
M: RawMutex,
@@ -77,6 +79,7 @@ where
7779
impl<'p, M, const N: usize> Unpin for WriteFuture<'p, M, N> where M: RawMutex {}
7880

7981
/// Read-only access to a [`Pipe`].
82+
#[derive(Debug)]
8083
pub struct Reader<'p, M, const N: usize>
8184
where
8285
M: RawMutex,
@@ -128,6 +131,7 @@ where
128131

129132
/// Future returned by [`Pipe::read`] and [`Reader::read`].
130133
#[must_use = "futures do nothing unless you `.await` or poll them"]
134+
#[derive(Debug)]
131135
pub struct ReadFuture<'p, M, const N: usize>
132136
where
133137
M: RawMutex,
@@ -154,6 +158,7 @@ impl<'p, M, const N: usize> Unpin for ReadFuture<'p, M, N> where M: RawMutex {}
154158

155159
/// Future returned by [`Reader::fill_buf`].
156160
#[must_use = "futures do nothing unless you `.await` or poll them"]
161+
#[derive(Debug)]
157162
pub struct FillBufFuture<'p, M, const N: usize>
158163
where
159164
M: RawMutex,
@@ -199,13 +204,15 @@ pub enum TryWriteError {
199204
Full,
200205
}
201206

207+
#[derive(Debug)]
202208
struct PipeState<const N: usize> {
203209
buffer: RingBuffer<N>,
204210
read_waker: WakerRegistration,
205211
write_waker: WakerRegistration,
206212
}
207213

208214
#[repr(transparent)]
215+
#[derive(Debug)]
209216
struct Buffer<const N: usize>(UnsafeCell<[u8; N]>);
210217

211218
impl<const N: usize> Buffer<N> {
@@ -230,6 +237,7 @@ unsafe impl<const N: usize> Sync for Buffer<N> {}
230237
/// buffer is full, attempts to `write` new bytes will wait until buffer space is freed up.
231238
///
232239
/// All data written will become available in the same order as it was written.
240+
#[derive(Debug)]
233241
pub struct Pipe<M, const N: usize>
234242
where
235243
M: RawMutex,

embassy-sync/src/pubsub/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub use subscriber::{DynSubscriber, Subscriber};
7171
/// # block_on(test);
7272
/// ```
7373
///
74+
#[derive(Debug)]
7475
pub struct PubSubChannel<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> {
7576
inner: Mutex<M, RefCell<PubSubState<T, CAP, SUBS, PUBS>>>,
7677
}
@@ -297,6 +298,7 @@ impl<M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usi
297298
}
298299

299300
/// Internal state for the PubSub channel
301+
#[derive(Debug)]
300302
struct PubSubState<T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize> {
301303
/// The queue contains the last messages that have been published and a countdown of how many subscribers are yet to read it
302304
queue: Deque<(T, usize), CAP>,

embassy-sync/src/pubsub/publisher.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use super::{PubSubBehavior, PubSubChannel};
1010
use crate::blocking_mutex::raw::RawMutex;
1111

1212
/// A publisher to a channel
13+
#[derive(Debug)]
1314
pub struct Pub<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> {
1415
/// The channel we are a publisher for
1516
channel: &'a PSB,
@@ -106,6 +107,7 @@ impl<'a, T: Clone> DerefMut for DynPublisher<'a, T> {
106107
}
107108

108109
/// A publisher that holds a generic reference to the channel
110+
#[derive(Debug)]
109111
pub struct Publisher<'a, M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize>(
110112
pub(super) Pub<'a, PubSubChannel<M, T, CAP, SUBS, PUBS>, T>,
111113
);
@@ -130,6 +132,7 @@ impl<'a, M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS:
130132

131133
/// A publisher that can only use the `publish_immediate` function, but it doesn't have to be registered with the channel.
132134
/// (So an infinite amount is possible)
135+
#[derive(Debug)]
133136
pub struct ImmediatePub<'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> {
134137
/// The channel we are a publisher for
135138
channel: &'a PSB,
@@ -205,6 +208,7 @@ impl<'a, T: Clone> DerefMut for DynImmediatePublisher<'a, T> {
205208
}
206209

207210
/// An immediate publisher that holds a generic reference to the channel
211+
#[derive(Debug)]
208212
pub struct ImmediatePublisher<'a, M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS: usize>(
209213
pub(super) ImmediatePub<'a, PubSubChannel<M, T, CAP, SUBS, PUBS>, T>,
210214
);
@@ -229,6 +233,7 @@ impl<'a, M: RawMutex, T: Clone, const CAP: usize, const SUBS: usize, const PUBS:
229233

230234
#[must_use = "Sinks do nothing unless polled"]
231235
/// [`futures_sink::Sink`] adapter for [`Pub`].
236+
#[derive(Debug)]
232237
pub struct PubSink<'a, 'p, PSB, T>
233238
where
234239
T: Clone,
@@ -290,6 +295,7 @@ where
290295

291296
/// Future for the publisher wait action
292297
#[must_use = "futures do nothing unless you `.await` or poll them"]
298+
#[derive(Debug)]
293299
pub struct PublisherWaitFuture<'s, 'a, PSB: PubSubBehavior<T> + ?Sized, T: Clone> {
294300
/// The message we need to publish
295301
message: Option<T>,

0 commit comments

Comments
 (0)