dtolnay / serde-yaml

Strongly typed YAML library for Rust
Apache License 2.0
965 stars 165 forks source link

unit struct deserializes from null only #216

Closed rukai closed 3 years ago

rukai commented 3 years ago

The following example demonstrates the issue:

use serde::Deserialize;

#[derive(Deserialize)]
struct Foo { }

#[derive(Deserialize)]
struct Bar;

fn main() {
    let _: Foo = serde_yaml::from_str("Foo: []").unwrap(); // this works
    let _: Bar = serde_yaml::from_str("null").unwrap(); // this does work but I think it shouldnt!
    let _: Bar = serde_yaml::from_str("Bar").unwrap(); // this doesnt work but I think it should work!
    let _: Bar = serde_yaml::from_str("Bar: []").unwrap(); // this doesnt work either
}

I didnt check Serialize compatibility because I am only using deserialize

rukai commented 3 years ago

Oh this looks pretty doable, ill give it a go.

rukai commented 3 years ago

A struct with no fields successfully deserializes from "Foo: []" not because its matching Foo as the struct name but because Foo is interpreted as a field which is ignored. This seems to be the root of my confusion, sorry for the erroneous issue.

I still have another problem related to typetag's handling of unit structs but ill think about it more and probably raise an issue on the typetag repo.