pub trait ToPyObject {
    type ObjectType: PythonObject;

    fn to_py_object(&self, py: Python<'_>) -> Self::ObjectType;

    fn into_py_object(self, py: Python<'_>) -> Self::ObjectType
    where
        Self: Sized
, { ... } fn with_borrowed_ptr<F, R>(&self, py: Python<'_>, f: F) -> R
    where
        F: FnOnce(*mut PyObject) -> R
, { ... } }
Expand description

Conversion trait that allows various objects to be converted into Python objects.

Note: The associated type ObjectType is used so that some Rust types convert to a more precise type of Python object. For example, [T]::to_py_object() will result in a PyList. You can always calls val.to_py_object(py).into_py_object() in order to obtain PyObject (the second into_py_object() call via the PythonObject trait corresponds to the upcast from PyList to PyObject).

Required Associated Types

Required Methods

Converts self into a Python object.

Provided Methods

Converts self into a Python object.

May be more efficient than to_py_object in some cases because it can move out of the input object.

Converts self into a Python object and calls the specified closure on the native FFI pointer underlying the Python object.

May be more efficient than to_py_object because it does not need to touch any reference counts when the input object already is a Python object.

Implementations on Foreign Types

ToPyObject for references: calls to_py_object() on the underlying T.

Option::Some<T> is converted like T. Option::None is converted to Python None.

Converts a rust bool to a Python bool.

Converts a Rust HashMap to a Python dict.

Converts a Rust BTreeMap to a Python dict.

Converts a Rust slice to a Python list.

Note: this conversion can be inefficient since a Python object is created for each element of the list. For primitive types T, consider using the buffer protocol instead.

Converts a Rust slice to a Python list.

Note: this conversion can be inefficient since a Python object is created for each element of the list. For primitive types T, consider using the buffer protocol instead.

Conversion of Rust integer to Python int.

Conversion of Rust integer to Python int.

Conversion of Rust integer to Python int.

Conversion of Rust integer to Python int.

Conversion of Rust integer to Python int.

Conversion of Rust integer to Python int. On Python 2.x, may also result in a long if the value does not fit into a Python int.

Conversion of Rust integer to Python int. On Python 2.x, may also result in a long if the value does not fit into a Python int.

Conversion of Rust integer to Python int. On Python 2.x, may also result in a long if the value does not fit into a Python int.

Conversion of Rust integer to Python int. On Python 2.x, may also result in a long if the value does not fit into a Python int.

Conversion of Rust integer to Python int. On Python 2.x, may also result in a long if the value does not fit into a Python int.

Conversion of Rust f64 to Python float.

Conversion of Rust f32 to Python float.

Converts Rust str to Python object.

On Python 2.7, this impl will create a byte string if the input string is ASCII-only; and a unicode string otherwise. Use PyUnicode::new() to always create a unicode string.

On Python 3.x, this function always creates unicode str objects.

Converts Rust str to Python object.

On Python 2.7, this impl will create a byte string if the input string is ASCII-only; and a unicode string otherwise. Use PyUnicode::new() to always create a unicode string.

On Python 3.x, this function always creates unicode str objects.

Converts Rust str to Python object.

On Python 2.7, this impl will create a byte string if the input string is ASCII-only; and a unicode string otherwise. Use PyUnicode::new() to always create a unicode string.

On Python 3.x, this function always creates unicode str objects.

Converts a Rust tuple to a Python tuple.

Converts a Rust tuple to a Python tuple.

Converts a Rust tuple to a Python tuple.

Converts a Rust tuple to a Python tuple.

Converts a Rust tuple to a Python tuple.

Converts a Rust tuple to a Python tuple.

Converts a Rust tuple to a Python tuple.

Converts a Rust tuple to a Python tuple.

Converts a Rust tuple to a Python tuple.

Implementors

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Converts NoArgs to an empty Python tuple.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.