##// END OF EJS Templates
rust-index: add a `inner` method to the Index struct...
rust-index: add a `inner` method to the Index struct We are about to introduce a Rust wrapper to the C index. It will be exposed as a Python object. As a start all call will be forwarded to an internal index object (as used now). For this forwarding to works, we need to access that index object (from the C extensions). We adds a method doing to on the existing `Index` rust struct. Differential Revision: https://phab.mercurial-scm.org/D7654

File last commit:

r44412:ab3fd807 default
r44412:ab3fd807 default
Show More
cindex.rs
110 lines | 3.5 KiB | application/rls-services+xml | RustLexer
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 // cindex.rs
//
// Copyright 2018 Georges Racinet <gracinet@anybox.fr>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//! Bindings to use the Index defined by the parsers C extension
//!
//! Ideally, we should use an Index entirely implemented in Rust,
//! but this will take some time to get there.
Yuya Nishihara
rust-cpython: leverage upstreamed py_capsule_fn!() macro
r43484 use cpython::{PyClone, PyObject, PyResult, Python};
Georges Racinet
rust-cpython: raising error.WdirUnsupported...
r41386 use hg::{Graph, GraphError, Revision, WORKING_DIRECTORY_REVISION};
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 use libc::c_int;
Georges Racinet
revlog: made C Capsule an array of function pointers...
r44411 #[repr(C)]
pub struct Revlog_CAPI {
index_parents: unsafe extern "C" fn(
index: *mut revlog_capi::RawPyObject,
rev: c_int,
ps: *mut [c_int; 2],
) -> c_int,
}
py_capsule!(
from mercurial.cext.parsers import revlog_CAPI
as revlog_capi for Revlog_CAPI);
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082
/// A `Graph` backed up by objects and functions from revlog.c
///
/// This implementation of the `Graph` trait, relies on (pointers to)
/// - the C index object (`index` member)
/// - the `index_get_parents()` function (`parents` member)
///
/// # Safety
///
/// The C index itself is mutable, and this Rust exposition is **not
/// protected by the GIL**, meaning that this construct isn't safe with respect
/// to Python threads.
///
/// All callers of this `Index` must acquire the GIL and must not release it
/// while working.
///
/// # TODO find a solution to make it GIL safe again.
///
/// This is non trivial, and can wait until we have a clearer picture with
/// more Rust Mercurial constructs.
///
/// One possibility would be to a `GILProtectedIndex` wrapper enclosing
/// a `Python<'p>` marker and have it be the one implementing the
/// `Graph` trait, but this would mean the `Graph` implementor would become
/// likely to change between subsequent method invocations of the `hg-core`
/// objects (a serious change of the `hg-core` API):
/// either exposing ways to mutate the `Graph`, or making it a non persistent
/// parameter in the relevant methods that need one.
///
/// Another possibility would be to introduce an abstract lock handle into
/// the core API, that would be tied to `GILGuard` / `Python<'p>`
/// in the case of the `cpython` crate bindings yet could leave room for other
/// mechanisms in other contexts.
pub struct Index {
index: PyObject,
Georges Racinet
revlog: made C Capsule an array of function pointers...
r44411 capi: &'static Revlog_CAPI,
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 }
impl Index {
pub fn new(py: Python, index: PyObject) -> PyResult<Self> {
Ok(Index {
index: index,
Georges Racinet
revlog: made C Capsule an array of function pointers...
r44411 capi: unsafe { revlog_capi::retrieve(py)? },
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 })
}
Georges Racinet
rust-index: add a `inner` method to the Index struct...
r44412
/// return a reference to the CPython Index object in this Struct
pub fn inner(&self) -> &PyObject {
&self.index
}
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 }
Georges Racinet
rust: core implementation for lazyancestors...
r41084 impl Clone for Index {
fn clone(&self) -> Self {
let guard = Python::acquire_gil();
Index {
index: self.index.clone_ref(guard.python()),
Georges Racinet
revlog: made C Capsule an array of function pointers...
r44411 capi: self.capi,
Georges Racinet
rust: core implementation for lazyancestors...
r41084 }
}
}
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 impl Graph for Index {
/// wrap a call to the C extern parents function
fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
Georges Racinet
rust-cpython: raising error.WdirUnsupported...
r41386 if rev == WORKING_DIRECTORY_REVISION {
return Err(GraphError::WorkingDirectoryUnsupported);
}
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 let mut res: [c_int; 2] = [0; 2];
let code = unsafe {
Georges Racinet
revlog: made C Capsule an array of function pointers...
r44411 (self.capi.index_parents)(
Georges Racinet
rust-cpython: implement Graph using C parents function...
r41082 self.index.as_ptr(),
rev as c_int,
&mut res as *mut [c_int; 2],
)
};
match code {
0 => Ok(res),
_ => Err(GraphError::ParentOutOfRange(rev)),
}
}
}