##// END OF EJS Templates
rust-cpython: make PyLeakedRef operations relatively safe...
rust-cpython: make PyLeakedRef operations relatively safe This patch encapsulates the access to the leaked reference to make most leaked-ref operations safe. The only exception is leaked_ref.map(). I couldn't figure out how to allow arbitrary map operation safely over an unsafe static reference. See the docstring and inline comment for details. Now leak_immutable() can be safely implemented as the PyLeakedRef owns its inner data.

File last commit:

r43579:ffc1fbd7 default
r43579:ffc1fbd7 default
Show More
dirs_multiset.rs
129 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 };
Yuya Nishihara
rust-cpython: move $leaked struct out of macro...
r43447 use crate::dirstate::extract_dirstate;
use crate::ref_sharing::{PyLeakedRef, PySharedRefCell};
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of sequence with concrete type...
r43157 use hg::{
Raphaël Gomès
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf...
r43227 utils::hg_path::{HgPath, HgPathBuf},
Yuya Nishihara
rust-cpython: replace dyn Iterator<..> of sequence with concrete type...
r43157 DirsMultiset, DirsMultisetIter, DirstateMapError, DirstateParseError,
EntryState,
};
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991
py_class!(pub class Dirs |py| {
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 data inner: PySharedRefCell<DirsMultiset>;
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)?;
Yuya Nishihara
rust-dirstate: split DirsMultiset constructor per input type...
r43070 DirsMultiset::from_dirstate(&dirstate, skip_state)
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 } else {
Raphaël Gomès
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf...
r43227 let map: Result<Vec<HgPathBuf>, PyErr> = map
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 .iter(py)?
Raphaël Gomès
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf...
r43227 .map(|o| {
Ok(HgPathBuf::from_bytes(
o?.extract::<PyBytes>(py)?.data(py),
))
})
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 .collect();
Yuya Nishihara
rust-dirstate: split DirsMultiset constructor per input type...
r43070 DirsMultiset::from_manifest(&map?)
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,
Yuya Nishihara
rust-cpython: introduce restricted variant of RefCell...
r43115 PySharedRefCell::new(inner),
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 )
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 }
def addpath(&self, path: PyObject) -> PyResult<PyObject> {
Yuya Nishihara
rust-cpython: drop self.borrow_mut() in favor of PySharedRef wrapper
r43450 self.inner_shared(py).borrow_mut()?.add_path(
Raphaël Gomès
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf...
r43227 HgPath::new(path.extract::<PyBytes>(py)?.data(py)),
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 );
Ok(py.None())
}
def delpath(&self, path: PyObject) -> PyResult<PyObject> {
Yuya Nishihara
rust-cpython: drop self.borrow_mut() in favor of PySharedRef wrapper
r43450 self.inner_shared(py).borrow_mut()?.delete_path(
Raphaël Gomès
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf...
r43227 HgPath::new(path.extract::<PyBytes>(py)?.data(py)),
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 )
.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> {
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 let leaked_ref = self.inner_shared(py).leak_immutable()?;
Yuya Nishihara
rust-cpython: leverage py_shared_iterator::from_inner() where appropriate
r43161 DirsMultisetKeysIterator::from_inner(
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 py,
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 unsafe { leaked_ref.map(py, |o| o.iter()) },
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 )
Raphaël Gomès
rust-dirstate: create dirstate submodule in hg-cpython...
r42991 }
def __contains__(&self, item: PyObject) -> PyResult<bool> {
Raphaël Gomès
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf...
r43227 Ok(self.inner(py).borrow().contains(HgPath::new(
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
Yuya Nishihara
rust-cpython: add safe wrapper representing shared data borrowed from PyObject...
r43448 py_shared_ref!(Dirs, DirsMultiset, inner, inner_shared);
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997
impl Dirs {
pub fn from_inner(py: Python, d: DirsMultiset) -> PyResult<Self> {
Yuya Nishihara
rust-cpython: move py_shared_state to PySharedRefCell object...
r43443 Self::create_instance(py, PySharedRefCell::new(d))
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 }
Raphaël Gomès
rust-hgpath: replace all paths and filenames with HgPath/HgPathBuf...
r43227 fn translate_key(
py: Python,
res: &HgPathBuf,
) -> PyResult<Option<PyBytes>> {
Ok(Some(PyBytes::new(py, res.as_ref())))
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 }
}
Yuya Nishihara
rust-cpython: rename py_shared_iterator_impl to py_shared_iterator...
r43159 py_shared_iterator!(
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 DirsMultisetKeysIterator,
Yuya Nishihara
rust-cpython: make PyLeakedRef operations relatively safe...
r43579 PyLeakedRef<DirsMultisetIter<'static>>,
Raphaël Gomès
rust-cpython: add macro for sharing references...
r42997 Dirs::translate_key,
Option<PyBytes>
);