@@ -573,6 +573,32 @@ impl<'a, T: ?Sized> MutexGuard<'a, T> {
573573 marker : marker:: PhantomData ,
574574 } )
575575 }
576+
577+ /// Returns a reference to the original `Mutex`.
578+ ///
579+ /// ```
580+ /// use tokio::sync::{Mutex, MutexGuard};
581+ ///
582+ /// async fn unlock_and_relock<'l>(guard: MutexGuard<'l, u32>) -> MutexGuard<'l, u32> {
583+ /// println!("1. contains: {:?}", *guard);
584+ /// let mutex = MutexGuard::mutex(&guard);
585+ /// drop(guard);
586+ /// let guard = mutex.lock().await;
587+ /// println!("2. contains: {:?}", *guard);
588+ /// guard
589+ /// }
590+ /// #
591+ /// # #[tokio::main]
592+ /// # async fn main() {
593+ /// # let mutex = Mutex::new(0u32);
594+ /// # let guard = mutex.lock().await;
595+ /// # unlock_and_relock(guard).await;
596+ /// # }
597+ /// ```
598+ #[ inline]
599+ pub fn mutex ( this : & Self ) -> & ' a Mutex < T > {
600+ this. lock
601+ }
576602}
577603
578604impl < T : ?Sized > Drop for MutexGuard < ' _ , T > {
@@ -608,6 +634,35 @@ impl<T: ?Sized + fmt::Display> fmt::Display for MutexGuard<'_, T> {
608634
609635// === impl OwnedMutexGuard ===
610636
637+ impl < T : ?Sized > OwnedMutexGuard < T > {
638+ /// Returns a reference to the original `Arc<Mutex>`.
639+ ///
640+ /// ```
641+ /// use std::sync::Arc;
642+ /// use tokio::sync::{Mutex, OwnedMutexGuard};
643+ ///
644+ /// async fn unlock_and_relock(guard: OwnedMutexGuard<u32>) -> OwnedMutexGuard<u32> {
645+ /// println!("1. contains: {:?}", *guard);
646+ /// let mutex: Arc<Mutex<u32>> = OwnedMutexGuard::mutex(&guard).clone();
647+ /// drop(guard);
648+ /// let guard = mutex.lock_owned().await;
649+ /// println!("2. contains: {:?}", *guard);
650+ /// guard
651+ /// }
652+ /// #
653+ /// # #[tokio::main]
654+ /// # async fn main() {
655+ /// # let mutex = Arc::new(Mutex::new(0u32));
656+ /// # let guard = mutex.lock_owned().await;
657+ /// # unlock_and_relock(guard).await;
658+ /// # }
659+ /// ```
660+ #[ inline]
661+ pub fn mutex ( this : & Self ) -> & Arc < Mutex < T > > {
662+ & this. lock
663+ }
664+ }
665+
611666impl < T : ?Sized > Drop for OwnedMutexGuard < T > {
612667 fn drop ( & mut self ) {
613668 self . lock . s . release ( 1 )
0 commit comments