D5438: rust-cpython: implementing Graph using C parents function

yuja (Yuya Nishihara) phabricator at mercurial-scm.org
Sat Dec 15 22:42:44 EST 2018


yuja added a comment.


  > +type IndexParentsFn = unsafe extern "C" fn(
  >  +    index: *mut python_sys::PyObject,
  >  +    rev: ssize_t,
  >  +    ps: *mut [c_int; 2],
  >  +    max_rev: c_int,
  >  +) -> c_int;
  
  The type differs from the private function. It's
  `int HgRevlogIndex_GetParents(PyObject *op, int rev, int *ps)`.
  
  > +impl Graph for Index {
  >  +    /// wrap a call to the C extern parents function
  >  +    fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
  >  +        let mut res: [c_int; 2] = [0; 2];
  >  +        let code = unsafe {
  >  +            (self.parents)(
  >  +                self.index.as_ptr(),
  >  +                rev as ssize_t,
  >  +                &mut res as *mut [c_int; 2],
  >  +                rev,
  >  +            )
  >  +        };
  >  +        match code {
  >  +            0 => Ok(res),
  >  +            _ => Err(GraphError::ParentOutOfRange(rev)),
  >  +        }
  
  I think `Python<'p>` is needed here to make it safe, but I have no idea how
  to express that clearly. Do we need a wrapper that implements `Graph` and
  translates `Graph::parents(rev)` to `Index::parents(py, rev)`?
  
  > - a/mercurial/cext/revlog.c +++ b/mercurial/cext/revlog.c @@ -2878,6 +2878,12 @@ 	if (nullentry) 		PyObject_GC_UnTrack(nullentry);
  > 
  >   +	void *caps = PyCapsule_New(
  
  Please move `void *caps` to top. We still need to conform to C89.

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D5438

To: gracinet, #hg-reviewers
Cc: yuja, durin42, kevincox, mercurial-devel


More information about the Mercurial-devel mailing list