Mercurial > hg
diff rust/hg-core/src/dirstate_tree/dirstate_map.rs @ 47123:d8ac62374943
dirstate-tree: Make `DirstateMap` borrow from a bytes buffer
… that has the contents of the `.hg/dirstate` file.
This only applies to the tree-based flavor of `DirstateMap`.
For now only the entire `&[u8]` slice is stored, so this is not useful yet.
Adding a lifetime parameter to the `DirstateMap` struct (in hg-core) makes
Python bindings non-trivial because we keep that struct in a Python object
that has a dynamic lifetime tied to Python’s reference-counting and GC.
As long as we keep the `PyBytes` that owns the borrowed bytes buffer next to
the borrowing struct, the buffer will live long enough for the borrows to stay
valid. However this relationship cannot be expressed in safe Rust code in a
way that would statisfy they borrow-checker. We use `unsafe` code to erase
that lifetime parameter, and encapsulate it in a safe abstraction similar to
the owning-ref crate: https://docs.rs/owning_ref/
Differential Revision: https://phab.mercurial-scm.org/D10557
author | Simon Sapin <simon.sapin@octobus.net> |
---|---|
date | Fri, 30 Apr 2021 18:24:54 +0200 |
parents | b6339a993b91 |
children | cd8ca38fccff |
line wrap: on
line diff
--- a/rust/hg-core/src/dirstate_tree/dirstate_map.rs Fri Apr 30 18:13:31 2021 +0200 +++ b/rust/hg-core/src/dirstate_tree/dirstate_map.rs Fri Apr 30 18:24:54 2021 +0200 @@ -24,7 +24,10 @@ use crate::StatusError; use crate::StatusOptions; -pub struct DirstateMap { +pub struct DirstateMap<'on_disk> { + /// Contents of the `.hg/dirstate` file + on_disk: &'on_disk [u8], + pub(super) root: ChildNodes, /// Number of nodes anywhere in the tree that have `.entry.is_some()`. @@ -69,13 +72,58 @@ &'a mut Option<HgPathBuf>, ); -impl DirstateMap { - pub fn new() -> Self { - Self { +impl<'on_disk> DirstateMap<'on_disk> { + pub fn new( + on_disk: &'on_disk [u8], + ) -> Result<(Self, Option<DirstateParents>), DirstateError> { + let mut map = Self { + on_disk, root: ChildNodes::default(), nodes_with_entry_count: 0, nodes_with_copy_source_count: 0, + }; + let parents = map.read()?; + Ok((map, parents)) + } + + /// Should only be called in `new` + #[timed] + fn read(&mut self) -> Result<Option<DirstateParents>, DirstateError> { + if self.on_disk.is_empty() { + return Ok(None); } + + let parents = parse_dirstate_entries( + self.on_disk, + |path, entry, copy_source| { + let tracked = entry.state.is_tracked(); + let node = Self::get_or_insert_node_tracing_ancestors( + &mut self.root, + path, + |ancestor| { + if tracked { + ancestor.tracked_descendants_count += 1 + } + }, + ); + assert!( + node.entry.is_none(), + "duplicate dirstate entry in read" + ); + assert!( + node.copy_source.is_none(), + "duplicate dirstate entry in read" + ); + node.entry = Some(*entry); + node.copy_source = copy_source.map(HgPath::to_owned); + self.nodes_with_entry_count += 1; + if copy_source.is_some() { + self.nodes_with_copy_source_count += 1 + } + }, + )?; + + Ok(Some(parents.clone())) } fn get_node(&self, path: &HgPath) -> Option<&Node> { @@ -280,7 +328,7 @@ } } -impl super::dispatch::DirstateMapMethods for DirstateMap { +impl<'on_disk> super::dispatch::DirstateMapMethods for DirstateMap<'on_disk> { fn clear(&mut self) { self.root.clear(); self.nodes_with_entry_count = 0; @@ -443,48 +491,6 @@ } } - #[timed] - fn read<'a>( - &mut self, - file_contents: &'a [u8], - ) -> Result<Option<&'a DirstateParents>, DirstateError> { - if file_contents.is_empty() { - return Ok(None); - } - - let parents = parse_dirstate_entries( - file_contents, - |path, entry, copy_source| { - let tracked = entry.state.is_tracked(); - let node = Self::get_or_insert_node_tracing_ancestors( - &mut self.root, - path, - |ancestor| { - if tracked { - ancestor.tracked_descendants_count += 1 - } - }, - ); - assert!( - node.entry.is_none(), - "duplicate dirstate entry in read" - ); - assert!( - node.copy_source.is_none(), - "duplicate dirstate entry in read" - ); - node.entry = Some(*entry); - node.copy_source = copy_source.map(HgPath::to_owned); - self.nodes_with_entry_count += 1; - if copy_source.is_some() { - self.nodes_with_copy_source_count += 1 - } - }, - )?; - - Ok(Some(parents)) - } - fn pack( &mut self, parents: DirstateParents,