如何实现rustc

编程入门 行业动态 更新时间:2024-10-25 07:30:49
如何实现rustc_serialize :: Decodable的结构有一个字段值是另一个结构?(How to implement rustc_serialize::Decodable for struct that has a field value that is another struct?)

我需要为我的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 satisfied
 

I'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"]}

更多推荐

本文发布于:2023-08-02 19:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1380696.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何实现   rustc

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!