Mercurial > public > mercurial-scm > hg
comparison rust/hg-cpython/src/exceptions.rs @ 50976: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 | 3aca98a35727 |
children | 2fb13c3f4496 |
comparison
equal
deleted
inserted
replaced
50975:27e773aa607d | 50976:4c5f6e95df84 |
---|---|
16 exc::{RuntimeError, ValueError}, | 16 exc::{RuntimeError, ValueError}, |
17 py_exception, PyErr, Python, | 17 py_exception, PyErr, Python, |
18 }; | 18 }; |
19 use hg; | 19 use hg; |
20 | 20 |
21 use crate::PyRevision; | |
22 | |
21 py_exception!(rustext, GraphError, ValueError); | 23 py_exception!(rustext, GraphError, ValueError); |
22 | 24 |
23 impl GraphError { | 25 impl GraphError { |
24 pub fn pynew(py: Python, inner: hg::GraphError) -> PyErr { | 26 pub fn pynew(py: Python, inner: hg::GraphError) -> PyErr { |
25 match inner { | 27 match inner { |
26 hg::GraphError::ParentOutOfRange(r) => { | 28 hg::GraphError::ParentOutOfRange(r) => { |
27 GraphError::new(py, ("ParentOutOfRange", r)) | 29 GraphError::new(py, ("ParentOutOfRange", PyRevision(r.0))) |
28 } | 30 } |
29 } | 31 } |
30 } | 32 } |
31 | 33 |
32 pub fn pynew_from_vcsgraph( | 34 pub fn pynew_from_vcsgraph( |