我正在尝试为我的Rust结构体添加序列化功能。这是一个日历事件,代码如下:
#[derive(PartialEq, Clone, Encodable, Decodable)]
pub struct Event {
pub id: Uuid,
pub name: String,
pub desc: String,
pub location: String,
pub start: DateTime<Local>,
pub end: DateTime<Local>,
}
该结构使用了第三方的两种不同类型,
Uuid
来自https://github.com/rust-lang/uuid,DateTime
来自https://github.com/lifthrasiir/rust-chrono。
如果我尝试构建项目,编译器会抱怨找不到 Uuid
和 DateTime
的 encode
函数,这是因为它们都没有从 serialize
crate 派生出 Encodable
和 Decodable
。
那么问题来了: 有没有一种方法可以在不触及库本身代码的情况下添加派生到第三方结构体上?如果没有,那么在这种情况下添加序列化功能的最佳方法是什么?
serde
呢?serde
难道不是使用Encodable
/Decodable
吗? - Daniel Fathserialize
crate。无论如何,如果他们确实使用了 Serde,他们应该这样说,否则答案将没有用处。 :) - DK.serialize
和rustc-serialize
,就是这个顺序。不,真的。是的,我知道看起来很奇怪。 - DK.