From c0ca4a77c852bdebe74134f99b2d4630fb6ac9bc Mon Sep 17 00:00:00 2001 From: George Pollard Date: Wed, 18 Jan 2023 14:57:02 +1300 Subject: [PATCH] Add Default implementations --- src/sync.rs | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/src/sync.rs b/src/sync.rs index 5dd249d..d16a717 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -22,16 +22,42 @@ pub struct FrozenMap { map: RwLock>, } +impl Default for FrozenMap { + fn default() -> Self { + Self { + map: Default::default(), + } + } +} + impl FrozenMap { // these should never return &K or &V // these should never delete any entries pub fn new() -> Self { - Self { - map: RwLock::new(Default::default()), - } + Self::default() } + /// If the key exists in the map, returns a reference + /// to the corresponding value, otherwise inserts a + /// new entry in the map for that key and returns a + /// reference to the given value. + /// + /// Existing values are never overwritten. + /// + /// The key may be any borrowed form of the map's key type, but + /// [`Hash`] and [`Eq`] on the borrowed form *must* match those for + /// the key type. + /// + /// # Examples + /// + /// ``` + /// use elsa::sync::FrozenMap; + /// + /// let map = FrozenMap::new(); + /// assert_eq!(map.insert(1, Box::new("a")), &"a"); + /// assert_eq!(map.insert(1, Box::new("b")), &"a"); + /// ``` pub fn insert(&self, k: K, v: V) -> &V::Target { let mut map = self.map.write().unwrap(); let ret = unsafe { @@ -118,12 +144,18 @@ pub struct FrozenVec { vec: RwLock>, } -impl FrozenVec { - pub fn new() -> Self { +impl Default for FrozenVec { + fn default() -> Self { Self { - vec: RwLock::new(Default::default()), + vec: Default::default(), } } +} + +impl FrozenVec { + pub fn new() -> Self { + Default::default() + } // these should never return &T // these should never delete any entries @@ -279,10 +311,10 @@ impl From> for FrozenBTreeMap Index<&Q> for FrozenBTreeMap - where - Q: Ord, - K: Clone + Ord + Borrow, - V: StableDeref +where + Q: Ord, + K: Clone + Ord + Borrow, + V: StableDeref, { type Output = V::Target;