rust/hg-cpython/src/dirstate/non_normal_entries.rs
author Simon Sapin <simon.sapin@octobus.net>
Fri, 30 Apr 2021 19:57:46 +0200
changeset 47124 cd8ca38fccff
parent 47094 e061a1df32a8
child 47335 ed1583a845d2
permissions -rw-r--r--
rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs Getting the former (through `Deref`) is almost the only useful thing one can do with the latter anyway. With this changes, API become more flexible for the "provider" of these paths which may store something else that Deref’s to HgPath, such as `std::borrow::Cow<HgPath>`. Using `Cow` can help reduce memory alloactions and copying. Differential Revision: https://phab.mercurial-scm.org/D10558
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     1
// non_normal_other_parent_entries.rs
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     2
//
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     3
// Copyright 2020 Raphaël Gomès <rgomes@octobus.net>
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     4
//
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     5
// This software may be used and distributed according to the terms of the
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     6
// GNU General Public License version 2 or any later version.
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     7
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
     8
use cpython::{
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
     9
    exc::NotImplementedError, CompareOp, ObjectProtocol, PyBytes, PyClone,
47094
e061a1df32a8 dirstate-tree: Abstract "non-normal" and "other parent" sets
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
    10
    PyErr, PyObject, PyResult, PyString, Python, PythonObject, ToPyObject,
e061a1df32a8 dirstate-tree: Abstract "non-normal" and "other parent" sets
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
    11
    UnsafePyLeaked,
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    12
};
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    13
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    14
use crate::dirstate::DirstateMap;
47124
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47094
diff changeset
    15
use hg::utils::hg_path::HgPath;
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    16
use std::cell::RefCell;
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    17
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    18
py_class!(pub class NonNormalEntries |py| {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    19
    data dmap: DirstateMap;
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    20
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    21
    def __contains__(&self, key: PyObject) -> PyResult<bool> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    22
        self.dmap(py).non_normal_entries_contains(py, key)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    23
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    24
    def remove(&self, key: PyObject) -> PyResult<PyObject> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    25
        self.dmap(py).non_normal_entries_remove(py, key)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    26
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    27
    def __richcmp__(&self, other: PyObject, op: CompareOp) -> PyResult<bool> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    28
        match op {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    29
            CompareOp::Eq => self.is_equal_to(py, other),
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    30
            CompareOp::Ne => Ok(!self.is_equal_to(py, other)?),
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    31
            _ => Err(PyErr::new::<NotImplementedError, _>(py, ""))
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    32
        }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    33
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    34
    def __repr__(&self) -> PyResult<PyString> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    35
        self.dmap(py).non_normal_entries_display(py)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    36
    }
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    37
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    38
    def __iter__(&self) -> PyResult<NonNormalEntriesIterator> {
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    39
        self.dmap(py).non_normal_entries_iter(py)
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    40
    }
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    41
});
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    42
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    43
impl NonNormalEntries {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    44
    pub fn from_inner(py: Python, dm: DirstateMap) -> PyResult<Self> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    45
        Self::create_instance(py, dm)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    46
    }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    47
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    48
    fn is_equal_to(&self, py: Python, other: PyObject) -> PyResult<bool> {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    49
        for item in other.iter(py)? {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    50
            if !self.dmap(py).non_normal_entries_contains(py, item?)? {
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    51
                return Ok(false);
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    52
            }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    53
        }
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    54
        Ok(true)
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    55
    }
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    56
47124
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47094
diff changeset
    57
    fn translate_key(py: Python, key: &HgPath) -> PyResult<Option<PyBytes>> {
44973
26114bd6ec60 rust: do a clippy pass
Raphaël Gomès <rgomes@octobus.net>
parents: 44416
diff changeset
    58
        Ok(Some(PyBytes::new(py, key.as_bytes())))
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    59
    }
44297
cf1f8660e568 rust-dirstatemap: add `NonNormalEntries` class
Raphaël Gomès <rgomes@octobus.net>
parents:
diff changeset
    60
}
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    61
47094
e061a1df32a8 dirstate-tree: Abstract "non-normal" and "other parent" sets
Simon Sapin <simon.sapin@octobus.net>
parents: 44973
diff changeset
    62
type NonNormalEntriesIter<'a> =
47124
cd8ca38fccff rust: Use `&HgPath` instead of `&HgPathBuf` in may APIs
Simon Sapin <simon.sapin@octobus.net>
parents: 47094
diff changeset
    63
    Box<dyn Iterator<Item = &'a HgPath> + Send + 'a>;
44416
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    64
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    65
py_shared_iterator!(
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    66
    NonNormalEntriesIterator,
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    67
    UnsafePyLeaked<NonNormalEntriesIter<'static>>,
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    68
    NonNormalEntries::translate_key,
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    69
    Option<PyBytes>
8ac5726d695d rust-cpython: make `NonNormalEntires` iterable to fix `fsmonitor` (issue6276)
Raphaël Gomès <rgomes@octobus.net>
parents: 44297
diff changeset
    70
);