Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why DateTime<Tz> can not satisfy serde::Serialize?

Tags:

rust

chrono

serde

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.

like image 291
davyzhang Avatar asked Jun 15 '19 07:06

davyzhang


1 Answers

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(){

}
like image 92
davyzhang Avatar answered Nov 11 '22 09:11

davyzhang