dtolnay / serde-yaml

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

Implementation of `serde::de::Deserialize` is not general enough #168

Closed zhzy0077 closed 4 years ago

zhzy0077 commented 4 years ago

When running following code snippet:

use std::collections::HashMap;

fn main() {
    let input = r###"
        ---
        key: Key
        value: Value
    "###;
    let x: HashMap<&str, &str> = serde_yaml::from_str(input).unwrap();

    // let input = r###"
    // {
    //     "key": "Key",
    //     "value": "Value"
    // }
    // "###;
    // let x: HashMap<&str, &str> = serde_json::from_str(input).unwrap();

    println!("{:?}", x);
}

it will result in compile-time error:

Compiling playground v0.0.1 (/playground)
error: implementation of `serde::de::Deserialize` is not general enough
   --> src/main.rs:9:34
    |
9   |       let x: HashMap<&str, &str> = serde_yaml::from_str(input).unwrap();
    |                                    ^^^^^^^^^^^^^^^^^^^^ implementation of `serde::de::Deserialize` is not general enough
    | 
   ::: /playground/.cargo/registry/src/github.com-1ecc6299db9ec823/serde-1.0.110/src/de/mod.rs:531:1
    |
531 | / pub trait Deserialize<'de>: Sized {
532 | |     /// Deserialize this value from the given Serde deserializer.
533 | |     ///
534 | |     /// See the [Implementing `Deserialize`][impl-deserialize] section of the
...   |
569 | |     }
570 | | }
    | |_- trait `serde::de::Deserialize` defined here
    |
    = note: `std::collections::HashMap<&str, &str>` must implement `serde::de::Deserialize<'0>`, for any lifetime `'0`...
    = note: ...but `std::collections::HashMap<&str, &str>` actually implements `serde::de::Deserialize<'1>`, for some specific lifetime `'1`

error: aborting due to previous error

error: could not compile `playground`.

However serde_json works for it.

Is it a limitation of serde_yaml?

dtolnay commented 4 years ago

Yes, serde_yaml does not allow deserializing &str. This is tracked in https://github.com/dtolnay/serde-yaml/issues/94.