##// END OF EJS Templates
rust-dirstate: improve API of `DirsMultiset`...
Raphaël Gomès -
r42977:4f9dff6f default draft
parent child Browse files
Show More
@@ -12,7 +12,7 b' use crate::{'
12 dirstate::EntryState, utils::files, DirsIterable, DirstateEntry,
12 dirstate::EntryState, utils::files, DirsIterable, DirstateEntry,
13 DirstateMapError,
13 DirstateMapError,
14 };
14 };
15 use std::collections::hash_map::{Entry, Iter};
15 use std::collections::hash_map::Entry;
16 use std::collections::HashMap;
16 use std::collections::HashMap;
17
17
18 #[derive(PartialEq, Debug)]
18 #[derive(PartialEq, Debug)]
@@ -98,12 +98,12 b' impl DirsMultiset {'
98 Ok(())
98 Ok(())
99 }
99 }
100
100
101 pub fn contains_key(&self, key: &[u8]) -> bool {
101 pub fn contains(&self, key: &[u8]) -> bool {
102 self.inner.contains_key(key)
102 self.inner.contains_key(key)
103 }
103 }
104
104
105 pub fn iter(&self) -> Iter<Vec<u8>, u32> {
105 pub fn iter(&self) -> impl Iterator<Item = &Vec<u8>> {
106 self.inner.iter()
106 self.inner.keys()
107 }
107 }
108
108
109 pub fn len(&self) -> usize {
109 pub fn len(&self) -> usize {
@@ -12,7 +12,7 b' use std::cell::RefCell;'
12
12
13 use cpython::{
13 use cpython::{
14 exc, ObjectProtocol, PyBytes, PyDict, PyErr, PyObject, PyResult,
14 exc, ObjectProtocol, PyBytes, PyDict, PyErr, PyObject, PyResult,
15 ToPyObject,
15 PythonObject, ToPyObject,
16 };
16 };
17
17
18 use crate::dirstate::extract_dirstate;
18 use crate::dirstate::extract_dirstate;
@@ -93,26 +93,21 b' py_class!(pub class Dirs |py| {'
93 // of having it work to continue working on the rest of the module
93 // of having it work to continue working on the rest of the module
94 // hopefully bypassing Python entirely pretty soon.
94 // hopefully bypassing Python entirely pretty soon.
95 def __iter__(&self) -> PyResult<PyObject> {
95 def __iter__(&self) -> PyResult<PyObject> {
96 let dict = PyDict::new(py);
96 let dirs = self.dirs_map(py).borrow();
97
97 let dirs: Vec<_> = dirs
98 for (key, value) in self.dirs_map(py).borrow().iter() {
98 .iter()
99 dict.set_item(
99 .map(|d| PyBytes::new(py, d))
100 py,
100 .collect();
101 PyBytes::new(py, &key[..]),
101 dirs.to_py_object(py)
102 value.to_py_object(py),
102 .into_object()
103 )?;
103 .iter(py)
104 }
104 .map(|o| o.into_object())
105
106 let locals = PyDict::new(py);
107 locals.set_item(py, "obj", dict)?;
108
109 py.eval("iter(obj)", None, Some(&locals))
110 }
105 }
111
106
112 def __contains__(&self, item: PyObject) -> PyResult<bool> {
107 def __contains__(&self, item: PyObject) -> PyResult<bool> {
113 Ok(self
108 Ok(self
114 .dirs_map(py)
109 .dirs_map(py)
115 .borrow()
110 .borrow()
116 .contains_key(item.extract::<PyBytes>(py)?.data(py).as_ref()))
111 .contains(item.extract::<PyBytes>(py)?.data(py).as_ref()))
117 }
112 }
118 });
113 });
General Comments 0
You need to be logged in to leave comments. Login now