Skip to content

Commit

Permalink
Add Serialize and Deserialize to SortedMap
Browse files Browse the repository at this point in the history
Summary: ^

Reviewed By: stepancheg

Differential Revision: D50662284

fbshipit-source-id: 88dcc1c41b884c686183c96530cac8ca5d208ab8
  • Loading branch information
Scott Cao authored and facebook-github-bot committed Oct 26, 2023
1 parent dbfd909 commit 9e783cb
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions starlark_map/src/sorted_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -183,6 +185,30 @@ impl<'a, K: Ord + Hash, V> IntoIterator for &'a mut SortedMap<K, V> {
}
}

impl<K: Serialize, V: Serialize> Serialize for SortedMap<K, V> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.map.serialize(serializer)
}
}

impl<'de, K, V> Deserialize<'de> for SortedMap<K, V>
where
K: Deserialize<'de> + Hash + Eq,
V: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Self {
map: OrderedMap::deserialize(deserializer)?,
})
}
}

#[cfg(test)]
mod tests {
use crate::sorted_map::SortedMap;
Expand Down

0 comments on commit 9e783cb

Please sign in to comment.