toml
toml copied to clipboard
Fail to work with typetag during serializing
toml
crate fail to work with typetag
with vector of trait object during serializing, but serde_json works:
use serde::{Serialize, Deserialize};
#[typetag::serde(tag = "type")]
trait WebEvent {
fn inspect(&self);
}
#[derive(Serialize, Deserialize)]
struct PageLoad(usize);
#[derive(Serialize, Deserialize, Clone)]
struct Click {
x: i32,
y: i32,
}
#[typetag::serde]
impl WebEvent for PageLoad {
fn inspect(&self) {
println!("200 milliseconds or bust");
}
}
#[typetag::serde]
impl WebEvent for Click {
fn inspect(&self) {
println!("negative space between the ads: x={} y={}", self.x, self.y);
}
}
let v = Click{ x: 1, y: 1 };
let dynv = Box::new(v.clone()) as Box::<dyn WebEvent>;
println!("{}", toml::to_string(&v).unwrap());
println!("{}", toml::to_string(&dynv).unwrap());
println!("{}", serde_json::to_string(&vec![dynv]).unwrap()); // This works
println!("{}", toml::to_string(&vec![dynv]).unwrap()); // This does not work
JSON output:
[{"type":"Click","x":1,"y":1}]
toml Error message:
thread '' panicked at 'called `Result::unwrap()` on an `Err` value: Error { inner: UnsupportedType(None) }'
versioning:
toml = "0.8.2" typetag = "0.2" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0"