[DEV-7084] TAMER: fs: impl File for BufReader

This further simplifies the POC linker.
master
Mike Gerwitz 2020-04-06 22:38:44 -04:00
parent fbfb3c4ba2
commit 7ed0691c45
2 changed files with 24 additions and 4 deletions

View File

@ -41,7 +41,7 @@ use std::collections::HashSet;
use std::ffi::OsString;
use std::fs;
use std::hash::BuildHasher;
use std::io::Result;
use std::io::{BufReader, Read, Result};
use std::path::{Path, PathBuf};
/// A file.
@ -58,6 +58,13 @@ impl File for fs::File {
}
}
impl<F: File + Read> File for BufReader<F> {
/// Open the file at `path` and construct a [`BufReader`] from it.
fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
Ok(BufReader::new(F::open(path)?))
}
}
pub struct CanonicalFile<F: File>(PathBuf, F);
impl<F: File> Into<(PathBuf, F)> for CanonicalFile<F> {
@ -178,6 +185,20 @@ mod test {
}
}
impl Read for DummyFile {
fn read(&mut self, _buf: &mut [u8]) -> Result<usize> {
Ok(0)
}
}
#[test]
fn buf_reader_file() {
let path: PathBuf = "buf/path".into();
let result: BufReader<DummyFile> = File::open(path.clone()).unwrap();
assert_eq!(DummyFile(path), result.into_inner());
}
#[test]
fn vist_once() {
let mut fs = VisitOnceFilesystem::<RandomState>::new();

View File

@ -133,15 +133,14 @@ fn load_xmlo<'a, 'i, I: Interner<'i>>(
let mut found: FxHashSet<&str> = Default::default();
let cfile: CanonicalFile<fs::File> = match fs.open(path_str)? {
let cfile: CanonicalFile<BufReader<fs::File>> = match fs.open(path_str)? {
VisitOnceFile::FirstVisit(file) => file,
VisitOnceFile::Visited => return Ok(None),
};
let (path, file) = cfile.into();
let reader = BufReader::new(file);
let mut xmlo: XmloReader<'_, _, _> = (reader, interner).into();
let mut xmlo: XmloReader<'_, _, _> = (file, interner).into();
let mut elig = None;
let mut name: Option<&'i Symbol<'i>> = None;