From 9e783cb2aa1eeb7bd1680215ace3bbba17cd6a98 Mon Sep 17 00:00:00 2001 From: Scott Cao Date: Wed, 25 Oct 2023 23:53:31 -0700 Subject: [PATCH] Add Serialize and Deserialize to SortedMap Summary: ^ Reviewed By: stepancheg Differential Revision: D50662284 fbshipit-source-id: 88dcc1c41b884c686183c96530cac8ca5d208ab8 --- starlark_map/src/sorted_map.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/starlark_map/src/sorted_map.rs b/starlark_map/src/sorted_map.rs index d92c99a6c..4fddf1837 100644 --- a/starlark_map/src/sorted_map.rs +++ b/starlark_map/src/sorted_map.rs @@ -20,6 +20,8 @@ use std::hash::Hash; use allocative::Allocative; +use serde::Deserialize; +use serde::Serialize; use crate::ordered_map::OrderedMap; use crate::small_map; @@ -183,6 +185,30 @@ impl<'a, K: Ord + Hash, V> IntoIterator for &'a mut SortedMap { } } +impl Serialize for SortedMap { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + self.map.serialize(serializer) + } +} + +impl<'de, K, V> Deserialize<'de> for SortedMap +where + K: Deserialize<'de> + Hash + Eq, + V: Deserialize<'de>, +{ + fn deserialize(deserializer: D) -> Result + where + D: serde::Deserializer<'de>, + { + Ok(Self { + map: OrderedMap::deserialize(deserializer)?, + }) + } +} + #[cfg(test)] mod tests { use crate::sorted_map::SortedMap;