Derive Macro PyAnd

Source
#[derive(PyAnd)]
Expand description

Derive macro generating an impl of __and__ method by BitAnd trait.

§Expansion

This implements:

#[pymethods]
impl PyClass {
    fn __and__(&self, other: &Self) -> <&Self as BitAnd<&Self>>::Output {
        BitAnd::bitand(self, other)
    }
}

§Example

use std::ops::BitAnd;

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

use pyderive::PyNew;
use pyderive::ops::PyAnd;

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

impl BitAnd for &PyClass {
    type Output = PyClass;

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

let test = "
actual = PyClass(9) & PyClass(5)
assert actual.field == 1
";

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