Macro kgdata::pylist

source ·
macro_rules! pylist {
    ($structname:ident (module = $module:literal, item = $item:ty as $itemview:ty $(, derive = ($( $derivetrait:ident ),*) )? )) => { ... };
}
Expand description

Generating a temporary iterator over an array that allow random access at O(1) cost once the iterator is still valid.

§Placeholder

  • structname: name of the generated struct
  • module: name of the python module that will contains this struct
  • derivetrait: optional, list of traits to derive for the struct
  • itemview: the item view type (a pyo3 class) – expect to have a constructor new(&item)
  • item: the item type (rust struct)

§Example

use kgdata::pyo3helper::PyStr;

pylist!(ListStrView (module = "kgdata.core.test", derive = (Clone, Debug)) {
    String: PyStr,
});

The above macro will generate the following code:

pub mod ListStrView {
    use pyo3::prelude::*;
    use kgdata::pyo3helper::PyStr;

    #[pyclass(module = "kgdata.core.test", name = "ListView")]
    #[derive(Clone, Debug)]
    pub struct ListView(pub &'static [String]);

    #[pyclass]
    pub struct IterView(pub std::slice::Iter<'static, String>);

    #[pymethods]
    impl ListView {
        fn new(lst: &[String]) -> Self;
        fn __len__(&self) -> usize;
        fn __iter__(&self) -> IterView;
        fn __getitem__(&self, i: usize) -> PyResult<PyStr>;
    }

    #[pymethods]
    impl IterView {
        fn __iter__(&self) -> Self;
        fn __next__(&mut self) -> Option<PyStr>;
    }

}