rust/hg-cpython/src/lib.rs
changeset 50976 4c5f6e95df84
parent 49932 136aa80aa8b2
child 52056 8b7123c8947b
equal deleted inserted replaced
50975:27e773aa607d 50976:4c5f6e95df84
    22 #![allow(clippy::zero_ptr)] // rust-cpython macros
    22 #![allow(clippy::zero_ptr)] // rust-cpython macros
    23 #![allow(clippy::needless_update)] // rust-cpython macros
    23 #![allow(clippy::needless_update)] // rust-cpython macros
    24 #![allow(clippy::manual_strip)] // rust-cpython macros
    24 #![allow(clippy::manual_strip)] // rust-cpython macros
    25 #![allow(clippy::type_complexity)] // rust-cpython macros
    25 #![allow(clippy::type_complexity)] // rust-cpython macros
    26 
    26 
       
    27 use cpython::{FromPyObject, PyInt, Python, ToPyObject};
       
    28 use hg::{BaseRevision, Revision};
       
    29 
    27 /// This crate uses nested private macros, `extern crate` is still needed in
    30 /// This crate uses nested private macros, `extern crate` is still needed in
    28 /// 2018 edition.
    31 /// 2018 edition.
    29 #[macro_use]
    32 #[macro_use]
    30 extern crate cpython;
    33 extern crate cpython;
    31 
    34 
    41 pub mod discovery;
    44 pub mod discovery;
    42 pub mod exceptions;
    45 pub mod exceptions;
    43 mod pybytes_deref;
    46 mod pybytes_deref;
    44 pub mod revlog;
    47 pub mod revlog;
    45 pub mod utils;
    48 pub mod utils;
       
    49 
       
    50 /// Revision as exposed to/from the Python layer.
       
    51 ///
       
    52 /// We need this indirection because of the orphan rule, meaning we can't
       
    53 /// implement a foreign trait (like [`cpython::ToPyObject`])
       
    54 /// for a foreign type (like [`hg::UncheckedRevision`]).
       
    55 ///
       
    56 /// This also acts as a deterrent against blindly trusting Python to send
       
    57 /// us valid revision numbers.
       
    58 #[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
       
    59 pub struct PyRevision(BaseRevision);
       
    60 
       
    61 impl From<Revision> for PyRevision {
       
    62     fn from(r: Revision) -> Self {
       
    63         PyRevision(r.0)
       
    64     }
       
    65 }
       
    66 
       
    67 impl<'s> FromPyObject<'s> for PyRevision {
       
    68     fn extract(
       
    69         py: Python,
       
    70         obj: &'s cpython::PyObject,
       
    71     ) -> cpython::PyResult<Self> {
       
    72         Ok(Self(obj.extract::<BaseRevision>(py)?))
       
    73     }
       
    74 }
       
    75 
       
    76 impl ToPyObject for PyRevision {
       
    77     type ObjectType = PyInt;
       
    78 
       
    79     fn to_py_object(&self, py: Python) -> Self::ObjectType {
       
    80         self.0.to_py_object(py)
       
    81     }
       
    82 }
    46 
    83 
    47 py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| {
    84 py_module_initializer!(rustext, initrustext, PyInit_rustext, |py, m| {
    48     m.add(
    85     m.add(
    49         py,
    86         py,
    50         "__doc__",
    87         "__doc__",