| 44 | LazyLock::new(|| RwLock::new(HashMap::new())); |
| 45 | |
| 46 | fn 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 |