Skip to content

Add Deserializer::deserialize_iter for convenience #3012

@cactusdualcore

Description

@cactusdualcore

It's like deserialize_seq, but lazily evaluated!

I think deserialize_seq is annoying. Every single collection needs its own visitor, but most collections already implement a (probably even optimized!) FromIter implementation or similar.

Whenever visit_seq is used, there already is a Iterator of some sort. Any type implementing SeqAccess behaves effectively like one. As far as I can tell, the only difference is that SeqAccess allows for passing in seeds during iteration.

In essence, this hypothetical deserialize_iter method could share most machinery with the sequence deserialization, except it gives control back up the call stack.

This would significantly reduce the pain of writing custom deserialization for types with sequence fields.

#[derive(Deserialize)]
struct Bar;

struct Foo(Vec<Bar>);

impl<'de> Deserialize<'de> for Foo {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>
    {
        let bars = deserializer
          .deserialize_iter::<Bar>()
          .collect::<Result<_, _>>()?;
        Ok(Foo(bars))
    }
}

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