From 2aa8a4b1179e999f11afaf5667025042a1b580fa Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Mon, 5 Sep 2022 17:09:35 +0200 Subject: [PATCH] No need to pass visitor instance to methods --- instant-xml-macros/src/de.rs | 9 ++---- instant-xml/src/de.rs | 18 +++++------- instant-xml/src/impls.rs | 53 ++++++++++++++++++------------------ 3 files changed, 36 insertions(+), 44 deletions(-) diff --git a/instant-xml-macros/src/de.rs b/instant-xml-macros/src/de.rs index 279c5d3..831715b 100644 --- a/instant-xml-macros/src/de.rs +++ b/instant-xml-macros/src/de.rs @@ -138,8 +138,7 @@ impl Deserializer { type Value = #ident #ty_generics; fn visit_struct( - &self, - deserializer: &mut ::instant_xml::Deserializer<'xml> + deserializer: &mut ::instant_xml::Deserializer<'xml>, ) -> Result { use ::instant_xml::de::Node; @@ -194,11 +193,7 @@ impl Deserializer { } #namespaces_map; - deserializer.deserialize_struct( - StructVisitor{ - marker: std::marker::PhantomData, - lifetime: std::marker::PhantomData - }, + deserializer.deserialize_struct::( #name, #default_namespace, &namespaces_map diff --git a/instant-xml/src/de.rs b/instant-xml/src/de.rs index 527258d..c7f13bf 100644 --- a/instant-xml/src/de.rs +++ b/instant-xml/src/de.rs @@ -96,7 +96,6 @@ impl<'xml> Deserializer<'xml> { pub fn deserialize_struct( &mut self, - visitor: V, name: &str, def_default_namespace: &'xml str, def_namespaces: &HashMap<&'xml str, &'xml str>, @@ -151,7 +150,7 @@ impl<'xml> Deserializer<'xml> { .filter(|(k, v)| self.parser_namespaces.insert(k, v).is_none()) .collect::>(); - let ret = visitor.visit_struct(self)?; + let ret = V::visit_struct(self)?; // Process close tag let item = match self.parser.next() { @@ -197,7 +196,7 @@ impl<'xml> Deserializer<'xml> { ret } - pub(crate) fn deserialize_element(&mut self, visitor: V) -> Result + pub(crate) fn deserialize_element(&mut self) -> Result where V: Visitor<'xml>, { @@ -209,7 +208,7 @@ impl<'xml> Deserializer<'xml> { match self.parser.next() { Some(Ok(XmlRecord::Element(v))) => { - let ret = visitor.visit_str(v); + let ret = V::visit_str(v); self.parser.next(); ret } @@ -217,12 +216,9 @@ impl<'xml> Deserializer<'xml> { } } - pub(crate) fn deserialize_attribute(&mut self, visitor: V) -> Result - where - V: Visitor<'xml>, - { + pub(crate) fn deserialize_attribute>(&mut self) -> Result { match self.tag_attributes.pop() { - Some(attr) => visitor.visit_str(attr.value), + Some(attr) => V::visit_str(attr.value), None => Err(Error::UnexpectedEndOfStream), } } @@ -383,11 +379,11 @@ impl<'xml> Iterator for XmlParser<'xml> { pub trait Visitor<'xml>: Sized { type Value; - fn visit_str(self, _value: &'xml str) -> Result { + fn visit_str(_value: &'xml str) -> Result { unimplemented!(); } - fn visit_struct(&self, _deserializer: &mut Deserializer<'xml>) -> Result { + fn visit_struct(_deserializer: &mut Deserializer<'xml>) -> Result { unimplemented!(); } } diff --git a/instant-xml/src/impls.rs b/instant-xml/src/impls.rs index c81c386..208e852 100644 --- a/instant-xml/src/impls.rs +++ b/instant-xml/src/impls.rs @@ -18,7 +18,8 @@ where ::Err: std::fmt::Display, { type Value = T; - fn visit_str(self, value: &str) -> Result { + + fn visit_str(value: &str) -> Result { match FromStr::from_str(value) { Ok(v) => Ok(v), Err(e) => Err(Error::Other(e.to_string())), @@ -31,8 +32,8 @@ struct BoolVisitor; impl<'xml> Visitor<'xml> for BoolVisitor { type Value = bool; - fn visit_str(self, value: &str) -> Result { - FromStrToVisitor(PhantomData::).visit_str(value) + fn visit_str(value: &str) -> Result { + FromStrToVisitor::::visit_str(value) } } @@ -41,8 +42,8 @@ impl<'xml> FromXml<'xml> for bool { fn deserialize(deserializer: &mut Deserializer) -> Result { match deserializer.consume_next_type() { - EntityType::Element => deserializer.deserialize_element(BoolVisitor), - EntityType::Attribute => deserializer.deserialize_attribute(BoolVisitor), + EntityType::Element => deserializer.deserialize_element::(), + EntityType::Attribute => deserializer.deserialize_attribute::(), } } } @@ -105,8 +106,8 @@ where { type Value = T; - fn visit_str(self, value: &str) -> Result { - FromStrToVisitor(PhantomData::).visit_str(value) + fn visit_str(value: &str) -> Result { + FromStrToVisitor::::visit_str(value) } } @@ -115,12 +116,12 @@ macro_rules! from_xml_for_number { impl<'xml> FromXml<'xml> for $typ { fn deserialize(deserializer: &mut Deserializer) -> Result { match deserializer.consume_next_type() { - EntityType::Element => deserializer.deserialize_element(NumberVisitor { - marker: PhantomData, - }), - EntityType::Attribute => deserializer.deserialize_attribute(NumberVisitor { - marker: PhantomData, - }), + EntityType::Element => { + deserializer.deserialize_element::>() + } + EntityType::Attribute => { + deserializer.deserialize_attribute::>() + } } } @@ -147,7 +148,7 @@ struct StringVisitor; impl<'xml> Visitor<'xml> for StringVisitor { type Value = String; - fn visit_str(self, value: &str) -> Result { + fn visit_str(value: &str) -> Result { Ok(escape_back(value).into_owned()) } } @@ -158,8 +159,8 @@ impl<'xml> FromXml<'xml> for String { fn deserialize(deserializer: &mut Deserializer) -> Result { //<&'xml str>::deserialize(deserializer); match deserializer.consume_next_type() { - EntityType::Element => deserializer.deserialize_element(StringVisitor), - EntityType::Attribute => deserializer.deserialize_attribute(StringVisitor), + EntityType::Element => deserializer.deserialize_element::(), + EntityType::Attribute => deserializer.deserialize_attribute::(), } } } @@ -169,7 +170,7 @@ struct CharVisitor; impl<'xml> Visitor<'xml> for CharVisitor { type Value = char; - fn visit_str(self, value: &str) -> Result { + fn visit_str(value: &str) -> Result { match value.len() { 1 => Ok(value.chars().next().expect("char type")), _ => Err(Error::Other("Expected char type".to_string())), @@ -182,8 +183,8 @@ impl<'xml> FromXml<'xml> for char { fn deserialize(deserializer: &mut Deserializer) -> Result { match deserializer.consume_next_type() { - EntityType::Element => deserializer.deserialize_element(CharVisitor), - EntityType::Attribute => deserializer.deserialize_attribute(CharVisitor), + EntityType::Element => deserializer.deserialize_element::(), + EntityType::Attribute => deserializer.deserialize_attribute::(), } } } @@ -193,7 +194,7 @@ struct StrVisitor; impl<'a> Visitor<'a> for StrVisitor { type Value = &'a str; - fn visit_str(self, value: &'a str) -> Result { + fn visit_str(value: &'a str) -> Result { match escape_back(value) { Cow::Owned(v) => Err(Error::Other(format!("Unsupported char: {}", v))), Cow::Borrowed(v) => Ok(v), @@ -206,8 +207,8 @@ impl<'xml> FromXml<'xml> for &'xml str { fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result { match deserializer.consume_next_type() { - EntityType::Element => deserializer.deserialize_element(StrVisitor), - EntityType::Attribute => deserializer.deserialize_attribute(StrVisitor), + EntityType::Element => deserializer.deserialize_element::(), + EntityType::Attribute => deserializer.deserialize_attribute::(), } } } @@ -217,18 +218,18 @@ struct CowStrVisitor; impl<'a> Visitor<'a> for CowStrVisitor { type Value = Cow<'a, str>; - fn visit_str(self, value: &'a str) -> Result { + fn visit_str(value: &'a str) -> Result { Ok(escape_back(value)) } } impl<'xml> FromXml<'xml> for Cow<'xml, str> { - const KIND: Kind = <&str>::KIND; + const KIND: Kind = Kind::Scalar; fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result { match deserializer.consume_next_type() { - EntityType::Element => deserializer.deserialize_element(CowStrVisitor), - EntityType::Attribute => deserializer.deserialize_attribute(CowStrVisitor), + EntityType::Element => deserializer.deserialize_element::(), + EntityType::Attribute => deserializer.deserialize_attribute::(), } } }