mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-08-22 00:00:11 +02:00
sync: print MutexGuard inner value for debugging (#1961)
This commit is contained in:
@@ -61,7 +61,6 @@ pub struct Mutex<T> {
|
||||
///
|
||||
/// The lock is automatically released whenever the guard is dropped, at which point `lock`
|
||||
/// will succeed yet again.
|
||||
#[derive(Debug)]
|
||||
pub struct MutexGuard<'a, T> {
|
||||
lock: &'a Mutex<T>,
|
||||
permit: semaphore::Permit,
|
||||
@@ -176,6 +175,12 @@ impl<'a, T> DerefMut for MutexGuard<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: fmt::Debug> fmt::Debug for MutexGuard<'a, T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Debug::fmt(&**self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: fmt::Display> fmt::Display for MutexGuard<'a, T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fmt::Display::fmt(&**self, f)
|
||||
|
||||
@@ -82,8 +82,7 @@ fn lock() {
|
||||
}
|
||||
*/
|
||||
|
||||
#[tokio::main]
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
/// Ensure a mutex is unlocked if a future holding the lock
|
||||
/// is aborted prematurely.
|
||||
async fn aborted_future_1() {
|
||||
@@ -108,8 +107,7 @@ async fn aborted_future_1() {
|
||||
.expect("Mutex is locked");
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
#[test]
|
||||
#[tokio::test]
|
||||
/// This test is similar to `aborted_future_1` but this time the
|
||||
/// aborted future is waiting for the lock.
|
||||
async fn aborted_future_2() {
|
||||
@@ -147,3 +145,10 @@ fn try_lock() {
|
||||
let g3 = m.try_lock();
|
||||
assert_eq!(g3.is_ok(), true);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn debug_format() {
|
||||
let s = "debug";
|
||||
let m = Mutex::new(s.to_string());
|
||||
assert_eq!(format!("{:?}", s), format!("{:?}", m.lock().await));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user