支持序列化和反序列化的 Serde 特性对象
此 crate 提供了一种宏,用于无痛地序列化 &dyn Trait 特性对象,以及序列化和反序列化 Box<dyn Trait> 特性对象。
fn process_event_from_clickfarm(json: &str) -> Result<()> {
let event: Box<dyn WebEvent> = serde_json::from_str(json)?;
overanalyze(event)?;
Ok(())
}介绍中声称这不会疼痛,但我会让你自己去判断。 法官。 首先在特征上贴上一个属性。
#[typetag::serde(tag = "type")]
trait WebEvent {
fn inspect(&self);
}然后在所有这些 impl 块上添加类似的属性。
#[typetag::serde]
impl WebEvent for PageLoad {
fn inspect(&self) {
println!("200 毫秒或更短");
}
}
#[typetag::serde]
impl WebEvent for Click {
fn inspect(&self) {
println!("广告之间的负空间: x={} y=
[](https://github.com/dtolnay/typetag)
[](https://crates.io/crates/typetag)
[](https://docs.rs/typetag)
[](https://github.com/dtolnay/typetag/actions?query=branch%3Amaster)
[dependencies]暂无开放 Issues,或尚未同步最近议题。