comparison rust/hg-cpython/src/copy_tracing.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 be3b545c5cff
children
comparison
equal deleted inserted replaced
50978:27e773aa607d 50979:4c5f6e95df84
12 use hg::copy_tracing::ChangedFiles; 12 use hg::copy_tracing::ChangedFiles;
13 use hg::copy_tracing::CombineChangesetCopies; 13 use hg::copy_tracing::CombineChangesetCopies;
14 use hg::Revision; 14 use hg::Revision;
15 15
16 use crate::pybytes_deref::PyBytesDeref; 16 use crate::pybytes_deref::PyBytesDeref;
17 use crate::PyRevision;
17 18
18 /// Combines copies information contained into revision `revs` to build a copy 19 /// Combines copies information contained into revision `revs` to build a copy
19 /// map. 20 /// map.
20 /// 21 ///
21 /// See mercurial/copies.py for details 22 /// See mercurial/copies.py for details
22 pub fn combine_changeset_copies_wrapper( 23 pub fn combine_changeset_copies_wrapper(
23 py: Python, 24 py: Python,
24 revs: PyList, 25 revs: PyList,
25 children_count: PyDict, 26 children_count: PyDict,
26 target_rev: Revision, 27 target_rev: PyRevision,
27 rev_info: PyObject, 28 rev_info: PyObject,
28 multi_thread: bool, 29 multi_thread: bool,
29 ) -> PyResult<PyDict> { 30 ) -> PyResult<PyDict> {
31 let target_rev = Revision(target_rev.0);
30 let children_count = children_count 32 let children_count = children_count
31 .items(py) 33 .items(py)
32 .iter() 34 .iter()
33 .map(|(k, v)| Ok((k.extract(py)?, v.extract(py)?))) 35 .map(|(k, v)| {
36 Ok((Revision(k.extract::<PyRevision>(py)?.0), v.extract(py)?))
37 })
34 .collect::<PyResult<_>>()?; 38 .collect::<PyResult<_>>()?;
35 39
36 /// (Revision number, parent 1, parent 2, copy data for this revision) 40 /// (Revision number, parent 1, parent 2, copy data for this revision)
37 type RevInfo<Bytes> = (Revision, Revision, Revision, Option<Bytes>); 41 type RevInfo<Bytes> = (Revision, Revision, Revision, Option<Bytes>);
38 42
39 let revs_info = 43 let revs_info =
40 revs.iter(py).map(|rev_py| -> PyResult<RevInfo<PyBytes>> { 44 revs.iter(py).map(|rev_py| -> PyResult<RevInfo<PyBytes>> {
41 let rev = rev_py.extract(py)?; 45 let rev = Revision(rev_py.extract::<PyRevision>(py)?.0);
42 let tuple: PyTuple = 46 let tuple: PyTuple =
43 rev_info.call(py, (rev_py,), None)?.cast_into(py)?; 47 rev_info.call(py, (rev_py,), None)?.cast_into(py)?;
44 let p1 = tuple.get_item(py, 0).extract(py)?; 48 let p1 =
45 let p2 = tuple.get_item(py, 1).extract(py)?; 49 Revision(tuple.get_item(py, 0).extract::<PyRevision>(py)?.0);
50 let p2 =
51 Revision(tuple.get_item(py, 1).extract::<PyRevision>(py)?.0);
46 let opt_bytes = tuple.get_item(py, 2).extract(py)?; 52 let opt_bytes = tuple.get_item(py, 2).extract(py)?;
47 Ok((rev, p1, p2, opt_bytes)) 53 Ok((rev, p1, p2, opt_bytes))
48 }); 54 });
49 55
50 let path_copies; 56 let path_copies;
177 py_fn!( 183 py_fn!(
178 py, 184 py,
179 combine_changeset_copies_wrapper( 185 combine_changeset_copies_wrapper(
180 revs: PyList, 186 revs: PyList,
181 children: PyDict, 187 children: PyDict,
182 target_rev: Revision, 188 target_rev: PyRevision,
183 rev_info: PyObject, 189 rev_info: PyObject,
184 multi_thread: bool 190 multi_thread: bool
185 ) 191 )
186 ), 192 ),
187 )?; 193 )?;