rust/hg-cpython/src/cindex.rs
author Augie Fackler <augie@google.com>
Wed, 06 Nov 2019 14:10:09 -0500
changeset 43471 8492c6293c31
parent 43213 0246bbe1045d
child 43959 f384d68d8ea8
permissions -rw-r--r--
hgignore: ignore generated pytype stubs Differential Revision: https://phab.mercurial-scm.org/D7269
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
41052
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     1
// cindex.rs
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     2
//
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     3
// Copyright 2018 Georges Racinet <gracinet@anybox.fr>
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     4
//
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     7
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     8
//! Bindings to use the Index defined by the parsers C extension
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
     9
//!
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    10
//! Ideally, we should use an Index entirely implemented in Rust,
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    11
//! but this will take some time to get there.
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    12
43213
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    13
use cpython::{PyClone, PyObject, PyResult, Python};
41350
ab0d762d89ef rust-cpython: raising error.WdirUnsupported
Georges Racinet <georges.racinet@octobus.net>
parents: 41054
diff changeset
    14
use hg::{Graph, GraphError, Revision, WORKING_DIRECTORY_REVISION};
41052
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    15
use libc::c_int;
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    16
43213
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    17
py_capsule_fn!(
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    18
    from mercurial.cext.parsers import index_get_parents_CAPI
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    19
        as get_parents_capi
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    20
        signature (
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    21
            index: *mut RawPyObject,
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    22
            rev: c_int,
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    23
            ps: *mut [c_int; 2],
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    24
        ) -> c_int
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    25
);
41052
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    26
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    27
/// A `Graph` backed up by objects and functions from revlog.c
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    28
///
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    29
/// This implementation of the `Graph` trait, relies on (pointers to)
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    30
/// - the C index object (`index` member)
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    31
/// - the `index_get_parents()` function (`parents` member)
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    32
///
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    33
/// # Safety
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    34
///
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    35
/// The C index itself is mutable, and this Rust exposition is **not
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    36
/// protected by the GIL**, meaning that this construct isn't safe with respect
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    37
/// to Python threads.
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    38
///
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    39
/// All callers of this `Index` must acquire the GIL and must not release it
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    40
/// while working.
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    41
///
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    42
/// # TODO find a solution to make it GIL safe again.
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    43
///
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    44
/// This is non trivial, and can wait until we have a clearer picture with
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    45
/// more Rust Mercurial constructs.
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    46
///
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    47
/// One possibility would be to a `GILProtectedIndex` wrapper enclosing
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    48
/// a `Python<'p>` marker and have it be the one implementing the
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    49
/// `Graph` trait, but this would mean the `Graph` implementor would become
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    50
/// likely to change between subsequent method invocations of the `hg-core`
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    51
/// objects (a serious change of the `hg-core` API):
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    52
/// either exposing ways to mutate the `Graph`, or making it a non persistent
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    53
/// parameter in the relevant methods that need one.
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    54
///
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    55
/// Another possibility would be to introduce an abstract lock handle into
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    56
/// the core API, that would be tied to `GILGuard` / `Python<'p>`
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    57
/// in the case of the `cpython` crate bindings yet could leave room for other
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    58
/// mechanisms in other contexts.
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    59
pub struct Index {
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    60
    index: PyObject,
43213
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    61
    parents: get_parents_capi::CapsuleFn,
41052
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    62
}
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    63
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    64
impl Index {
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    65
    pub fn new(py: Python, index: PyObject) -> PyResult<Self> {
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    66
        Ok(Index {
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    67
            index: index,
43213
0246bbe1045d rust-cpython: leverage upstreamed py_capsule_fn!() macro
Yuya Nishihara <yuya@tcha.org>
parents: 42609
diff changeset
    68
            parents: get_parents_capi::retrieve(py)?,
41052
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    69
        })
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    70
    }
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    71
}
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    72
41054
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 41052
diff changeset
    73
impl Clone for Index {
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 41052
diff changeset
    74
    fn clone(&self) -> Self {
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 41052
diff changeset
    75
        let guard = Python::acquire_gil();
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 41052
diff changeset
    76
        Index {
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 41052
diff changeset
    77
            index: self.index.clone_ref(guard.python()),
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 41052
diff changeset
    78
            parents: self.parents.clone(),
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 41052
diff changeset
    79
        }
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 41052
diff changeset
    80
    }
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 41052
diff changeset
    81
}
ef54bd33b476 rust: core implementation for lazyancestors
Georges Racinet <gracinet@anybox.fr>
parents: 41052
diff changeset
    82
41052
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    83
impl Graph for Index {
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    84
    /// wrap a call to the C extern parents function
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    85
    fn parents(&self, rev: Revision) -> Result<[Revision; 2], GraphError> {
41350
ab0d762d89ef rust-cpython: raising error.WdirUnsupported
Georges Racinet <georges.racinet@octobus.net>
parents: 41054
diff changeset
    86
        if rev == WORKING_DIRECTORY_REVISION {
ab0d762d89ef rust-cpython: raising error.WdirUnsupported
Georges Racinet <georges.racinet@octobus.net>
parents: 41054
diff changeset
    87
            return Err(GraphError::WorkingDirectoryUnsupported);
ab0d762d89ef rust-cpython: raising error.WdirUnsupported
Georges Racinet <georges.racinet@octobus.net>
parents: 41054
diff changeset
    88
        }
41052
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    89
        let mut res: [c_int; 2] = [0; 2];
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    90
        let code = unsafe {
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    91
            (self.parents)(
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    92
                self.index.as_ptr(),
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    93
                rev as c_int,
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    94
                &mut res as *mut [c_int; 2],
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    95
            )
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    96
        };
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    97
        match code {
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    98
            0 => Ok(res),
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
    99
            _ => Err(GraphError::ParentOutOfRange(rev)),
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
   100
        }
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
   101
    }
4c25038c112c rust-cpython: implement Graph using C parents function
Georges Racinet <gracinet@anybox.fr>
parents:
diff changeset
   102
}