Skip to content
This repository was archived by the owner on Mar 26, 2026. It is now read-only.

Commit 6526d03

Browse files
nevakrienzesterer
authored andcommitted
more sane backwards compatible filecache
1 parent 4ebd26d commit 6526d03

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/source.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,21 @@ impl Cache<Path> for FileCache {
340340
}
341341
}
342342

343+
impl Cache<&Path> for FileCache {
344+
type Storage = String;
345+
346+
fn fetch(&mut self, path: &&Path) -> Result<&Source, impl fmt::Debug> {
347+
Ok::<_, Error>(match self.files.entry(path.to_path_buf()) {
348+
// TODO: Don't allocate here
349+
Entry::Occupied(entry) => entry.into_mut(),
350+
Entry::Vacant(entry) => entry.insert(Source::from(fs::read_to_string(path)?)),
351+
})
352+
}
353+
fn display<'a>(&self, path: &'a& Path) -> Option<impl fmt::Display + 'a> {
354+
Some(Box::new(path.display()))
355+
}
356+
}
357+
343358
/// A [`Cache`] that fetches [`Source`]s using the provided function.
344359
#[derive(Debug, Clone)]
345360
pub struct FnCache<Id, F, I>

0 commit comments

Comments
 (0)