pub trait WriteAs<F, T> {
// Required method
fn write_as(&self, fmt: F, wtr: &mut dyn Write) -> Result<()>;
}
Expand description
A trait which can supply write_as
on a type to serialise it into a specific format and write it into a
Write
r.
Its counterpart ReadAs
works on any reader, WriteAs
works differently, being available on
any type which matches the format’s input.
§Example
#[derive(Serialize)]
struct City {
city: String,
pop: u32,
}
let mut buf = Vec::new();
let sydney = City {
city: "Sydney".to_string(),
pop: 200_000
};
// we serialise as JSON
sydney.write_as(JSON, &mut buf).unwrap();
assert_eq!(buf, r#"{
"city": "Sydney",
"pop": 200000
}"#.as_bytes());
// but we could also easily serialise as TOML
buf.clear();
sydney.write_as(TOML, &mut buf).unwrap();
assert_eq!(buf, r#"city = "Sydney"
pop = 200000
"#.as_bytes());