Derive Macro PyXor

Source
#[derive(PyXor)]
Expand description

Derive macro generating an impl of __xor__ method by BitXor trait.

§Expansion

This implements:

#[pymethods]
impl PyClass {
    fn __xor__(&self, other: &Self) -> <&Self as BitXor<&Self>>::Output {
        BitXor::bitxor(self, other)
    }
}

§Example

use std::ops::BitXor;

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

use pyderive::PyNew;
use pyderive::ops::PyXor;

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

impl BitXor for &PyClass {
    type Output = PyClass;

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

let test = "
actual = PyClass(7) ^ PyClass(3)
assert actual.field == 4
";

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