# HG changeset patch # User Georges Racinet # Date 2024-04-22 17:47:08 # Node ID b08c5fbe0e70a56f761bec76ef2ae3018d36fef6 # Parent ccf5c44092db24a6391d6f5bf3bb82872b9ad9eb rust: blanket implementation of Graph for Graph references The need comes from the fact that `AncestorsIterator` and many Graph-related algorithms take ownership of the `Graph` they work with. This, in turn is due to them needing to accept the `Index` instances that are provided by the Python layers (that neither rhg nor `RHGitaly` use, of course): the fact that nowadays the Python layer holds an object that is itself implemented in Rust does not change the core problem that they cannot be tracked by the borrow checker. Even though it looks like cloning `Changelog` would be cheap, it seems hard to guarantee that on the long run. The object is already too rich for us to be comfortable with it, when using references is the most natural and guaranteed way of proceeding. The added test seems a bit superfleous, but it will act as a reminder that this feature is really useful until something in the Mercurial code base actually uses it. diff --git a/rust/hg-core/src/ancestors.rs b/rust/hg-core/src/ancestors.rs --- a/rust/hg-core/src/ancestors.rs +++ b/rust/hg-core/src/ancestors.rs @@ -423,6 +423,18 @@ mod tests { ), vec![8, 7, 4, 3, 2, 1, 0] ); + // it works as well on references, because &Graph implements Graph + // this is needed as of this writing by RHGitaly + assert_eq!( + list_ancestors( + &SampleGraph, + vec![11.into(), 13.into()], + 0.into(), + false + ), + vec![8, 7, 4, 3, 2, 1, 0] + ); + assert_eq!( list_ancestors( SampleGraph, diff --git a/rust/hg-core/src/revlog/mod.rs b/rust/hg-core/src/revlog/mod.rs --- a/rust/hg-core/src/revlog/mod.rs +++ b/rust/hg-core/src/revlog/mod.rs @@ -132,6 +132,12 @@ pub enum GraphError { ParentOutOfRange(Revision), } +impl Graph for &T { + fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> { + (*self).parents(rev) + } +} + /// The Mercurial Revlog Index /// /// This is currently limited to the minimal interface that is needed for