From e6980211c4c53fc1eeb686cd12363a8fa603daa3 Mon Sep 17 00:00:00 2001 From: Max Audron Date: Wed, 20 Dec 2023 13:13:00 +0100 Subject: rework Network derives to be more consistent i don't even know anymore, blame past me --- src/primitive/message.rs | 10 ++++------ src/primitive/variant.rs | 32 +++----------------------------- src/primitive/variantlist.rs | 30 ++++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 35 deletions(-) (limited to 'src/primitive') diff --git a/src/primitive/message.rs b/src/primitive/message.rs index ff992e3..12edb29 100644 --- a/src/primitive/message.rs +++ b/src/primitive/message.rs @@ -6,7 +6,7 @@ use crate::{deserialize::*, serialize::*}; use crate::primitive::BufferInfo; -use super::Variant; +use super::{Variant, VariantList}; /// The Message struct represents a Message as received in IRC /// @@ -222,13 +222,11 @@ bitflags! { } } -impl crate::message::Network for HashMap +impl crate::message::NetworkList for HashMap where T: std::convert::TryFrom + Into + Clone + std::hash::Hash + std::cmp::Eq, { - type Item = super::VariantList; - - fn to_network(&self) -> Self::Item { + fn to_network_list(&self) -> VariantList { let mut res = Vec::with_capacity(self.len() * 2); self.iter().for_each(|(k, v)| { @@ -239,7 +237,7 @@ where return res; } - fn from_network(input: &mut Self::Item) -> Self { + fn from_network_list(input: &mut VariantList) -> Self { use itertools::Itertools; let mut res = HashMap::with_capacity(input.len() / 2); diff --git a/src/primitive/variant.rs b/src/primitive/variant.rs index 673d39f..b306ea7 100644 --- a/src/primitive/variant.rs +++ b/src/primitive/variant.rs @@ -86,38 +86,12 @@ impl From<&str> for Variant { /// Implements the Network trait genericly for everything that /// can be a VariantList / Vec -impl crate::message::Network for Vec -where - T: std::convert::TryFrom + Into + Clone, -{ - type Item = super::VariantList; - - fn to_network(&self) -> Self::Item { - self.iter().map(|i| (*i).clone().into()).collect() - } - - fn from_network(input: &mut Self::Item) -> Self { - input - .iter() - .map(|i| match T::try_from(i.clone()) { - Ok(it) => it, - // TODO handle error - _ => unreachable!(), - }) - .collect() - } -} - -/// Implements the Network trait genericly for everything that -/// can be a VariantList / Vec -impl crate::message::Network for HashMap +impl crate::message::NetworkList for HashMap where T: std::convert::TryFrom + Into + Clone + std::hash::Hash + std::cmp::Eq, S: std::convert::TryFrom + Into + Clone + std::hash::Hash + std::cmp::Eq, { - type Item = super::VariantList; - - fn to_network(&self) -> Self::Item { + fn to_network_list(&self) -> VariantList { let mut res = Vec::with_capacity(self.len() * 2); self.iter().for_each(|(k, v)| { @@ -128,7 +102,7 @@ where return res; } - fn from_network(input: &mut Self::Item) -> Self { + fn from_network_list(input: &mut VariantList) -> Self { let mut res = HashMap::with_capacity(input.len() / 2); input.iter().tuples().for_each(|(k, v)| { diff --git a/src/primitive/variantlist.rs b/src/primitive/variantlist.rs index b3ca720..d3518e6 100644 --- a/src/primitive/variantlist.rs +++ b/src/primitive/variantlist.rs @@ -45,3 +45,33 @@ impl Deserialize for VariantList { return Ok((pos, res)); } } + +impl crate::message::NetworkMap for Vec +where + S: std::convert::TryFrom + Into + Clone + std::hash::Hash + std::cmp::Eq, + >::Error: std::fmt::Debug, +{ + type Item = VariantList; + + fn to_network_map(&self) -> VariantList { + self.iter().map(|i| i.clone().into()).collect() + } + + fn from_network_map(input: &mut VariantList) -> Self { + input.iter().map(|i| i.clone().try_into().unwrap()).collect() + } +} + +impl crate::message::NetworkList for Vec +where + S: std::convert::TryFrom + Into + Clone + std::hash::Hash + std::cmp::Eq, + >::Error: std::fmt::Debug, +{ + fn to_network_list(&self) -> VariantList { + self.iter().map(|i| i.clone().into()).collect() + } + + fn from_network_list(input: &mut VariantList) -> Self { + input.iter().map(|i| i.clone().try_into().unwrap()).collect() + } +} -- cgit v1.2.3