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

Yuya Nishihara yuya at tcha.org
Sat Dec 15 22:36:38 EST 2018


> +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.


More information about the Mercurial-devel mailing list