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
AsRefto access the inner Rust type frompyo3code.PyWrapperas non-generic aliases for the aboveToPyObject
ยง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 (usuallyPySomething).$from: The Rust type to wrap.$py_alias(optional): The type name to expose to Python (usually$namewithout a leadingPy).
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());