Macro rigetti_pyo3::py_wrap_type

source ·
macro_rules! py_wrap_type {
    (
        $(#[$meta: meta])*
        $name: ident($from: ty)$(as $py_alias: literal)?$(;)?
    ) => { ... };
}
Expand description

Create a Python wrapper around a Rust type.

You probably do not want to call this directly, as other macros build on top of this.

Implements:

  • Conversion between wrapper and inner Rust type
  • AsRef to access the inner Rust type from pyo3 code.
  • PyWrapper as non-generic aliases for the above
  • ToPyObject

§Macro inputs:

  • $meta: Any attributes to apply to the wrapper type. Supports #[pyo3(...)] for configuring the Python type.
  • $name: The Rust name for the wrapper type (usually PySomething).
  • $from: The Rust type to wrap.
  • $py_alias (optional): The type name to expose to Python (usually $name without a leading Py).
use std::collections::HashMap;
use rigetti_pyo3::py_wrap_type;

py_wrap_type! {
    #[derive(Debug)]
    PyNumberLabels(HashMap<String, i32>) as "NumberLabels";
}

let map = HashMap::new();
let dict = PyNumberLabels::from(map);
let map = HashMap::from(dict);
let dict = PyNumberLabels::from(&map);
assert_eq!(&map, dict.as_ref());