【问题标题】:How to implement rustc_serialize::Decodable for struct that has a field value that is another struct?如何为具有另一个结构的字段值的结构实现 rustc_serialize::Decodable?
【发布时间】:2017-03-02 23:47:32
【问题描述】:

我需要为我的 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,这就是您在上面的代码中看到的内容。 NaiveDate from the rust-chrono crate 是一个结构,其中一个字段的数据类型为 i32。

当我现在运行代码时,我收到消息:

struct chrono::NaiveDate 的字段 ymdf 是私有的

如何为NaiveDate 实现Decoder?有没有更简单的方法来为我的Herd 结构实现Decodable?我是一个全能的初学者程序员;也许还有另一种方式来看待这个问题。

我正在使用具有以下依赖项的 Rust 1.12:

  • nickel = "0.9.0"
  • postgres = { version = "0.12", features = ["with-chrono"]}
  • chrono = "0.2"
  • rustc-serialize = "0.3"

【问题讨论】:

    标签: struct rust


    【解决方案1】:

    NaiveDate 确实实现了Decodable,但在可选功能"rustc-serialize" 下。

    您应该在Cargo.toml 中添加它以激活它:

    chrono = { version = "0.2", features = ["rustc-serialize"]}
    

    【讨论】:

      【解决方案2】:

      为了能够derive Decodable 为Herd,NaiveDate 也需要为Decodable。不过,它的 ymdf 字段是私有的,如果您想手动实现它,这是一个问题。

      您可以做的是为ymdf 定义您自己的包装器类型(并在Herd 中使用它) - 然后您应该能够为derive Decodable 为它,然后为Herd。

      编辑:不一定要使用NaiveDate,因为没有简单的方法可以将ymdf 转换为它(或导入DateImpl,它的类型),但是根据您的需要,以下方式可能就足够了:

      use std::i32;
      
      pub type DateImpl = i32;
      pub const MAX_YEAR: DateImpl = i32::MAX >> 13;
      pub const MIN_YEAR: DateImpl = i32::MIN >> 13;
      
      #[derive(RustcDecodable)]
      struct YmdfWrapper {
          ymdf: DateImpl
      }
      
      #[derive(RustcDecodable)]
      struct Herd {
          id: i32,
          breed: String,
          name: String,
          purchase_date: YmdfWrapper,
      }
      

      【讨论】:

      • 要实现Decodable,Herd不需要它的所有字段都是Decodable。仅当您想要派生特征时才需要这样做。在这种情况下,手动实现 trait 会更容易。
      • 我认为这个答案会更好,如果它显示了如何做到这一点的 sn-p。这也是回答OP提出的一般问题的唯一答案,而不是具体实例^_^
      • @Shepmaster 我愿意,但是this issue 让我很难用extern crates 测试代码,而且我不想让一些错误溜走。当我有时间和适当的网络时,我会添加它。
      • @mcarton 我差点忘了;我按照你的建议修复了答案。
      • Let me help you not do work at work。并非所有的游乐场都是平等的^_^。
      猜你喜欢
      • 2021-01-08
      • 1970-01-01
      • 2021-11-01
      • 2014-07-06
      • 1970-01-01
      • 2022-08-17
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      相关资源
      最近更新 更多