rust_script_ext::prelude

Trait ReadAs

Source
pub trait ReadAs {
    // Required method
    fn read_as<F, T>(&mut self) -> Result<F::Output<T>>
       where F: Format,
             for<'de> T: Deserialize<'de>;
}
Expand description

A trait which gives any Reader the read_as function which can be used to read with a specific format.

Noteable examples of using read_as would be to read a file directly as CSV/json/toml.

§Example

#[derive(Deserialize, Debug, PartialEq)]
struct City {
    city: String,
    pop: u32,
}

let csv = "city,pop\nBrisbane,100000\nSydney,200000\n";

// read_as on anything that is Read
let x = csv.as_bytes().read_as::<CSV, City>().unwrap();

assert_eq!(
    x,
    vec![
        City {
            city: "Brisbane".to_string(),
            pop: 100_000,
        },
        City {
            city: "Sydney".to_string(),
            pop: 200_000,
        }
    ]
);

Required Methods§

Source

fn read_as<F, T>(&mut self) -> Result<F::Output<T>>
where F: Format, for<'de> T: Deserialize<'de>,

Read the data as if it is structured with format F, deserialising into F::Output<T>.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<R: Read> ReadAs for R