2019-10-25 10:12:05 +00:00
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2020-07-04 15:55:26 +00:00
|
|
|
use pyo3::exceptions::PyKeyError;
|
2019-10-25 10:12:05 +00:00
|
|
|
use pyo3::prelude::*;
|
|
|
|
use pyo3::types::IntoPyDict;
|
|
|
|
use pyo3::types::PyList;
|
|
|
|
use pyo3::PyMappingProtocol;
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct Mapping {
|
|
|
|
index: HashMap<String, usize>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl Mapping {
|
|
|
|
#[new]
|
2019-12-14 14:16:39 +00:00
|
|
|
fn new(elements: Option<&PyList>) -> PyResult<Self> {
|
2019-10-25 10:12:05 +00:00
|
|
|
if let Some(pylist) = elements {
|
|
|
|
let mut elems = HashMap::with_capacity(pylist.len());
|
|
|
|
for (i, pyelem) in pylist.into_iter().enumerate() {
|
|
|
|
let elem = String::extract(pyelem)?;
|
|
|
|
elems.insert(elem, i);
|
|
|
|
}
|
2019-12-14 14:16:39 +00:00
|
|
|
Ok(Self { index: elems })
|
2019-10-25 10:12:05 +00:00
|
|
|
} else {
|
2019-12-14 14:16:39 +00:00
|
|
|
Ok(Self {
|
2019-10-25 10:12:05 +00:00
|
|
|
index: HashMap::new(),
|
2019-12-14 14:16:39 +00:00
|
|
|
})
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyproto]
|
|
|
|
impl PyMappingProtocol for Mapping {
|
2020-06-23 10:40:17 +00:00
|
|
|
fn __len__(&self) -> usize {
|
|
|
|
self.index.len()
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn __getitem__(&self, query: String) -> PyResult<usize> {
|
|
|
|
self.index
|
|
|
|
.get(&query)
|
|
|
|
.copied()
|
2020-07-04 15:55:26 +00:00
|
|
|
.ok_or_else(|| PyKeyError::py_err("unknown key"))
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
|
|
|
|
2020-06-23 10:40:17 +00:00
|
|
|
fn __setitem__(&mut self, key: String, value: usize) {
|
2019-10-25 10:12:05 +00:00
|
|
|
self.index.insert(key, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __delitem__(&mut self, key: String) -> PyResult<()> {
|
|
|
|
if self.index.remove(&key).is_none() {
|
2020-07-04 15:55:26 +00:00
|
|
|
PyKeyError::py_err("unknown key").into()
|
2019-10-25 10:12:05 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// not an actual reversed implementation, just to demonstrate that the method is callable.
|
2020-06-23 10:40:17 +00:00
|
|
|
fn __reversed__(&self) -> PyObject {
|
2019-10-25 10:12:05 +00:00
|
|
|
let gil = Python::acquire_gil();
|
2020-06-23 10:40:17 +00:00
|
|
|
self.index
|
2019-10-25 10:12:05 +00:00
|
|
|
.keys()
|
|
|
|
.cloned()
|
|
|
|
.collect::<Vec<String>>()
|
2020-06-23 10:40:17 +00:00
|
|
|
.into_py(gil.python())
|
2019-10-25 10:12:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_getitem() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let d = [("Mapping", py.get_type::<Mapping>())].into_py_dict(py);
|
|
|
|
|
|
|
|
let run = |code| py.run(code, None, Some(d)).unwrap();
|
|
|
|
let err = |code| py.run(code, None, Some(d)).unwrap_err();
|
|
|
|
|
|
|
|
run("m = Mapping(['1', '2', '3']); assert m['1'] == 0");
|
|
|
|
run("m = Mapping(['1', '2', '3']); assert m['2'] == 1");
|
|
|
|
run("m = Mapping(['1', '2', '3']); assert m['3'] == 2");
|
|
|
|
err("m = Mapping(['1', '2', '3']); print(m['4'])");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_setitem() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let d = [("Mapping", py.get_type::<Mapping>())].into_py_dict(py);
|
|
|
|
|
|
|
|
let run = |code| py.run(code, None, Some(d)).unwrap();
|
|
|
|
let err = |code| py.run(code, None, Some(d)).unwrap_err();
|
|
|
|
|
|
|
|
run("m = Mapping(['1', '2', '3']); m['1'] = 4; assert m['1'] == 4");
|
|
|
|
run("m = Mapping(['1', '2', '3']); m['0'] = 0; assert m['0'] == 0");
|
|
|
|
run("m = Mapping(['1', '2', '3']); len(m) == 4");
|
|
|
|
err("m = Mapping(['1', '2', '3']); m[0] = 'hello'");
|
|
|
|
err("m = Mapping(['1', '2', '3']); m[0] = -1");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_delitem() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
let d = [("Mapping", py.get_type::<Mapping>())].into_py_dict(py);
|
|
|
|
let run = |code| py.run(code, None, Some(d)).unwrap();
|
|
|
|
let err = |code| py.run(code, None, Some(d)).unwrap_err();
|
|
|
|
|
|
|
|
run(
|
|
|
|
"m = Mapping(['1', '2', '3']); del m['1']; assert len(m) == 2; \
|
|
|
|
assert m['2'] == 1; assert m['3'] == 2",
|
|
|
|
);
|
|
|
|
err("m = Mapping(['1', '2', '3']); del m[-1]");
|
|
|
|
err("m = Mapping(['1', '2', '3']); del m['4']");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_reversed() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
|
|
|
|
let d = [("Mapping", py.get_type::<Mapping>())].into_py_dict(py);
|
|
|
|
let run = |code| py.run(code, None, Some(d)).unwrap();
|
|
|
|
|
|
|
|
run("m = Mapping(['1', '2']); assert set(reversed(m)) == {'1', '2'}");
|
|
|
|
}
|