comparison rust/hg-cpython/src/lib.rs @ 50979:4c5f6e95df84

rust: make `Revision` a newtype This change is the one we've been building towards during this series. The aim is to make `Revision` mean more than a simple integer, holding the information that it is valid for a given revlog index. While this still allows for programmer error, since creating a revision directly and querying a different index with a "checked" revision are still possible, the friction created by the newtype will hopefully make us think twice about which type to use. Enough of the Rust ecosystem relies on the newtype pattern to be efficiently optimized away (even compiler in codegen testsĀ¹), so I'm not worried about this being a fundamental problem. [1] https://github.com/rust-lang/rust/blob/7a70647f195f6b0a0f1ebd72b1542ba91a32f43a/tests/codegen/vec-in-place.rs#L47
author Raphaël Gomès <rgomes@octobus.net>
date Fri, 18 Aug 2023 14:34:29 +0200
parents 136aa80aa8b2
children
comparison
equal deleted inserted replaced
50978:27e773aa607d 50979: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__",