From 819122ead6899e167e4d775dce75d2d5c306a645 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Wed, 9 Aug 2023 20:52:30 -0700 Subject: [PATCH] feat: implement serde for FrozenMap --- src/sync.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/sync.rs b/src/sync.rs index 7cd7c58..fc1d939 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -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 { map: RwLock>, } @@ -383,6 +384,20 @@ impl std::convert::AsMut> for FrozenMap { } } +#[cfg(feature = "serde")] +impl<'de, K, V> Deserialize<'de> for FrozenMap +where + K: Deserialize<'de> + Eq + Hash, + V: Deserialize<'de>, +{ + fn deserialize>(deserializer: D) -> Result { + let map = HashMap::::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))]