@@ -60,7 +60,6 @@ use std::hash::{Hash, Hasher};
60
60
use std:: io:: { self , IoSlice , IoSliceMut , SeekFrom } ;
61
61
use std:: ops:: { Deref , DerefMut } ;
62
62
use std:: pin:: Pin ;
63
- use std:: sync:: TryLockError ;
64
63
use std:: task:: { Context , Poll } ;
65
64
66
65
use futures_io:: { AsyncRead , AsyncSeek , AsyncWrite } ;
@@ -227,7 +226,7 @@ where
227
226
/// - `impl<T> AsyncWrite for &Mutex<T> where T: AsyncWrite + Unpin {}`
228
227
/// - `impl<T> AsyncSeek for Mutex<T> where T: AsyncSeek + Unpin {}`
229
228
/// - `impl<T> AsyncSeek for &Mutex<T> where T: AsyncSeek + Unpin {}`
230
- pub struct Mutex < T > ( std :: sync :: Mutex < T > ) ;
229
+ pub struct Mutex < T > ( async_lock :: Mutex < T > ) ;
231
230
232
231
impl < T > Mutex < T > {
233
232
/// Creates a new mutex.
@@ -257,7 +256,7 @@ impl<T> Mutex<T> {
257
256
/// assert_eq!(*guard, 10);
258
257
/// ```
259
258
pub fn lock ( & self ) -> MutexGuard < ' _ , T > {
260
- MutexGuard ( self . 0 . lock ( ) . unwrap_or_else ( |e| e . into_inner ( ) ) )
259
+ MutexGuard ( self . 0 . lock_blocking ( ) )
261
260
}
262
261
263
262
/// Attempts to acquire the mutex.
@@ -279,16 +278,7 @@ impl<T> Mutex<T> {
279
278
/// # ;
280
279
/// ```
281
280
pub fn try_lock ( & self ) -> Option < MutexGuard < ' _ , T > > {
282
- self . 0
283
- . try_lock ( )
284
- . map_or_else (
285
- |e| match e {
286
- TryLockError :: Poisoned ( e) => Some ( e. into_inner ( ) ) ,
287
- TryLockError :: WouldBlock => None ,
288
- } ,
289
- Some ,
290
- )
291
- . map ( MutexGuard )
281
+ self . 0 . try_lock ( ) . map ( MutexGuard )
292
282
}
293
283
294
284
/// Consumes the mutex, returning the underlying data.
@@ -302,7 +292,7 @@ impl<T> Mutex<T> {
302
292
/// assert_eq!(mutex.into_inner(), 10);
303
293
/// ```
304
294
pub fn into_inner ( self ) -> T {
305
- self . 0 . into_inner ( ) . unwrap_or_else ( |e| e . into_inner ( ) )
295
+ self . 0 . into_inner ( )
306
296
}
307
297
308
298
/// Returns a mutable reference to the underlying data.
@@ -320,7 +310,7 @@ impl<T> Mutex<T> {
320
310
/// assert_eq!(*mutex.lock(), 10);
321
311
/// ```
322
312
pub fn get_mut ( & mut self ) -> & mut T {
323
- self . 0 . get_mut ( ) . unwrap_or_else ( |e| e . into_inner ( ) )
313
+ self . 0 . get_mut ( )
324
314
}
325
315
}
326
316
@@ -461,7 +451,7 @@ impl<T: AsyncSeek + Unpin> AsyncSeek for &Mutex<T> {
461
451
}
462
452
463
453
/// A guard that releases the mutex when dropped.
464
- pub struct MutexGuard < ' a , T > ( std :: sync :: MutexGuard < ' a , T > ) ;
454
+ pub struct MutexGuard < ' a , T > ( async_lock :: MutexGuard < ' a , T > ) ;
465
455
466
456
impl < T : fmt:: Debug > fmt:: Debug for MutexGuard < ' _ , T > {
467
457
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
@@ -488,3 +478,27 @@ impl<T> DerefMut for MutexGuard<'_, T> {
488
478
& mut self . 0
489
479
}
490
480
}
481
+
482
+ #[ cfg( test) ]
483
+ mod tests {
484
+ use super :: * ;
485
+
486
+ fn is_send < T : Send > ( _: & T ) { }
487
+ fn is_sync < T : Sync > ( _: & T ) { }
488
+
489
+ #[ test]
490
+ fn is_send_sync ( ) {
491
+ let arc = Arc :: new ( ( ) ) ;
492
+ let mutex = Mutex :: new ( ( ) ) ;
493
+
494
+ is_send ( & arc) ;
495
+ is_sync ( & arc) ;
496
+
497
+ is_send ( & mutex) ;
498
+ is_sync ( & mutex) ;
499
+
500
+ let guard = mutex. lock ( ) ;
501
+ is_send ( & guard) ;
502
+ is_sync ( & guard) ;
503
+ }
504
+ }
0 commit comments