##// END OF EJS Templates
rust-dirstate: remove unneeded "ref"...
rust-dirstate: remove unneeded "ref" At 7cae6bc29ff9, .to_owned() was rewritten as .to_owned().to_vec(), which is no longer needed since the filename is a single reference.

File last commit:

r42997:30320c7b default
r43062:cc424cc1 default
Show More
dirs_multiset.rs
130 lines | 3.9 KiB | application/rls-services+xml | RustLexer
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 // dirs_multiset.rs
//
// Copyright 2019 Raphaël Gomès <rgomes@octobus.net>
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
//! Bindings for the `hg::dirstate::dirs_multiset` file provided by the
//! `hg-core` package.
use std::cell::RefCell;
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 use std::convert::TryInto;
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991
use cpython::{
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 exc, ObjectProtocol, PyBytes, PyClone, PyDict, PyErr, PyObject, PyResult,
Python,
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 };
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 use crate::{dirstate::extract_dirstate, ref_sharing::PySharedState};
Raphaël Gomès
rust-dirstate: use EntryState enum instead of literals...
r42994 use hg::{
DirsIterable, DirsMultiset, DirstateMapError, DirstateParseError,
EntryState,
};
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991
py_class!(pub class Dirs |py| {
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 data inner: RefCell<DirsMultiset>;
data py_shared_state: PySharedState;
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991
// `map` is either a `dict` or a flat iterator (usually a `set`, sometimes
// a `list`)
def __new__(
_cls,
map: PyObject,
skip: Option<PyObject> = None
) -> PyResult<Self> {
Raphaël Gomès
rust-dirstate: use EntryState enum instead of literals...
r42994 let mut skip_state: Option<EntryState> = None;
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 if let Some(skip) = skip {
Raphaël Gomès
rust-dirstate: use EntryState enum instead of literals...
r42994 skip_state = Some(
skip.extract::<PyBytes>(py)?.data(py)[0]
.try_into()
.map_err(|e: DirstateParseError| {
PyErr::new::<exc::ValueError, _>(py, e.to_string())
})?,
);
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 }
Raphaël Gomès
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop...
r42993 let inner = if let Ok(map) = map.cast_as::<PyDict>(py) {
let dirstate = extract_dirstate(py, &map)?;
DirsMultiset::new(
DirsIterable::Dirstate(&dirstate),
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 skip_state,
)
} else {
let map: Result<Vec<Vec<u8>>, PyErr> = map
.iter(py)?
.map(|o| Ok(o?.extract::<PyBytes>(py)?.data(py).to_owned()))
.collect();
Raphaël Gomès
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop...
r42993 DirsMultiset::new(
DirsIterable::Manifest(&map?),
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 skip_state,
)
Raphaël Gomès
rust-parsers: switch to parse/pack_dirstate to mutate-on-loop...
r42993 };
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 Self::create_instance(
py,
RefCell::new(inner),
PySharedState::default()
)
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 }
def addpath(&self, path: PyObject) -> PyResult<PyObject> {
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 self.borrow_mut(py)?.add_path(
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 path.extract::<PyBytes>(py)?.data(py),
);
Ok(py.None())
}
def delpath(&self, path: PyObject) -> PyResult<PyObject> {
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 self.borrow_mut(py)?.delete_path(
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 path.extract::<PyBytes>(py)?.data(py),
)
.and(Ok(py.None()))
.or_else(|e| {
match e {
DirstateMapError::PathNotFound(_p) => {
Err(PyErr::new::<exc::ValueError, _>(
py,
"expected a value, found none".to_string(),
))
}
DirstateMapError::EmptyPath => {
Ok(py.None())
}
}
})
}
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 def __iter__(&self) -> PyResult<DirsMultisetKeysIterator> {
DirsMultisetKeysIterator::create_instance(
py,
RefCell::new(Some(DirsMultisetLeakedRef::new(py, &self))),
RefCell::new(Box::new(self.leak_immutable(py)?.iter())),
)
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 }
def __contains__(&self, item: PyObject) -> PyResult<bool> {
Ok(self
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 .inner(py)
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 .borrow()
Raphaël Gomès
rust-dirstate: improve API of `DirsMultiset`...
r42995 .contains(item.extract::<PyBytes>(py)?.data(py).as_ref()))
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 }
});
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997
py_shared_ref!(Dirs, DirsMultiset, inner, DirsMultisetLeakedRef,);
impl Dirs {
pub fn from_inner(py: Python, d: DirsMultiset) -> PyResult<Self> {
Self::create_instance(py, RefCell::new(d), PySharedState::default())
}
fn translate_key(py: Python, res: &Vec<u8>) -> PyResult<Option<PyBytes>> {
Ok(Some(PyBytes::new(py, res)))
}
}
py_shared_sequence_iterator!(
DirsMultisetKeysIterator,
DirsMultisetLeakedRef,
Vec<u8>,
Dirs::translate_key,
Option<PyBytes>
);