Last active
August 15, 2020 09:46
-
-
Save justanotherdot/ee67b6972d5fb235bcef458d9005e71a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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