Skip to content

Commit

Permalink
refactor: Refactor debug implementations in sync.rs
Browse files Browse the repository at this point in the history
- Replaces the `debug_struct` method with straight `debug_map` for `FrozenMap` and `debug_list` for `FrozenVec`
- Simplifies error handling by moving to `f.debug_tuple()` method instead of the previous `field()` calls based on `DebugStruct`
  • Loading branch information
huitseeker committed Oct 9, 2023
1 parent 666915e commit f2fac5b
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@ pub struct FrozenMap<K, V> {

impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for FrozenMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct("FrozenMap");
match self.map.try_read() {
Ok(guard) => {
d.field("map", &&*guard);
f.debug_map().entries(guard.iter()).finish()
},
Err(TryLockError::Poisoned(err)) => {
d.field("map", &&**err.get_ref());
f.debug_tuple("FrozenMap").field(&&**err.get_ref()).finish()
}
Err(TryLockError::WouldBlock) => {
struct LockedPlaceholder;
Expand All @@ -46,10 +45,9 @@ impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for FrozenMap<K, V> {
f.write_str("<locked>")
}
}
d.field("map", &LockedPlaceholder);
f.debug_tuple("FrozenMap").field(&LockedPlaceholder).finish()
},
}
d.finish_non_exhaustive()
}
}

Expand Down Expand Up @@ -415,13 +413,12 @@ pub struct FrozenVec<T> {

impl<T: fmt::Debug> fmt::Debug for FrozenVec<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct("FrozenVec");
match self.vec.try_read() {
Ok(guard) => {
d.field("vec", &&*guard);
f.debug_list().entries(guard.iter()).finish()
},
Err(TryLockError::Poisoned(err)) => {
d.field("vec", &&**err.get_ref());
f.debug_tuple("FrozenMap").field(&&**err.get_ref()).finish()
}
Err(TryLockError::WouldBlock) => {
struct LockedPlaceholder;
Expand All @@ -430,10 +427,9 @@ impl<T: fmt::Debug> fmt::Debug for FrozenVec<T> {
f.write_str("<locked>")
}
}
d.field("vec", &LockedPlaceholder);
f.debug_tuple("FrozenMap").field(&LockedPlaceholder).finish()
},
}
d.finish_non_exhaustive()
}
}

Expand Down

0 comments on commit f2fac5b

Please sign in to comment.