Skip to content

Duplicate visitors with yaserde_derive #198

Open

Description

@NiklasVousten

If you have a simple struct with two fields of the same type and use yaserde_derive, two identical visitors will be generated for these.

Example:

#[derive(YaDeserialize)]
pub struct XmlStruct {
  #[yaserde]
  num1: u32,

  #[yaserde]
  num2: u32,
}

Using cargo expand shows that two visitors are generated

#[allow(non_snake_case, non_camel_case_types)]
struct __Visitor_Num1_;
impl<'de> ::yaserde::Visitor<'de> for __Visitor_Num1_ {
    type Value = u32;
    fn visit_u32(
        self,
        v: &str,
    ) -> ::std::result::Result<Self::Value, ::std::string::String> {
        u32::from_str(v).map_err(|e| e.to_string())
    }
}
#[allow(non_snake_case, non_camel_case_types)]
struct __Visitor_Num2_;
impl<'de> ::yaserde::Visitor<'de> for __Visitor_Num2_ {
    type Value = u32;
    fn visit_u32(
        self,
        v: &str,
    ) -> ::std::result::Result<Self::Value, ::std::string::String> {
        u32::from_str(v).map_err(|e| e.to_string())
    }
}

I believe the derive macro should only create implementations for the YaDeserialize trait of unknown types. Instead yaserde should have implementations for default visitors similar to serde.

Furthermore I think it would be good, if the primitives have an implementation for the YaDeserialize trait

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions