tiger_lib/
pathtable.rs

1//! A global table for the pathnames used in `FileEntry` and `Loc`.
2//!
3//! Using this will make the often-cloned Loc faster to copy, since it will just contain an index into the global table.
4//! It also makes it faster to compare pathnames, because the table will be created in lexical order by the caller
5//! ([`Fileset`](crate::fileset::Fileset)), with the exception of some stray files (such as the config file)
6//! where the order doesn't matter.
7use std::hash::Hash;
8use std::path::{Path, PathBuf};
9use std::sync::{LazyLock, RwLock};
10
11#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
12pub struct PathTableIndex(u32);
13
14static PATHTABLE: LazyLock<RwLock<PathTable>> = LazyLock::new(|| RwLock::new(PathTable::default()));
15
16/// A global table for the pathnames used in `FileEntry` and `Loc`.
17///
18/// See the [`self`](module-level documentation) for details.
19#[derive(Debug, Default)]
20pub struct PathTable {
21    /// This is indexed by a `PathTableIndex`. It contains two paths per entry: a path relative to
22    /// a `FileKind` root, and a full filesystem path.
23    ///
24    /// The paths must never be moved. This works even though the `Vec` can reallocate, because the
25    /// underlying paths are constructed from leaked `Pathbuf`s.
26    paths: Vec<(&'static Path, &'static Path)>,
27}
28
29impl PathTable {
30    /// Stores a path in the path table and returns the index for the entry.
31    /// It's assumed that the caller has a master list of paths and won't store duplicates.
32    ///
33    /// The indexes are guaranteed to be in ascending order, so that if the caller stores a sorted
34    /// list of paths then the indexes will also be sorted.
35    pub fn store(local: PathBuf, fullpath: PathBuf) -> PathTableIndex {
36        PATHTABLE.write().unwrap().store_internal(local, fullpath)
37    }
38
39    fn store_internal(&mut self, local: PathBuf, fullpath: PathBuf) -> PathTableIndex {
40        let idx = PathTableIndex(u32::try_from(self.paths.len()).expect("internal error"));
41        let local = Box::leak(local.into_boxed_path());
42        let fullpath = Box::leak(fullpath.into_boxed_path());
43        self.paths.push((local, fullpath));
44        idx
45    }
46
47    /// Return the local path based on its index.
48    /// This can panic if the index is not one provided by `PathTable::store`.
49    pub fn lookup_path(idx: PathTableIndex) -> &'static Path {
50        PATHTABLE.read().unwrap().lookup_paths_inner(idx).0
51    }
52
53    /// Return the full path based on its index.
54    /// This can panic if the index is not one provided by `PathTable::store`.
55    pub fn lookup_fullpath(idx: PathTableIndex) -> &'static Path {
56        PATHTABLE.read().unwrap().lookup_paths_inner(idx).1
57    }
58
59    #[inline]
60    fn lookup_paths_inner(&self, idx: PathTableIndex) -> (&'static Path, &'static Path) {
61        let PathTableIndex(idx) = idx;
62        // This will panic if idx is out of range.
63        // Should never happen as long as lookups are only done on PathTableIndex provided by this module.
64        self.paths[idx as usize]
65    }
66}