Mercurial > public > mercurial-scm > hg-stable
changeset 52848:bc095c0db77c
rust-pyo3: implement missing traits for `PySharedIndex`
This is going to be needed for the ancestors module once we switch over
to the pyo3 innerrevlog
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Sun, 05 Jan 2025 23:21:56 +0100 |
parents | 8ebe20a6fcb7 |
children | 9749a97d3cfb |
files | rust/hg-pyo3/src/revlog/index.rs |
diffstat | 1 files changed, 33 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/rust/hg-pyo3/src/revlog/index.rs Tue Jan 07 17:57:58 2025 +0100 +++ b/rust/hg-pyo3/src/revlog/index.rs Sun Jan 05 23:21:56 2025 +0100 @@ -7,15 +7,17 @@ // This software may be used and distributed according to the terms of the // GNU General Public License version 2 or any later version. //! Utilities for dealing with the index at the Python boundary +use hg::{BaseRevision, Graph}; use pyo3::prelude::*; use pyo3::types::{PyBytes, PyTuple}; +use vcsgraph::graph::Graph as VCSGraph; use hg::revlog::{ index::{Index, RevisionDataParams}, Node, Revision, RevlogIndex, }; -#[derive(derive_more::From)] +#[derive(derive_more::From, Clone)] pub struct PySharedIndex { /// The underlying hg-core index inner: &'static Index, @@ -43,6 +45,36 @@ } } +impl Graph for PySharedIndex { + #[inline(always)] + fn parents(&self, rev: Revision) -> Result<[Revision; 2], hg::GraphError> { + self.inner.parents(rev) + } +} + +impl VCSGraph for PySharedIndex { + #[inline(always)] + fn parents( + &self, + rev: BaseRevision, + ) -> Result<vcsgraph::graph::Parents, vcsgraph::graph::GraphReadError> + { + // FIXME This trait should be reworked to decide between Revision + // and UncheckedRevision, get better errors names, etc. + match Graph::parents(self, Revision(rev)) { + Ok(parents) => { + Ok(vcsgraph::graph::Parents([parents[0].0, parents[1].0])) + } + Err(hg::GraphError::ParentOutOfRange(rev)) => { + Err(vcsgraph::graph::GraphReadError::KeyedInvalidKey(rev.0)) + } + Err(hg::GraphError::ParentOutOfOrder(rev)) => { + Err(vcsgraph::graph::GraphReadError::KeyedInvalidKey(rev.0)) + } + } + } +} + pub fn py_tuple_to_revision_data_params( tuple: &Bound<'_, PyTuple>, ) -> PyResult<RevisionDataParams> {