Skip to content

Instantly share code, notes, and snippets.

@justanotherdot
Last active August 15, 2020 09:46
Show Gist options
  • Save justanotherdot/ee67b6972d5fb235bcef458d9005e71a to your computer and use it in GitHub Desktop.
Save justanotherdot/ee67b6972d5fb235bcef458d9005e71a to your computer and use it in GitHub Desktop.
use std::{
fs::File,
io::{BufReader, Cursor, Result},
path::Path,
};
trait Open<T> {
fn open(&self) -> Result<T>;
}
impl Open<File> for Path {
fn open(&self) -> Result<File> {
File::open(self)
}
}
impl Open<BufReader<File>> for Path {
fn open(&self) -> Result<BufReader<File>> {
Ok(BufReader::new(File::open(self)?))
}
}
impl Open<Cursor<File>> for Path {
fn open(&self) -> Result<Cursor<File>> {
Ok(Cursor::new(File::open(self)?))
}
}
fn main() {
let p = Path::new("foo");
File::create(p).unwrap();
dbg!(
Open::<File>::open(p).unwrap(),
Open::<BufReader<File>>::open(p).unwrap(),
Open::<Cursor<File>>::open(p).unwrap(),
);
let _: File = dbg!(p.open().unwrap());
let _: BufReader<File> = dbg!(p.open().unwrap());
let _: Cursor<File> = dbg!(p.open().unwrap());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment