rust_script_ext::prelude

Trait WriteAs

Source
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 Writer.

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());

Required Methods§

Source

fn write_as(&self, fmt: F, wtr: &mut dyn Write) -> Result<()>

Serialise this with the format F into the wtr.

Implementors§

Source§

impl<F, T, A> WriteAs<F, T> for A
where F: Format, T: Serialize, A: Borrow<F::Input<T>>,