Module rustypy::pytypes::pylist [] [src]

An analog of a Python list which contains elements of a single type, will accept an undefined number of one (and just one) of any other supported type (including other PyLists).

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

PyList::from_iter(vec![1u32; 3]);
PyList::from(vec![1u32; 3]);

You can also use the typical vector interfaces (push, pop, remove, etc.) as long as the type is supported (check PyArg variants). PyList types and their content can also be implicitly converted through an special iterator type.

let mut l = PyList::new();
for e in vec![0u32, 1, 3] {
    l.push(e);
}

let mut iter = PyList::into_iter::<u32>(l);
assert_eq!(iter.collect::<Vec<u32>>(), vec![0u32, 1, 3])

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

Safety

PyList must be passed between Rust and Python as a raw pointer. You can get a raw pointer using as_ptr and convert from a raw pointer using the "static" method PyList::from_ptr which is unsafe as it requires dereferencing a raw pointer.

For convinience there are some methods to perform conversions to Vec from PyList, 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 PyList from Python

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

Structs

IntoIter
PyList

An analog of a Python list which contains an undefined number of elements of a single kind, of any supported type.