Skip to content

Question: Why is the Visitor trait so important? #3013

@cactusdualcore

Description

@cactusdualcore

Custom deserialization code seems to turn very complex quickly, mostly because of all the visitors. I wonder, what was the original reason this API was chosen? Is there a reason the crate decided not to expose "simpler" APIs, like in the example below?

enum Value {
    Name(String),
    Age(u32),
}

struct Student(HashMap<bool, Value>);

impl<'de> Deserialize<'de> for Student {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>
    {
        #[derive(DeserializeOrEnumerate)]
        enum Field { Name, Age };
     
        let name = None;
        let age = None;

        while let Some(field) = deserializer.deserialize_field<Field>()? {
            match field {
                // filling those `Option<T>` vars...
            }
        }

        Self { /* ... */ }
    }
}

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