changeset 53207:4682ca00a4f0 stable

rust-nodemap: add a `catch_up_to_index` method We're going to re-use this logic in `hg-core`, so let's refactor it first.
author Rapha?l Gom?s <rgomes@octobus.net>
date Mon, 13 Jan 2025 14:01:12 +0100
parents dbbf4d4ee720
children 8f3d4e2dedce
files rust/hg-core/src/revlog/nodemap.rs
diffstat 1 files changed, 26 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/rust/hg-core/src/revlog/nodemap.rs	Wed Apr 16 22:54:12 2025 +0200
+++ b/rust/hg-core/src/revlog/nodemap.rs	Mon Jan 13 14:01:12 2025 +0100
@@ -14,6 +14,7 @@
 
 use crate::UncheckedRevision;
 
+use super::BaseRevision;
 use super::{
     node::NULL_NODE, Node, NodePrefix, Revision, RevlogIndex, NULL_REVISION,
 };
@@ -27,7 +28,7 @@
 
 type NodeTreeBuffer = Box<dyn Deref<Target = [u8]> + Send + Sync>;
 
-#[derive(Debug, PartialEq)]
+#[derive(Debug, PartialEq, derive_more::Display)]
 pub enum NodeMapError {
     /// A `NodePrefix` matches several [`Revision`]s.
     ///
@@ -529,6 +530,30 @@
         Ok(())
     }
 
+    /// Insert all [`Revision`] from `from` inclusive, up to
+    /// [`RevlogIndex::len`] exclusive.
+    ///
+    /// `from` must be a valid revision for `index`, most likely should be the
+    /// tip of the nodemap docket.
+    ///
+    /// Useful for updating the [`NodeTree`] when the index has moved forward.
+    pub fn catch_up_to_index(
+        &mut self,
+        index: &impl RevlogIndex,
+        from: Revision,
+    ) -> Result<(), NodeMapError> {
+        for r in (from.0)..index.len() as BaseRevision {
+            let rev = Revision(r);
+            // in this case node() won't ever return None
+            self.insert(
+                index,
+                index.node(rev).expect("node should exist"),
+                rev,
+            )?;
+        }
+        Ok(())
+    }
+
     /// Make the whole `NodeTree` logically empty, without touching the
     /// immutable part.
     pub fn invalidate_all(&mut self) {