Derive Macro PyMod

Source
#[derive(PyMod)]
Expand description

Derive macro generating an impl of __mod__ method by Rem trait.

§Expansion

This implements:

#[pymethods]
impl PyClass {
    fn __mod__(&self, other: &Self) -> <&Self as Rem<&Self>>::Output {
        Rem::rem(self, other)
    }
}

§Example

use std::ops::Rem;

use pyo3::{prelude::*, py_run};

use pyderive::PyNew;
use pyderive::ops::PyMod;

#[derive(PyNew, PyMod)]
#[pyclass(get_all)]
struct PyClass {
    field: i64
}

impl Rem for &PyClass {
    type Output = PyClass;

    fn rem(self, rhs: Self) -> Self::Output {
        PyClass { field: self.field % rhs.field }
    }
}

let test = "
actual = PyClass(7) % PyClass(2)
assert actual.field == 1
";

Python::with_gil(|py| {
    let PyClass = py.get_type::<PyClass>();
    py_run!(py, PyClass, test)
});