[][src]Macro rustypy::unpack_pydict

macro_rules! unpack_pydict {
    ( $pydict:ident; PyDict{($kt:ty, $o:tt { $($t:tt)* })} ) => { ... };
    ( $pytuple:ident; PyTuple { $t:tt } ) => { ... };
    ( $pylist:ident; PyList{ $($u:tt)* } ) => { ... };
    ( $pydict:ident; PyDict{($kt:ty, $t:tt => $type_:ty)} ) => { ... };
}

Consumes the content of a PyDict<K, T> as received from Python (raw pointer) and returns a HashMap<K, T> from it, no copies are performed in the process.

All inner elements are moved out if possible, if not (like with PyTuples) are copied. PyTuple variants are destructured into Rust tuples which contain the appropiate Rust types (valid syntax for unpack_pytuple! macro must be provided). The same happens with other container types (inner PyList, PyDict, etc.).

Examples

A simple PyDict with i32 keys which contains PyString types::

use rustypy::{PyDict, PyString};
use std::collections::HashMap;

let mut hm = HashMap::from_iter(vec![(0_i32, "Hello"), (1_i32, " "), (2_i32, "World!")]);
let dict = PyDict::from(hm).into_raw();
let unpacked = unpack_pydict!(dict; PyDict{(i32, PyString => String)});

With nested container types:

// dict from Python: {str: ([u64], {i32: str})} as *mut size_t
let unpacked = unpack_pydict!(dict;
    PyDict{(PyString, PyTuple{({PyList{I64 => i64}},
                               {PyDict{(i32, PyString => String)}},)})}
    );