4
extern crate serde;
extern crate serde_json;

#[macro_use]
extern crate serde_derive;


use chrono::{self, Date,DateTime, TimeZone};
use serde_derive::{Serialize,Deserialize}; // 1.0.91


#[derive(Serialize,Deserialize )]
struct Test<Tz>
where Tz:TimeZone,
{
    t:DateTime<Tz>
}

fn main(){

}

The code above is not gonna compile with the error:

error[E0277]: the trait bound chrono::datetime::DateTime<Tz>: serde::Serialize is not satisfied --> src/main.rs:16:5

I have

chrono = {version="0.4",features = ["serde"]}

in my Cargo.toml

I found that the implementation is here: https://docs.rs/chrono/0.4.6/chrono/struct.DateTime.html#impl-Serialize

full code sample here https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=57b41f6dd1c4c0a2c7f4f541234137a7

but I am not sure if the playground have serde feature enabled or not.

E_net4
  • 27,810
  • 13
  • 101
  • 139
davyzhang
  • 2,419
  • 3
  • 26
  • 34
  • 2
    Are you sure the error message says `serde::Serialize` and not `serde::Deserialize`? If I take out the `Deserialize` in the derive, your example works. Also, there's no `Deserialize` in the documentation you've linked for that type. `rustc --version: 1.35.0`, `[dependencies] chrono = {version="0.4",features = ["serde"]} serde = "1.0.92" serde_json = "1.0.39" serde_derive = "1.0.92"` – nelsonjchen Jun 15 '19 at 10:52
  • @crazysim thanks for your reply. My bad, I didn't see that deserialize will be the blocker here. Thank you! – davyzhang Jun 15 '19 at 11:13
  • I don't understand how you got that error message. , did you guess that was the error message? I'm sure simply running the Rust compiler would have been faster than making an SO question. – nelsonjchen Jun 15 '19 at 15:56

1 Answers1

2

Sorry guys the problems is as @crazysim said in the comment.

DateTime didn't implement Deserialize trait.

If I remove it, the code will work:

extern crate serde;
extern crate serde_json;

#[macro_use]
extern crate serde_derive;


use chrono::{self,DateTime, TimeZone};



#[derive(Serialize )]
struct Test<Tz>
where Tz:TimeZone,
{
    t:DateTime<Tz>
}

fn main(){

}
davyzhang
  • 2,419
  • 3
  • 26
  • 34