Skip to content

Commit

Permalink
feat: implement serde for FrozenMap
Browse files Browse the repository at this point in the history
  • Loading branch information
aminya committed Aug 10, 2023
1 parent 0eb5c76 commit 819122e
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Append-only threadsafe version of `std::collections::HashMap` where
/// insertion does not require mutable access
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct FrozenMap<K, V> {
map: RwLock<HashMap<K, V>>,
}
Expand Down Expand Up @@ -383,6 +384,20 @@ impl<K, V> std::convert::AsMut<HashMap<K, V>> for FrozenMap<K, V> {
}
}

#[cfg(feature = "serde")]
impl<'de, K, V> Deserialize<'de> for FrozenMap<K, V>
where
K: Deserialize<'de> + Eq + Hash,
V: Deserialize<'de>,
{
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let map = HashMap::<K, V>::deserialize(deserializer)?;
Ok(Self {
map: RwLock::new(map),
})
}
}

/// Append-only threadsafe version of `std::vec::Vec` where
/// insertion does not require mutable access
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
Expand Down

0 comments on commit 819122e

Please sign in to comment.