rust/hg-cpython/src/cindex.rs
changeset 44066 f5d2720f3bea
parent 44010 2728fcb8127c
child 44502 166349510398
equal deleted inserted replaced
44065:ab41dad7345e 44066:f5d2720f3bea
     8 //! Bindings to use the Index defined by the parsers C extension
     8 //! Bindings to use the Index defined by the parsers C extension
     9 //!
     9 //!
    10 //! Ideally, we should use an Index entirely implemented in Rust,
    10 //! Ideally, we should use an Index entirely implemented in Rust,
    11 //! but this will take some time to get there.
    11 //! but this will take some time to get there.
    12 
    12 
    13 use cpython::{PyClone, PyObject, PyResult, Python};
    13 use cpython::{exc::ImportError, PyClone, PyErr, PyObject, PyResult, Python};
    14 use hg::{Graph, GraphError, Revision, WORKING_DIRECTORY_REVISION};
    14 use hg::{Graph, GraphError, Revision, WORKING_DIRECTORY_REVISION};
    15 use libc::c_int;
    15 use libc::c_int;
    16 
    16 
       
    17 const REVLOG_CABI_VERSION: c_int = 1;
       
    18 
    17 #[repr(C)]
    19 #[repr(C)]
    18 pub struct Revlog_CAPI {
    20 pub struct Revlog_CAPI {
       
    21     abi_version: c_int,
    19     index_parents: unsafe extern "C" fn(
    22     index_parents: unsafe extern "C" fn(
    20         index: *mut revlog_capi::RawPyObject,
    23         index: *mut revlog_capi::RawPyObject,
    21         rev: c_int,
    24         rev: c_int,
    22         ps: *mut [c_int; 2],
    25         ps: *mut [c_int; 2],
    23     ) -> c_int,
    26     ) -> c_int,
    64     capi: &'static Revlog_CAPI,
    67     capi: &'static Revlog_CAPI,
    65 }
    68 }
    66 
    69 
    67 impl Index {
    70 impl Index {
    68     pub fn new(py: Python, index: PyObject) -> PyResult<Self> {
    71     pub fn new(py: Python, index: PyObject) -> PyResult<Self> {
       
    72         let capi = unsafe { revlog_capi::retrieve(py)? };
       
    73         if capi.abi_version != REVLOG_CABI_VERSION {
       
    74             return Err(PyErr::new::<ImportError, _>(
       
    75                 py,
       
    76                 format!(
       
    77                     "ABI version mismatch: the C ABI revlog version {} \
       
    78                      does not match the {} expected by Rust hg-cpython",
       
    79                     capi.abi_version, REVLOG_CABI_VERSION
       
    80                 ),
       
    81             ));
       
    82         }
    69         Ok(Index {
    83         Ok(Index {
    70             index: index,
    84             index: index,
    71             capi: unsafe { revlog_capi::retrieve(py)? },
    85             capi: capi,
    72         })
    86         })
    73     }
    87     }
    74 
    88 
    75     /// return a reference to the CPython Index object in this Struct
    89     /// return a reference to the CPython Index object in this Struct
    76     pub fn inner(&self) -> &PyObject {
    90     pub fn inner(&self) -> &PyObject {