rust/hg-pyo3/src/discovery.rs
author Pierre-Yves David <pierre-yves.david@octobus.net>
Tue, 18 Feb 2025 22:24:08 +0100
changeset 52964 469b9a628b51
parent 52837 01aff9437828
permissions -rw-r--r--
dirstatemap: update, document and type the identity tracking This new form should hopefully be clearer and less error prone.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
52837
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     1
//! Discovery of common node sets
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     2
use std::collections::HashSet;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     3
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     4
use hg::{discovery::PartialDiscovery as CorePartialDiscovery, Revision};
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     5
use pyo3::{
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     6
    intern, pyclass, pymethods,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     7
    types::{PyAnyMethods, PyDict, PyModule, PyModuleMethods, PyTuple},
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     8
    Bound, Py, PyAny, PyObject, PyResult, Python,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
     9
};
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    10
use pyo3_sharedref::SharedByPyObject;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    11
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    12
use crate::{
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    13
    exceptions::GraphError,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    14
    revision::{rev_pyiter_collect, PyRevision},
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    15
    revlog::PySharedIndex,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    16
    utils::{new_submodule, py_rust_index_to_graph},
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    17
};
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    18
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    19
#[pyclass]
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    20
struct PartialDiscovery {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    21
    inner: SharedByPyObject<CorePartialDiscovery<PySharedIndex>>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    22
    idx: SharedByPyObject<PySharedIndex>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    23
}
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    24
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    25
#[pymethods]
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    26
impl PartialDiscovery {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    27
    #[pyo3(signature = (repo, targetheads, respectsize, randomize=true))]
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    28
    #[new]
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    29
    fn new(
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    30
        py: Python,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    31
        repo: &Bound<'_, PyAny>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    32
        targetheads: &Bound<'_, PyAny>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    33
        respectsize: bool,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    34
        randomize: bool,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    35
    ) -> PyResult<Self> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    36
        let index = repo
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    37
            .getattr(intern!(py, "changelog"))?
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    38
            .getattr(intern!(py, "index"))?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    39
        let cloned_index = py_rust_index_to_graph(&index.clone())?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    40
        let index = py_rust_index_to_graph(&index)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    41
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    42
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    43
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    44
        let target_heads = {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    45
            let borrowed_idx = unsafe { index.try_borrow(py)? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    46
            rev_pyiter_collect(targetheads, &*borrowed_idx)?
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    47
        };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    48
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    49
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    50
        let lazy_disco = unsafe {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    51
            index.map(py, |idx| {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    52
                CorePartialDiscovery::new(
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    53
                    idx,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    54
                    target_heads,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    55
                    respectsize,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    56
                    randomize,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    57
                )
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    58
            })
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    59
        };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    60
        Ok(Self {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    61
            inner: lazy_disco,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    62
            idx: cloned_index,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    63
        })
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    64
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    65
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    66
    fn addcommons(
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    67
        &mut self,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    68
        py: Python,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    69
        commons: &Bound<'_, PyAny>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    70
    ) -> PyResult<PyObject> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    71
        let commons = self.pyiter_to_vec(commons)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    72
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    73
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    74
        let mut inner = unsafe { self.inner.try_borrow_mut(py)? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    75
        inner
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    76
            .add_common_revisions(commons)
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    77
            .map_err(GraphError::from_hg)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    78
        Ok(py.None())
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    79
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    80
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    81
    fn addmissings(
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    82
        &mut self,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    83
        py: Python,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    84
        missings: &Bound<'_, PyAny>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    85
    ) -> PyResult<PyObject> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    86
        let missings = self.pyiter_to_vec(missings)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    87
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    88
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    89
        let mut inner = unsafe { self.inner.try_borrow_mut(py)? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    90
        inner
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    91
            .add_missing_revisions(missings)
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    92
            .map_err(GraphError::from_hg)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    93
        Ok(py.None())
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    94
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    95
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    96
    fn addinfo(
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    97
        &mut self,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    98
        py: Python,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
    99
        sample: &Bound<'_, PyAny>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   100
    ) -> PyResult<PyObject> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   101
        let mut missing: Vec<Revision> = vec![];
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   102
        let mut common: Vec<Revision> = vec![];
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   103
        for info in sample.try_iter()? {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   104
            // info is a pair (Revision, bool)
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   105
            let info = info?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   106
            let info = info.downcast::<PyTuple>()?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   107
            let rev: PyRevision = info.get_item(0)?.extract()?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   108
            // This is fine since we're just using revisions as integers
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   109
            // for the purposes of discovery
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   110
            let rev = Revision(rev.0);
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   111
            let known: bool = info.get_item(1)?.extract()?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   112
            if known {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   113
                common.push(rev);
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   114
            } else {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   115
                missing.push(rev);
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   116
            }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   117
        }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   118
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   119
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   120
        let mut inner = unsafe { self.inner.try_borrow_mut(py)? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   121
        inner
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   122
            .add_common_revisions(common)
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   123
            .map_err(GraphError::from_hg)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   124
        inner
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   125
            .add_missing_revisions(missing)
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   126
            .map_err(GraphError::from_hg)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   127
        Ok(py.None())
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   128
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   129
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   130
    fn hasinfo(&self, py: Python<'_>) -> PyResult<bool> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   131
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   132
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   133
        let inner = unsafe { self.inner.try_borrow(py)? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   134
        Ok(inner.has_info())
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   135
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   136
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   137
    fn iscomplete(&self, py: Python<'_>) -> PyResult<bool> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   138
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   139
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   140
        let inner = unsafe { self.inner.try_borrow(py)? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   141
        Ok(inner.is_complete())
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   142
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   143
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   144
    fn stats(&self, py: Python<'_>) -> PyResult<Py<PyDict>> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   145
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   146
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   147
        let inner = unsafe { self.inner.try_borrow(py)? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   148
        let stats = inner.stats();
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   149
        let as_dict = PyDict::new(py);
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   150
        as_dict.set_item("undecided", stats.undecided)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   151
        Ok(as_dict.unbind())
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   152
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   153
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   154
    fn commonheads(&self, py: Python<'_>) -> PyResult<HashSet<PyRevision>> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   155
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   156
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   157
        let inner = unsafe { self.inner.try_borrow(py)? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   158
        let common_heads =
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   159
            inner.common_heads().map_err(GraphError::from_hg)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   160
        Ok(common_heads.into_iter().map(Into::into).collect())
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   161
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   162
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   163
    fn takefullsample(
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   164
        &mut self,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   165
        py: Python,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   166
        _headrevs: &Bound<'_, PyAny>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   167
        size: usize,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   168
    ) -> PyResult<Py<PyTuple>> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   169
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   170
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   171
        let mut inner = unsafe { self.inner.try_borrow_mut(py)? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   172
        let sample =
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   173
            inner.take_full_sample(size).map_err(GraphError::from_hg)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   174
        let as_pyrevision = sample.into_iter().map(|rev| PyRevision(rev.0));
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   175
        Ok(PyTuple::new(py, as_pyrevision)?.unbind())
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   176
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   177
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   178
    fn takequicksample(
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   179
        &mut self,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   180
        py: Python,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   181
        headrevs: &Bound<'_, PyAny>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   182
        size: usize,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   183
    ) -> PyResult<Py<PyTuple>> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   184
        let revs = self.pyiter_to_vec(headrevs)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   185
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   186
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   187
        let mut inner = unsafe { self.inner.try_borrow_mut(py)? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   188
        let sample = inner
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   189
            .take_quick_sample(revs, size)
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   190
            .map_err(GraphError::from_hg)?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   191
        let as_pyrevision = sample.into_iter().map(|rev| PyRevision(rev.0));
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   192
        Ok(PyTuple::new(py, as_pyrevision)?.unbind())
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   193
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   194
}
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   195
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   196
impl PartialDiscovery {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   197
    /// Convert a Python iterator of revisions into a vector
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   198
    fn pyiter_to_vec(
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   199
        &self,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   200
        iter: &Bound<'_, PyAny>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   201
    ) -> PyResult<Vec<Revision>> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   202
        // Safety: we don't leak any reference derived form the "faked" one in
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   203
        // `SharedByPyObject`
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   204
        let index = unsafe { self.idx.try_borrow(iter.py())? };
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   205
        rev_pyiter_collect(iter, &*index)
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   206
    }
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   207
}
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   208
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   209
pub fn init_module<'py>(
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   210
    py: Python<'py>,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   211
    package: &str,
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   212
) -> PyResult<Bound<'py, PyModule>> {
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   213
    let m = new_submodule(py, package, "discovery")?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   214
    m.add_class::<PartialDiscovery>()?;
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   215
    Ok(m)
01aff9437828 rust-pyo3: translate discovery module from hg-cpython
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
   216
}