Trait cpython::ExtractPyObject [] [src]

pub trait ExtractPyObject<'prepared>: Sized {
    type Prepared: 'static;
    fn prepare_extract<'a, 'p>(py: Python<'p>, obj: &'a PyObject) -> PyResult<Self::Prepared>;
    fn extract<'p>(py: Python<'p>, prepared: &'prepared Self::Prepared) -> PyResult<Self>;
}

FromPyObject is implemented by various types that can be extracted from a Python object.

Usage: let obj: PyObject = ...; let prepared = <TargetType as ExtractPyObject>::prepare_extract(&obj); let extracted = try!(extract(&prepared));

Note: depending on the implementation, the lifetime of the extracted result may depend on the lifetime of the obj or the prepared variable.

For example, when extracting &str from a python byte string, the resulting string slice will point to the existing string data (lifetime: 'source). On the other hand, when extracting &str from a python unicode string, the preparation step will convert the string to UTF-8, and the resulting string slice will have lifetime 'prepared. Since only which of these cases applies depends on the runtime type of the python object, both the obj and prepared variables must outlive the resulting string slice.

In cases where the result does not depend on the 'prepared lifetime, the inherent method PyObject::extract() can be used.

Associated Types

type Prepared: 'static

Required Methods

fn prepare_extract<'a, 'p>(py: Python<'p>, obj: &'a PyObject) -> PyResult<Self::Prepared>

fn extract<'p>(py: Python<'p>, prepared: &'prepared Self::Prepared) -> PyResult<Self>

Implementors