aboutsummaryrefslogtreecommitdiff
path: root/embassy-sync
diff options
context:
space:
mode:
Diffstat (limited to 'embassy-sync')
-rw-r--r--embassy-sync/src/mutex.rs78
1 files changed, 77 insertions, 1 deletions
diff --git a/embassy-sync/src/mutex.rs b/embassy-sync/src/mutex.rs
index b48a408c4..8c3a3af9f 100644
--- a/embassy-sync/src/mutex.rs
+++ b/embassy-sync/src/mutex.rs
@@ -3,9 +3,9 @@
3//! This module provides a mutex that can be used to synchronize data between asynchronous tasks. 3//! This module provides a mutex that can be used to synchronize data between asynchronous tasks.
4use core::cell::{RefCell, UnsafeCell}; 4use core::cell::{RefCell, UnsafeCell};
5use core::future::poll_fn; 5use core::future::poll_fn;
6use core::mem;
7use core::ops::{Deref, DerefMut}; 6use core::ops::{Deref, DerefMut};
8use core::task::Poll; 7use core::task::Poll;
8use core::{fmt, mem};
9 9
10use crate::blocking_mutex::raw::RawMutex; 10use crate::blocking_mutex::raw::RawMutex;
11use crate::blocking_mutex::Mutex as BlockingMutex; 11use crate::blocking_mutex::Mutex as BlockingMutex;
@@ -129,6 +129,42 @@ where
129 } 129 }
130} 130}
131 131
132impl<M: RawMutex, T> From<T> for Mutex<M, T> {
133 fn from(from: T) -> Self {
134 Self::new(from)
135 }
136}
137
138impl<M, T> Default for Mutex<M, T>
139where
140 M: RawMutex,
141 T: ?Sized + Default,
142{
143 fn default() -> Self {
144 Self::new(Default::default())
145 }
146}
147
148impl<M, T> fmt::Debug for Mutex<M, T>
149where
150 M: RawMutex,
151 T: ?Sized + fmt::Debug,
152{
153 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
154 let mut d = f.debug_struct("Mutex");
155 match self.try_lock() {
156 Ok(value) => {
157 d.field("inner", &&*value);
158 }
159 Err(TryLockError) => {
160 d.field("inner", &format_args!("<locked>"));
161 }
162 }
163
164 d.finish_non_exhaustive()
165 }
166}
167
132/// Async mutex guard. 168/// Async mutex guard.
133/// 169///
134/// Owning an instance of this type indicates having 170/// Owning an instance of this type indicates having
@@ -202,6 +238,26 @@ where
202 } 238 }
203} 239}
204 240
241impl<'a, M, T> fmt::Debug for MutexGuard<'a, M, T>
242where
243 M: RawMutex,
244 T: ?Sized + fmt::Debug,
245{
246 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
247 fmt::Debug::fmt(&**self, f)
248 }
249}
250
251impl<'a, M, T> fmt::Display for MutexGuard<'a, M, T>
252where
253 M: RawMutex,
254 T: ?Sized + fmt::Display,
255{
256 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
257 fmt::Display::fmt(&**self, f)
258 }
259}
260
205/// A handle to a held `Mutex` that has had a function applied to it via [`MutexGuard::map`] or 261/// A handle to a held `Mutex` that has had a function applied to it via [`MutexGuard::map`] or
206/// [`MappedMutexGuard::map`]. 262/// [`MappedMutexGuard::map`].
207/// 263///
@@ -285,6 +341,26 @@ where
285{ 341{
286} 342}
287 343
344impl<'a, M, T> fmt::Debug for MappedMutexGuard<'a, M, T>
345where
346 M: RawMutex,
347 T: ?Sized + fmt::Debug,
348{
349 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
350 fmt::Debug::fmt(&**self, f)
351 }
352}
353
354impl<'a, M, T> fmt::Display for MappedMutexGuard<'a, M, T>
355where
356 M: RawMutex,
357 T: ?Sized + fmt::Display,
358{
359 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
360 fmt::Display::fmt(&**self, f)
361 }
362}
363
288#[cfg(test)] 364#[cfg(test)]
289mod tests { 365mod tests {
290 use crate::blocking_mutex::raw::NoopRawMutex; 366 use crate::blocking_mutex::raw::NoopRawMutex;