MCPcopy Index your code
hub / github.com/carllerche/assert-struct / cached_source

Function cached_source

assert-struct/src/error.rs:46–59  ·  view source on GitHub ↗
(path: &PathBuf)

Source from the content-addressed store, hash-verified

44 LazyLock::new(|| RwLock::new(HashMap::new()));
45
46fn cached_source(path: &PathBuf) -> Option<Arc<str>> {
47 // Fast path: already cached.
48 if let Ok(cache) = SOURCE_CACHE.read() {
49 if let Some(content) = cache.get(path) {
50 return Some(content.clone());
51 }
52 }
53 // Slow path: read file and populate cache.
54 let content: Arc<str> = std::fs::read_to_string(path).ok()?.into();
55 if let Ok(mut cache) = SOURCE_CACHE.write() {
56 cache.entry(path.clone()).or_insert_with(|| content.clone());
57 }
58 Some(content)
59}
60
61/// Derive the absolute source file path from two compile-time constants:
62/// - `manifest_dir`: value of `env!("CARGO_MANIFEST_DIR")` — absolute path to the package root

Callers 1

fmtMethod · 0.85

Calls 1

getMethod · 0.80

Tested by

no test coverage detected