This way it should be possible to access paths immutably even while entries are available mutably, assuming we stagger accesses to put mutation of entries last.
Unified · Split
| @@ -1,6 +1,6 @@ | ||
| 1 | 1 | use bstr::{BStr, ByteSlice}; |
| 2 | 2 | |
| 3 | −use crate::{extension, Entry, State, Version}; | |
| 3 | +use crate::{extension, Entry, PathStorage, State, Version}; | |
| 4 | 4 | |
| 5 | 5 | impl State { |
| 6 | 6 | pub fn version(&self) -> Version { |
| @@ -10,6 +10,23 @@ impl State { · 3 unmodified lines | ||
| 10 | 10 | pub fn entries(&self) -> &[Entry] { |
| 11 | 11 | &self.entries |
| 12 | 12 | } |
| 13 | + pub fn take_path_backing(&mut self) -> PathStorage { | |
| 14 | + assert_eq!( | |
| 15 | + self.entries.is_empty(), | |
| 16 | + self.path_backing.is_empty(), | |
| 17 | + "BUG: cannot take out backing multiple times" | |
| 18 | + ); | |
| 19 | + std::mem::take(&mut self.path_backing) | |
| 20 | + } | |
| 21 | + | |
| 22 | + pub fn return_path_backing(&mut self, backing: PathStorage) { | |
| 23 | + assert!( | |
| 24 | + self.path_backing.is_empty(), | |
| 25 | + "BUG: return path backing only after taking it, once" | |
| 26 | + ); | |
| 27 | + self.path_backing = backing; | |
| 28 | + } | |
| 29 | + | |
| 13 | 30 | pub fn entries_with_paths_by_filter_map<'a, T>( |
| 14 | 31 | &'a self, |
| 15 | 32 | mut filter_map: impl FnMut(&'a BStr, &Entry) -> Option<T> + 'a, |
| @@ -29,6 +46,15 @@ impl State { · 13 unmodified lines | ||
| 29 | 46 | (e, path) |
| 30 | 47 | }) |
| 31 | 48 | } |
| 49 | + pub fn entries_mut_with_paths_in<'state, 'backing>( | |
| 50 | + &'state mut self, | |
| 51 | + backing: &'backing PathStorage, | |
| 52 | + ) -> impl Iterator<Item = (&'state mut Entry, &'backing BStr)> { | |
| 53 | + self.entries.iter_mut().map(move |e| { | |
| 54 | + let path = (&backing[e.path.clone()]).as_bstr(); | |
| 55 | + (e, path) | |
| 56 | + }) | |
| 57 | + } | |
| 32 | 58 | pub fn tree(&self) -> Option<&extension::Tree> { |
| 33 | 59 | self.tree.as_ref() |
| 34 | 60 | } |
| @@ -143,6 +143,10 @@ mod access { · 142 unmodified lines | ||
| 143 | 143 | (&state.path_backing[self.path.clone()]).as_bstr() |
| 144 | 144 | } |
| 145 | 145 | |
| 146 | + pub fn path_in<'backing>(&self, backing: &'backing crate::PathStorage) -> &'backing BStr { | |
| 147 | + (backing[self.path.clone()]).as_bstr() | |
| 148 | + } | |
| 149 | + | |
| 146 | 150 | pub fn stage(&self) -> u32 { |
| 147 | 151 | self.flags.stage() |
| 148 | 152 | } |
| @@ -51,6 +51,9 @@ pub struct File { · 50 unmodified lines | ||
| 51 | 51 | pub checksum: git_hash::ObjectId, |
| 52 | 52 | } |
| 53 | 53 | |
| 54 | +/// The type to use and store paths to all entries. | |
| 55 | +pub type PathStorage = Vec<u8>; | |
| 56 | + | |
| 54 | 57 | /// An in-memory cache of a fully parsed git index file. |
| 55 | 58 | /// |
| 56 | 59 | /// As opposed to a snapshot, it's meant to be altered and eventually be written back to disk or converted into a tree. |
| @@ -65,7 +68,7 @@ pub struct State { · 8 unmodified lines | ||
| 65 | 68 | version: Version, |
| 66 | 69 | entries: Vec<Entry>, |
| 67 | 70 | /// A memory area keeping all index paths, in full length, independently of the index version. |
| 68 | − path_backing: Vec<u8>, | |
| 71 | + path_backing: PathStorage, | |
| 69 | 72 | /// True if one entry in the index has a special marker mode |
| 70 | 73 | #[allow(dead_code)] |
| 71 | 74 | is_sparse: bool, |