Details
Details
- Reviewers
kevincox - Group Reviewers
hg-reviewers - Commits
- rHG1c4b5689bef5: rust-discovery: exposing sampling to python
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
kevincox |
hg-reviewers |
Lint Skipped |
Unit Tests Skipped |
//! `mercurial.setdiscovery.partialdiscovery`. | //! `mercurial.setdiscovery.partialdiscovery`. | ||||
use crate::{ | use crate::{ | ||||
cindex::Index, | cindex::Index, | ||||
conversion::{py_set, rev_pyiter_collect}, | conversion::{py_set, rev_pyiter_collect}, | ||||
exceptions::GraphError, | exceptions::GraphError, | ||||
}; | }; | ||||
use cpython::{ | use cpython::{ | ||||
ObjectProtocol, PyDict, PyModule, PyObject, PyResult, Python, | ObjectProtocol, PyDict, PyModule, PyObject, PyResult, PyTuple, Python, | ||||
PythonObject, ToPyObject, | PythonObject, ToPyObject, | ||||
}; | }; | ||||
use hg::discovery::PartialDiscovery as CorePartialDiscovery; | use hg::discovery::PartialDiscovery as CorePartialDiscovery; | ||||
use hg::Revision; | use hg::Revision; | ||||
use std::cell::RefCell; | use std::cell::RefCell; | ||||
py_class!(pub class PartialDiscovery |py| { | py_class!(pub class PartialDiscovery |py| { | ||||
def commonheads(&self) -> PyResult<PyObject> { | def commonheads(&self) -> PyResult<PyObject> { | ||||
py_set( | py_set( | ||||
py, | py, | ||||
&self.inner(py).borrow().common_heads() | &self.inner(py).borrow().common_heads() | ||||
.map_err(|e| GraphError::pynew(py, e))? | .map_err(|e| GraphError::pynew(py, e))? | ||||
) | ) | ||||
} | } | ||||
def takefullsample(&self, _headrevs: PyObject, | |||||
size: usize) -> PyResult<PyObject> { | |||||
let mut inner = self.inner(py).borrow_mut(); | |||||
let sample = inner.take_full_sample(size) | |||||
.map_err(|e| GraphError::pynew(py, e))?; | |||||
let as_vec: Vec<PyObject> = sample | |||||
.iter() | |||||
.map(|rev| rev.to_py_object(py).into_object()) | |||||
.collect(); | |||||
Ok(PyTuple::new(py, as_vec.as_slice()).into_object()) | |||||
} | |||||
def takequicksample(&self, headrevs: PyObject, | |||||
size: usize) -> PyResult<PyObject> { | |||||
let mut inner = self.inner(py).borrow_mut(); | |||||
let revsvec: Vec<Revision> = rev_pyiter_collect(py, &headrevs)?; | |||||
let sample = inner.take_quick_sample(revsvec, size) | |||||
.map_err(|e| GraphError::pynew(py, e))?; | |||||
let as_vec: Vec<PyObject> = sample | |||||
.iter() | |||||
.map(|rev| rev.to_py_object(py).into_object()) | |||||
.collect(); | |||||
Ok(PyTuple::new(py, as_vec.as_slice()).into_object()) | |||||
} | |||||
}); | }); | ||||
/// Create the module, with __package__ given from parent | /// Create the module, with __package__ given from parent | ||||
pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { | pub fn init_module(py: Python, package: &str) -> PyResult<PyModule> { | ||||
let dotted_name = &format!("{}.discovery", package); | let dotted_name = &format!("{}.discovery", package); | ||||
let m = PyModule::new(py, dotted_name)?; | let m = PyModule::new(py, dotted_name)?; | ||||
m.add(py, "__package__", package)?; | m.add(py, "__package__", package)?; | ||||
m.add( | m.add( |