我需要为我的Herd结构实现rustc_serialize::Decoder特征:
extern crate chrono; extern crate rustc_serialize; use chrono::NaiveDate; use rustc_serialize::Decodable; struct Herd { id: i32, breed: String, name: String, purchase_date: NaiveDate, } impl Decodable for Herd { fn decode<D: Decoder>(d: &mut D) -> Result<Herd, D::Error> { d.read_struct("Herd", 4, |d| { let id = try!(d.read_struct_field("id", 0, |d| d.read_i32())); let breed = try!(d.read_struct_field("breed", 1, |d| d.read_str())); let name = try!(d.read_struct_field("name", 2, |d| d.read_str())); let purchase_date = try!(d.read_struct_field("purchase_date", 3, |i| { i.read_struct("NaiveDate", 1, |i| { let ymdf = try!(i.read_struct_field("ymdf", 0, |i| i.read_i32())); Ok(NaiveDate { ymdf: ymdf }) }) })); Ok(Herd { id: id, breed: breed, name: name, purchase_date: purchase_date, }) }) } }我无法使用#[derive(RustcDecodable)]因为发生以下错误:
error[E0277]: the trait bound chrono::NaiveDate: rustc_serialize::Decodable is not satisfied我正在手动实现Decodable ,这就是你在上面的代码中看到的。 来自rust- NaiveDate crate的NaiveDate是一个具有数据类型为i32字段的结构。
当我现在运行代码时,我收到消息:
struct chrono :: NaiveDate的字段ymdf是私有的
我如何实现NaiveDate Decoder ? 有一种更简单的方法来实现Decodable我的Herd结构? 我是一个全能的初学者程序员; 也许有另一种方法来看待这个问题。
我正在使用Rust 1.12以下依赖项:
nickel = "0.9.0" postgres = { version = "0.12", features = ["with-chrono"]} chrono = "0.2" rustc-serialize = "0.3"I need to implement the rustc_serialize::Decoder trait for my Herd struct:
extern crate chrono; extern crate rustc_serialize; use chrono::NaiveDate; use rustc_serialize::Decodable; struct Herd { id: i32, breed: String, name: String, purchase_date: NaiveDate, } impl Decodable for Herd { fn decode<D: Decoder>(d: &mut D) -> Result<Herd, D::Error> { d.read_struct("Herd", 4, |d| { let id = try!(d.read_struct_field("id", 0, |d| d.read_i32())); let breed = try!(d.read_struct_field("breed", 1, |d| d.read_str())); let name = try!(d.read_struct_field("name", 2, |d| d.read_str())); let purchase_date = try!(d.read_struct_field("purchase_date", 3, |i| { i.read_struct("NaiveDate", 1, |i| { let ymdf = try!(i.read_struct_field("ymdf", 0, |i| i.read_i32())); Ok(NaiveDate { ymdf: ymdf }) }) })); Ok(Herd { id: id, breed: breed, name: name, purchase_date: purchase_date, }) }) } }I'm unable to use #[derive(RustcDecodable)] because the following error occurs:
error[E0277]: the trait bound chrono::NaiveDate: rustc_serialize::Decodable is not satisfiedI'm working on manually implementing Decodable and that's what you see in the code above. NaiveDate from the rust-chrono crate is a struct with one field that is of data type i32.
When I run the code right now, I receive the message:
field ymdf of struct chrono::NaiveDate is private
How do I implement Decoder for NaiveDate? Is there a simpler way to implement Decodable for my Herd struct? I am an all-around beginner programmer; perhaps there is another way to look at this problem.
I am using Rust 1.12 with the following dependencies:
nickel = "0.9.0" postgres = { version = "0.12", features = ["with-chrono"]} chrono = "0.2" rustc-serialize = "0.3"最满意答案
NaiveDate确实实现了可Decodable但在可选功能"rustc-serialize" 。
您应该在您的Cargo.toml添加以激活它:
chrono = { version = "0.2", features = ["rustc-serialize"]}NaiveDate does implement Decodable but under an optional feature "rustc-serialize".
You should add this in your Cargo.toml to activate it:
chrono = { version = "0.2", features = ["rustc-serialize"]}
更多推荐
发布评论