Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Implement Debug trait for sync variants of FrozenMap and FrozenVec #65

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ use std::alloc::Layout;
use std::borrow::Borrow;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
use std::iter::{FromIterator, IntoIterator};
use std::ops::Index;

use std::sync::TryLockError;
use std::sync::atomic::AtomicBool;
use std::sync::atomic::AtomicPtr;
use std::sync::atomic::AtomicUsize;
Expand All @@ -27,6 +29,28 @@ pub struct FrozenMap<K, V> {
map: RwLock<HashMap<K, V>>,
}

impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for FrozenMap<K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.map.try_read() {
Ok(guard) => {
guard.fmt(f)
},
Err(TryLockError::Poisoned(err)) => {
f.debug_tuple("FrozenMap").field(&&**err.get_ref()).finish()
}
Err(TryLockError::WouldBlock) => {
struct LockedPlaceholder;
impl fmt::Debug for LockedPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<locked>")
}
}
f.debug_tuple("FrozenMap").field(&LockedPlaceholder).finish()
},
}
}
}

impl<K, V> Default for FrozenMap<K, V> {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -387,6 +411,28 @@ pub struct FrozenVec<T> {
vec: RwLock<Vec<T>>,
}

impl<T: fmt::Debug> fmt::Debug for FrozenVec<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.vec.try_read() {
Ok(guard) => {
guard.fmt(f)
},
Err(TryLockError::Poisoned(err)) => {
f.debug_tuple("FrozenMap").field(&&**err.get_ref()).finish()
}
Err(TryLockError::WouldBlock) => {
struct LockedPlaceholder;
impl fmt::Debug for LockedPlaceholder {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<locked>")
}
}
f.debug_tuple("FrozenMap").field(&LockedPlaceholder).finish()
},
}
}
}

impl<T> FrozenVec<T> {
/// Returns the number of elements in the vector.
pub fn len(&self) -> usize {
Expand Down
Loading