rust/hg-cpython/src/dirstate/copymap.rs
author Yuya Nishihara <yuya@tcha.org>
Sun, 08 Sep 2019 12:23:18 +0900
changeset 42888 67853749961b
parent 42754 4e8f504424f3
child 42889 ea91a126c803
permissions -rw-r--r--
rust-cpython: replace dyn Iterator<..> of mapping with concrete type See the previous commit for why. The docstring is moved accordingly.

// 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, DirstateMapLeakedRef};
use hg::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: (&Vec<u8>, &Vec<u8>),
    ) -> PyResult<Option<PyBytes>> {
        Ok(Some(PyBytes::new(py, res.0)))
    }
    fn translate_key_value(
        py: Python,
        res: (&Vec<u8>, &Vec<u8>),
    ) -> PyResult<Option<(PyBytes, PyBytes)>> {
        let (k, v) = res;
        Ok(Some((PyBytes::new(py, k), PyBytes::new(py, v))))
    }
}

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

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