1

I'm trying to use the TOML crate to read a configuration file into a Rust struct. I was getting a consistent Serde error that seemed unrelated to my code, so I decided to try the decode examples from the TOML documentation and to my surprise, it failed to build with the exact same error.

I have filed an issue with the crate maintainer, but I have a nagging feeling I might be missing something.

The code example in question is the following:

//! An example showing off the usage of `Deserialize` to automatically decode
//! TOML into a Rust `struct`

#![deny(warnings)]

extern crate toml;
extern crate serde;
#[macro_use]
extern crate serde_derive;

/// This is what we're going to decode into. Each field is optional, meaning
/// that it doesn't have to be present in TOML.
#[derive(Debug, Deserialize)]
struct Config {
    global_string: Option<String>,
    global_integer: Option<u64>,
    server: Option<ServerConfig>,
    peers: Option<Vec<PeerConfig>>,
}

/// Sub-structs are decoded from tables, so this will decode from the `[server]`
/// table.
///
/// Again, each field is optional, meaning they don't have to be present.
#[derive(Debug, Deserialize)]
struct ServerConfig {
    ip: Option<String>,
    port: Option<u64>,
}

#[derive(Debug, Deserialize)]
struct PeerConfig {
    ip: Option<String>,
    port: Option<u64>,
}

fn main() {
    let toml_str = r#"
        global_string = "test"
        global_integer = 5
        [server]
        ip = "127.0.0.1"
        port = 80
        [[peers]]
        ip = "127.0.0.1"
        port = 8080
        [[peers]]
        ip = "127.0.0.1"
    "#;

    let decoded: Config = toml::from_str(toml_str).unwrap();
    println!("{:#?}", decoded);
}

The error I get when building is the following:

error[E0277]: the trait bound `Config: serde::de::Deserialize<'_>` is not satisfied
  --> src/main.rs:51:27
   |
51 |     let decoded: Config = toml::from_str(toml_str).unwrap();
   |                           ^^^^^^^^^^^^^^ the trait `serde::de::Deserialize<'_>` is not implemented for `Config`
   |
   = note: required by `toml::from_str`

I have tried to build it with the following toolchains:

  • rustc 1.20.0-nightly (2652ce677 2017-07-17)

  • rustc 1.18.0 (03fc9d622 2017-06-06)

My Cargo.toml includes the following:

[dependencies]
serde = "*"
serde_derive = "*"
toml = "*"

Am I missing something, or is the base example of decoding with this crate simply broken?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Dash83
  • 1,357
  • 2
  • 17
  • 31
  • You need to include your `Cargo.toml`'s dependency section. This is probably a version mismatch. – DK. Jul 18 '17 at 14:35
  • Thank you, @DK. just added it. – Dash83 Jul 18 '17 at 14:37
  • Just tried it with rustc 1.20.0 nightly of 2016-06-22, and it works just fine. – DK. Jul 18 '17 at 14:45
  • Thanks for trying it, @DK. Any suggestions on why it could be failing on my machine? I'm running Ubuntu 16.04. – Dash83 Jul 18 '17 at 14:47
  • And it also appears to work fine with 1.18.0 stable. – DK. Jul 18 '17 at 14:48
  • I don't know. Have you tried in a fresh directory with no existing build artefacts or temporary files? Maybe there's a messed-up `Cargo.lock` file. – DK. Jul 18 '17 at 14:50
  • This even runs in a [playground](http://play.integer32.com/?gist=95b00206bbeb7c64f5496680d0bf41c9&version=stable), so I can only imagine a corrupted target folder or a bad Cargo.lock. – E_net4 Jul 18 '17 at 14:50
  • Alright, that's confirmation enough for me that this is localized to my environment. Thanks for the help, guys. I'll close the issue with the crate maintainer and dig up more. – Dash83 Jul 18 '17 at 14:53
  • I agree with @DK. — you almost certainly have two versions of Serde and your dependency is using a different version from what you have. I've marked as a duplicate of a question that shows how to figure out exactly why. – Shepmaster Jul 18 '17 at 15:05

0 Answers0