Module rustypy::pytypes::pydict [] [src]

An analog of a Python dict which contains pairs of (key, values) each of a single type, will accept an undefined number of one (and just one) of any other supported type (including other PyDict) as value and a corresponding key of a single supported hashable type (check PyDictK to see which ones are supported).

PyDict can be constructed from other iterable types as long as the inner type is supported (a copy will be performed in case is necessary).

use std::iter::FromIterator;
let mut hm = HashMap::from_iter(vec![(0u32, "Hi"), (1, "Rust")]);
PyDict::from_iter(hm.clone().into_iter());
PyDict::from(hm);

You can also use the typical hashmap interfaces (insert, get, remove, etc.) as long as the type is supported (check PyArg variants). PyDict types and their content can be converted back to a HashMap or into a (K, V) iterator.

use std::iter::FromIterator;
let hm = HashMap::from_iter(vec![(0u32, "Hello"), (1, "from"), (3, "Rust")]);

let ptr = PyDict::from(hm).as_ptr();
// to get a PyDict from a raw pointer we need to provide the key type:
let d = unsafe { PyDict::<u32>::from_ptr(ptr) };
let hm_from_pd = PyDict::into_hashmap::<String>(d.clone());
assert_eq!(hm_from_pd.get(&0).unwrap(), "Hello");

let hm_from_iter: HashMap<u32, String> = HashMap::from_iter(d.into_iter::<String>());
assert_eq!(hm_from_pd.get(&3).unwrap(), "Rust");

When extracting in Python with the FFI, elements are moved, not copied and when free'd all the original elements are dropped.

Safety

PyList must be passed between Rust and Python as a size_t raw pointer. You can get a raw pointer using as_ptr and convert from a raw pointer using the "static" method PyDict::from_ptr which is unsafe as it requires dereferencing a raw pointer. PyDict also require providing the key type, in case the key type is not the expected one undefined behaviour will happen.

For convinience there are some methods to perform conversions to HashMap<K,V> from PyDict<K,PyArg>, while none of those are unsafe per se, they require providing the expected PyArg enum variant. In case the expected variant is wrong, the process will abort and exit as it's not possible to handle errors acrosss the FFI boundary.

Unpacking PyDict from Python

Is recommended to use the unpack_pydict! macro in order to convert a PyDict to a Rust native type. Check the macro documentation for more info.

Structs

IntoIter
PyDict

Enums

PyDictK

Types allowed as PyDict key values.

Functions

pydict_get_drain
pydict_insert
pydict_new