diff options
Diffstat (limited to 'embassy-sync')
| -rw-r--r-- | embassy-sync/src/mutex.rs | 78 |
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. |
| 4 | use core::cell::{RefCell, UnsafeCell}; | 4 | use core::cell::{RefCell, UnsafeCell}; |
| 5 | use core::future::poll_fn; | 5 | use core::future::poll_fn; |
| 6 | use core::mem; | ||
| 7 | use core::ops::{Deref, DerefMut}; | 6 | use core::ops::{Deref, DerefMut}; |
| 8 | use core::task::Poll; | 7 | use core::task::Poll; |
| 8 | use core::{fmt, mem}; | ||
| 9 | 9 | ||
| 10 | use crate::blocking_mutex::raw::RawMutex; | 10 | use crate::blocking_mutex::raw::RawMutex; |
| 11 | use crate::blocking_mutex::Mutex as BlockingMutex; | 11 | use crate::blocking_mutex::Mutex as BlockingMutex; |
| @@ -129,6 +129,42 @@ where | |||
| 129 | } | 129 | } |
| 130 | } | 130 | } |
| 131 | 131 | ||
| 132 | impl<M: RawMutex, T> From<T> for Mutex<M, T> { | ||
| 133 | fn from(from: T) -> Self { | ||
| 134 | Self::new(from) | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | impl<M, T> Default for Mutex<M, T> | ||
| 139 | where | ||
| 140 | M: RawMutex, | ||
| 141 | T: ?Sized + Default, | ||
| 142 | { | ||
| 143 | fn default() -> Self { | ||
| 144 | Self::new(Default::default()) | ||
| 145 | } | ||
| 146 | } | ||
| 147 | |||
| 148 | impl<M, T> fmt::Debug for Mutex<M, T> | ||
| 149 | where | ||
| 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 | ||
| 241 | impl<'a, M, T> fmt::Debug for MutexGuard<'a, M, T> | ||
| 242 | where | ||
| 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 | |||
| 251 | impl<'a, M, T> fmt::Display for MutexGuard<'a, M, T> | ||
| 252 | where | ||
| 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 | ||
| 344 | impl<'a, M, T> fmt::Debug for MappedMutexGuard<'a, M, T> | ||
| 345 | where | ||
| 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 | |||
| 354 | impl<'a, M, T> fmt::Display for MappedMutexGuard<'a, M, T> | ||
| 355 | where | ||
| 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)] |
| 289 | mod tests { | 365 | mod tests { |
| 290 | use crate::blocking_mutex::raw::NoopRawMutex; | 366 | use crate::blocking_mutex::raw::NoopRawMutex; |
