view rust/hg-cpython/src/dirstate/copymap.rs @ 43422:b9f791090211

rust-cpython: rename PyLeakedRef to PyLeaked This series will make PyLeaked* behave more like a Python iterator, which means mutation of the owner object will be allowed and the leaked reference (i.e. the iterator) will be invalidated instead. I'll add PyLeakedRef/PyLeakedRefMut structs which will represent a "borrowed" state, and prevent the underlying value from being mutably borrowed while the leaked reference is in use: let shared = self.inner_shared(py); let leaked = shared.leak_immutable(); { let leaked_ref: PyLeakedRef<_> = leaked.borrow(py); shared.borrow_mut(); // panics since the underlying value is borrowed } shared.borrow_mut(); // allowed The relation between PyLeaked* structs is quite similar to RefCell/Ref/RefMut, but the implementation can't be reused because the borrowing state will have to be shared across objects having no lifetime relation. PyLeaked isn't named as PyLeakedCell since it isn't actually a cell in that leaked.borrow_mut() will require &mut self.
author Yuya Nishihara <yuya@tcha.org>
date Sat, 12 Oct 2019 19:10:51 +0900
parents ffc1fbd7d1f5
children bad4e7b361d2
line wrap: on
line source

// copymap.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 `hg::dirstate::dirstate_map::CopyMap` provided by the
//! `hg-core` package.

use cpython::{PyBytes, PyClone, PyDict, PyObject, PyResult, Python};
use std::cell::RefCell;

use crate::dirstate::dirstate_map::DirstateMap;
use crate::ref_sharing::PyLeaked;
use hg::{utils::hg_path::HgPathBuf, CopyMapIter};

py_class!(pub class CopyMap |py| {
    data dirstate_map: DirstateMap;

    def __getitem__(&self, key: PyObject) -> PyResult<PyBytes> {
        (*self.dirstate_map(py)).copymapgetitem(py, key)
    }

    def __len__(&self) -> PyResult<usize> {
        self.dirstate_map(py).copymaplen(py)
    }

    def __contains__(&self, key: PyObject) -> PyResult<bool> {
        self.dirstate_map(py).copymapcontains(py, key)
    }

    def get(
        &self,
        key: PyObject,
        default: Option<PyObject> = None
    ) -> PyResult<Option<PyObject>> {
        self.dirstate_map(py).copymapget(py, key, default)
    }

    def pop(
        &self,
        key: PyObject,
        default: Option<PyObject> = None
    ) -> PyResult<Option<PyObject>> {
        self.dirstate_map(py).copymappop(py, key, default)
    }

    def __iter__(&self) -> PyResult<CopyMapKeysIterator> {
        self.dirstate_map(py).copymapiter(py)
    }

    // Python's `dict()` builtin works with either a subclass of dict
    // or an abstract mapping. Said mapping needs to implement `__getitem__`
    // and `keys`.
    def keys(&self) -> PyResult<CopyMapKeysIterator> {
        self.dirstate_map(py).copymapiter(py)
    }

    def items(&self) -> PyResult<CopyMapItemsIterator> {
        self.dirstate_map(py).copymapitemsiter(py)
    }

    def iteritems(&self) -> PyResult<CopyMapItemsIterator> {
        self.dirstate_map(py).copymapitemsiter(py)
    }

    def __setitem__(
        &self,
        key: PyObject,
        item: PyObject
    ) -> PyResult<()> {
        self.dirstate_map(py).copymapsetitem(py, key, item)?;
        Ok(())
    }

    def copy(&self) -> PyResult<PyDict> {
        self.dirstate_map(py).copymapcopy(py)
    }

});

impl CopyMap {
    pub fn from_inner(py: Python, dm: DirstateMap) -> PyResult<Self> {
        Self::create_instance(py, dm)
    }
    fn translate_key(
        py: Python,
        res: (&HgPathBuf, &HgPathBuf),
    ) -> PyResult<Option<PyBytes>> {
        Ok(Some(PyBytes::new(py, res.0.as_ref())))
    }
    fn translate_key_value(
        py: Python,
        res: (&HgPathBuf, &HgPathBuf),
    ) -> PyResult<Option<(PyBytes, PyBytes)>> {
        let (k, v) = res;
        Ok(Some((
            PyBytes::new(py, k.as_ref()),
            PyBytes::new(py, v.as_ref()),
        )))
    }
}

py_shared_iterator!(
    CopyMapKeysIterator,
    PyLeaked<CopyMapIter<'static>>,
    CopyMap::translate_key,
    Option<PyBytes>
);

py_shared_iterator!(
    CopyMapItemsIterator,
    PyLeaked<CopyMapIter<'static>>,
    CopyMap::translate_key_value,
    Option<(PyBytes, PyBytes)>
);