Below code fails with error message "cannot find attribute getter
in this scope".
Why is 'getter' not in scope?
use std::collections::HashMap;
use std::str;
use pyo3::prelude::*;
#[macro_export]
macro_rules! create_map_getter {
($(#[$m:meta])* $key:ident, $class:ident) => {
$(#[$m])*
fn $key(slf: PyRef<Self>) -> PyResult<HashMap<String, $class>> {
Ok(slf
.0
.$key
.iter()
.map(|(k, v)| (str::from_utf8(k).unwrap().to_owned(), $class(v.clone())))
.collect::<HashMap<String, $class>>())
}
};
}
#[derive(Debug, Clone)]
pub struct Value {
pub foo: u32
}
#[derive(Debug, Clone)]
pub struct Data {
pub foo: HashMap<Vec<u8>, Value>
}
#[pyclass]
#[derive(Debug, Clone)]
pub struct PyValue(Value);
#[pyclass]
#[derive(Debug, Clone)]
pub struct PyData(Data);
#[pymethods]
impl PyData {
create_map_getter!(
#[getter]
foo,
PyValue
);
}
fn main() {
println!("Hello, world!");
}