From 166078c29d8a40d3a7f6c972b655ce78311fb3fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Garillot?= Date: Thu, 12 Oct 2023 17:10:39 -0400 Subject: [PATCH] refactor: Refactor sync structures and improve debugging output - Renamed and relocated `sync_index_set.rs` and `sync_index_map.rs` files into a new `sync` directory for better file structuring. - Enabled custom `Debug` implementations for sync variants of `FrozenIndexSet` and `FrozenIndexMap`. - Introduced feature-gated support for `indexmap` within `sync` with `index_map` and `index_set` modules. --- src/lib.rs | 4 -- src/{sync_index_map.rs => sync/index_map.rs} | 42 +++++++++++++++----- src/{sync_index_set.rs => sync/index_set.rs} | 40 ++++++++++++++----- src/{sync.rs => sync/mod.rs} | 5 +++ 4 files changed, 65 insertions(+), 26 deletions(-) rename src/{sync_index_map.rs => sync/index_map.rs} (85%) rename src/{sync_index_set.rs => sync/index_set.rs} (84%) rename src/{sync.rs => sync/mod.rs} (99%) diff --git a/src/lib.rs b/src/lib.rs index ec8f323..848dceb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,10 +17,6 @@ pub mod vec; pub mod index_map; #[cfg(feature = "indexmap")] pub mod index_set; -#[cfg(feature = "indexmap")] -pub mod sync_index_map; -#[cfg(feature = "indexmap")] -pub mod sync_index_set; pub mod sync; diff --git a/src/sync_index_map.rs b/src/sync/index_map.rs similarity index 85% rename from src/sync_index_map.rs rename to src/sync/index_map.rs index 3fb8e6f..e9d7475 100644 --- a/src/sync_index_map.rs +++ b/src/sync/index_map.rs @@ -1,21 +1,41 @@ use std::borrow::Borrow; use std::collections::hash_map::RandomState; +use std::fmt; use std::hash::{BuildHasher, Hash}; use std::iter::FromIterator; use std::ops::Index; use indexmap::IndexMap; use stable_deref_trait::StableDeref; -use std::sync::RwLock; +use std::sync::{RwLock, TryLockError}; /// Append-only threadsafe version of `indexmap::IndexMap` where /// insertion does not require mutable access -#[derive(Debug)] pub struct FrozenIndexMap { map: RwLock>, } -// safety: UnsafeCell implies !Sync +impl fmt::Debug for FrozenIndexMap { + 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("FrozenIndexMap").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("") + } + } + f.debug_tuple("FrozenIndexMap") + .field(&LockedPlaceholder) + .finish() + } + } + } +} impl FrozenIndexMap { pub fn new() -> Self { @@ -41,7 +61,7 @@ impl FrozenIndexMap { /// /// # Example /// ``` - /// use elsa::sync_index_map::FrozenIndexMap; + /// use elsa::sync::index_map::FrozenIndexMap; /// let map = FrozenIndexMap::new(); /// assert_eq!(map.insert(1, Box::new("a")), &"a"); /// assert_eq!(map.insert(1, Box::new("b")), &"a"); @@ -69,7 +89,7 @@ impl FrozenIndexMap { /// /// # Example /// ``` - /// use elsa::sync_index_map::FrozenIndexMap; + /// use elsa::sync::index_map::FrozenIndexMap; /// let map = FrozenIndexMap::new(); /// assert_eq!(map.insert_full(12, Box::new("a")), (0, &"a")); /// assert_eq!(map.insert_full(12, Box::new("b")), (0, &"a")); @@ -94,7 +114,7 @@ impl FrozenIndexMap { /// # Examples /// /// ``` - /// use elsa::sync_index_map::FrozenIndexMap; + /// use elsa::sync::index_map::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// map.insert(1, Box::new("a")); @@ -120,7 +140,7 @@ impl FrozenIndexMap { /// # Examples /// /// ``` - /// use elsa::sync_index_map::FrozenIndexMap; + /// use elsa::sync::index_map::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// let (idx, _ref) = map.insert_full(Box::new("foo"), Box::new("a")); @@ -150,7 +170,7 @@ impl FrozenIndexMap { /// # Examples /// /// ``` - /// use elsa::sync_index_map::FrozenIndexMap; + /// use elsa::sync::index_map::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// map.insert(1, Box::new("a")); @@ -177,7 +197,7 @@ impl FrozenIndexMap { /// # Examples /// /// ``` - /// use elsa::sync_index_map::FrozenIndexMap; + /// use elsa::sync::index_map::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// map.insert(1, Box::new("a")); @@ -209,7 +229,7 @@ impl FrozenIndexMap { /// # Examples /// /// ``` - /// use elsa::sync_index_map::FrozenIndexMap; + /// use elsa::sync::index_map::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// assert_eq!(map.is_empty(), true); @@ -242,7 +262,7 @@ where /// # Examples /// /// ``` - /// use elsa::sync_index_map::FrozenIndexMap; + /// use elsa::sync::index_map::FrozenIndexMap; /// /// let map = FrozenIndexMap::new(); /// map.insert(1, Box::new("a")); diff --git a/src/sync_index_set.rs b/src/sync/index_set.rs similarity index 84% rename from src/sync_index_set.rs rename to src/sync/index_set.rs index 4811447..fb2588c 100644 --- a/src/sync_index_set.rs +++ b/src/sync/index_set.rs @@ -1,21 +1,41 @@ use std::borrow::Borrow; use std::collections::hash_map::RandomState; +use std::fmt; use std::hash::{BuildHasher, Hash}; use std::iter::FromIterator; use std::ops::Index; -use std::sync::RwLock; +use std::sync::{RwLock, TryLockError}; use indexmap::IndexSet; use stable_deref_trait::StableDeref; /// Append-only threadsafeversion of `indexmap::IndexSet` where /// insertion does not require mutable access -#[derive(Debug)] pub struct FrozenIndexSet { set: RwLock>, } -// safety: UnsafeCell implies !Sync +impl fmt::Debug for FrozenIndexSet { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self.set.try_read() { + Ok(guard) => guard.fmt(f), + Err(TryLockError::Poisoned(err)) => { + f.debug_tuple("FrozenIndexSet").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("") + } + } + f.debug_tuple("FrozenIndexSet") + .field(&LockedPlaceholder) + .finish() + } + } + } +} impl FrozenIndexSet { pub fn new() -> Self { @@ -34,7 +54,7 @@ impl FrozenIndexSet { /// /// # Example /// ``` - /// use elsa::sync_index_set::FrozenIndexSet; + /// use elsa::sync::index_set::FrozenIndexSet; /// let set = FrozenIndexSet::new(); /// let a_ref = set.insert(Box::new("a")); /// let aa = "a"; @@ -63,7 +83,7 @@ impl FrozenIndexSet { /// /// # Example /// ``` - /// use elsa::sync_index_set::FrozenIndexSet; + /// use elsa::sync::index_set::FrozenIndexSet; /// let map = FrozenIndexSet::new(); /// assert_eq!(map.insert_full(Box::new("a")), (0, &"a")); /// assert_eq!(map.insert_full(Box::new("b")), (1, &"b")); @@ -86,7 +106,7 @@ impl FrozenIndexSet { /// /// # Example /// ``` - /// use elsa::sync_index_set::FrozenIndexSet; + /// use elsa::sync::index_set::FrozenIndexSet; /// let map = FrozenIndexSet::new(); /// assert_eq!(map.insert_probe(Box::new("a")), (0, true)); /// assert_eq!(map.insert_probe(Box::new("b")), (1, true)); @@ -102,7 +122,7 @@ impl FrozenIndexSet { /// # Examples /// /// ``` - /// use elsa::sync_index_set::FrozenIndexSet; + /// use elsa::sync::index_set::FrozenIndexSet; /// /// let set = FrozenIndexSet::new(); /// set.insert(Box::new("a")); @@ -125,7 +145,7 @@ impl FrozenIndexSet { /// # Examples /// /// ``` - /// use elsa::sync_index_set::FrozenIndexSet; + /// use elsa::sync::index_set::FrozenIndexSet; /// /// let set = FrozenIndexSet::new(); /// set.insert(Box::new("a")); @@ -153,7 +173,7 @@ impl FrozenIndexSet { /// # Examples /// /// ``` - /// use elsa::sync_index_set::FrozenIndexSet; + /// use elsa::sync::index_set::FrozenIndexSet; /// /// let set = FrozenIndexSet::new(); /// set.insert(Box::new("a")); @@ -184,8 +204,6 @@ impl FrozenIndexSet { pub fn as_mut(&mut self) -> &mut IndexSet { self.set.get_mut().unwrap() } - - // TODO add more } impl From> for FrozenIndexSet { diff --git a/src/sync.rs b/src/sync/mod.rs similarity index 99% rename from src/sync.rs rename to src/sync/mod.rs index bb9e608..f0ef0bf 100644 --- a/src/sync.rs +++ b/src/sync/mod.rs @@ -25,6 +25,11 @@ use std::sync::atomic::Ordering; use std::sync::RwLock; use std::sync::TryLockError; +#[cfg(feature = "indexmap")] +pub mod index_map; +#[cfg(feature = "indexmap")] +pub mod index_set; + /// Append-only threadsafe version of `std::collections::HashMap` where /// insertion does not require mutable access pub struct FrozenMap {