diff options
| author | Max Audron <audron@cocaine.farm> | 2021-10-01 18:13:43 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-10-01 18:17:03 +0200 |
| commit | 8465010f3cb51d672b60df39a7dfd34624ab5c7c (patch) | |
| tree | 062d8640f7e7c4aea2a2e4029ea742982ba20911 /src/primitive | |
| parent | adapt Sync* types (diff) | |
migrate to separated NetworkMap and NetworkList macros
Diffstat (limited to '')
| -rw-r--r-- | src/primitive/variant.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/primitive/variant.rs b/src/primitive/variant.rs index d49f0dc..92e3211 100644 --- a/src/primitive/variant.rs +++ b/src/primitive/variant.rs @@ -141,6 +141,44 @@ where } } +impl<T, S> crate::message::NetworkMap for HashMap<T, S> +where + T: std::convert::TryFrom<Variant> + Into<Variant> + Clone + std::hash::Hash + std::cmp::Eq, + S: std::convert::TryFrom<Variant> + Into<Variant> + Clone + std::hash::Hash + std::cmp::Eq, +{ + type Item = super::VariantList; + + fn to_network_map(&self) -> Self::Item { + let mut res = Vec::with_capacity(self.len() * 2); + + self.iter().for_each(|(k, v)| { + res.push((*k).clone().into()); + res.push((*v).clone().into()); + }); + + return res; + } + + fn from_network_map(input: &mut Self::Item) -> Self { + let mut res = HashMap::with_capacity(input.len() / 2); + + input.iter().tuples().for_each(|(k, v)| { + res.insert( + match T::try_from(k.clone()) { + Ok(it) => it, + _ => unreachable!(), + }, + match S::try_from(v.clone()) { + Ok(it) => it, + _ => unreachable!(), + }, + ); + }); + + return res; + } +} + impl Serialize for Variant { fn serialize(&self) -> Result<Vec<u8>, Error> { let unknown: u8 = 0x00; |
