PyNamedTupleMake

Derive Macro PyNamedTupleMake 

Source
#[derive(PyNamedTupleMake)]
{
    // Attributes available to this derive:
    #[pyderive]
}
Expand description

Derive macro generating a _make fn/Python class method.

It constructs Self from the argument iterable, doesn’t use any other value.

  • It should place #[derive(PyNamedTupleMake)] before #[pyclass].

§Implementation Note

This is experimental. Behavior may change in future releases.

§Example

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

// Place before `#[pyclass]`
#[derive(PyNamedTupleMake)]
#[pyclass(get_all)]
struct PyClass {
    string: String,
    integer: i64,
    float: f64,
    tuple: (String, i64, f64),
}

Python::attach(|py| -> PyResult<()> {
    let Class = py.get_type::<PyClass>();

    py_run!(py, Class, r#"
a =  Class._make(['a', 1, 2.0, ('a', 1, 2.0)])

assert a.string == 'a'
assert a.integer == 1
assert a.float == 2.0
assert a.tuple == ('a', 1, 2.0)
"#);

    Ok(())
});