Hello friends,
I've been building a parser, reader + writer, and I get the compiler telling me I need type annotations to compile. I'd ideally like to add a bit of code so I don't need to add these annotations as the API becomes a bit more annoying. My code is as such:
pub struct Writer<W: io::Write> {
pub wtr: io::BufWriter<W>,
}
impl<W: io::Write> Writer<W> {
pub fn new(wtr: W) -> Writer<W> {
Writer {
wtr: io::BufWriter::new(wtr),
}
}
//
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Writer<File>> {
Ok(Writer::new(File::create(path)?))
}
// other fields...
}
Now when I try and call Writer
:
let mut writer = Writer::from_path("./test.refer")?; // fails
let mut writer = Writer::<Vec<u8>>::from_path("./test.refer")?; // okay!
I'm sure this is something to do with traits that I don't understand. Many thanks,
M