Trait cpython::ToPyObject[][src]

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
, { ... } }

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).

Associated Types

type ObjectType: PythonObject[src]

Loading content...

Required methods

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

Converts self into a Python object.

Loading content...

Provided methods

fn into_py_object(self, py: Python<'_>) -> Self::ObjectType where
    Self: Sized
[src]

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.

fn with_borrowed_ptr<F, R>(&self, py: Python<'_>, f: F) -> R where
    F: FnOnce(*mut PyObject) -> R, 
[src]

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.

Loading content...

Implementations on Foreign Types

impl<'a, T: ?Sized> ToPyObject for &'a T where
    T: ToPyObject
[src]

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

type ObjectType = T::ObjectType

impl<T> ToPyObject for Option<T> where
    T: ToPyObject
[src]

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

type ObjectType = PyObject

impl ToPyObject for bool[src]

Converts a rust bool to a Python bool.

type ObjectType = PyBool

impl<K, V, H> ToPyObject for HashMap<K, V, H> where
    K: Hash + Eq + ToPyObject,
    V: ToPyObject,
    H: BuildHasher
[src]

Converts a Rust HashMap to a Python dict.

type ObjectType = PyDict

impl<K, V> ToPyObject for BTreeMap<K, V> where
    K: Eq + ToPyObject,
    V: ToPyObject
[src]

Converts a Rust BTreeMap to a Python dict.

type ObjectType = PyDict

impl<T> ToPyObject for [T] where
    T: ToPyObject
[src]

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.

type ObjectType = PyList

impl<T> ToPyObject for Vec<T> where
    T: ToPyObject
[src]

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.

type ObjectType = PyList

impl ToPyObject for i8[src]

Conversion of Rust integer to Python int.

type ObjectType = PyInt

impl ToPyObject for u8[src]

Conversion of Rust integer to Python int.

type ObjectType = PyInt

impl ToPyObject for i16[src]

Conversion of Rust integer to Python int.

type ObjectType = PyInt

impl ToPyObject for u16[src]

Conversion of Rust integer to Python int.

type ObjectType = PyInt

impl ToPyObject for i32[src]

Conversion of Rust integer to Python int.

type ObjectType = PyInt

impl ToPyObject for u32[src]

Conversion of Rust integer to Python int.

type ObjectType = PyInt

impl ToPyObject for i64[src]

Conversion of Rust integer to Python int.

type ObjectType = PyInt

impl ToPyObject for isize[src]

Conversion of Rust integer to Python int.

type ObjectType = PyInt

impl ToPyObject for usize[src]

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.

type ObjectType = <u64 as ToPyObject>::ObjectType

impl<'p> ToPyObject for u64[src]

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.

type ObjectType = PyObject

impl ToPyObject for f64[src]

Conversion of Rust f64 to Python float.

type ObjectType = PyFloat

impl ToPyObject for f32[src]

Conversion of Rust f32 to Python float.

type ObjectType = PyFloat

impl<V, H> ToPyObject for HashSet<V, H> where
    V: Hash + Eq + ToPyObject,
    H: BuildHasher
[src]

type ObjectType = PySet

impl<V> ToPyObject for BTreeSet<V> where
    V: Eq + ToPyObject
[src]

type ObjectType = PySet

impl ToPyObject for str[src]

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.

type ObjectType = PyString

impl<'a> ToPyObject for Cow<'a, str>[src]

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.

type ObjectType = PyString

impl ToPyObject for String[src]

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.

type ObjectType = PyString

impl<A: ToPyObject> ToPyObject for (A,)[src]

Converts a Rust tuple to a Python tuple.

type ObjectType = PyTuple

impl<A: ToPyObject, B: ToPyObject> ToPyObject for (A, B)[src]

Converts a Rust tuple to a Python tuple.

type ObjectType = PyTuple

impl<A: ToPyObject, B: ToPyObject, C: ToPyObject> ToPyObject for (A, B, C)[src]

Converts a Rust tuple to a Python tuple.

type ObjectType = PyTuple

impl<A: ToPyObject, B: ToPyObject, C: ToPyObject, D: ToPyObject> ToPyObject for (A, B, C, D)[src]

Converts a Rust tuple to a Python tuple.

type ObjectType = PyTuple

impl<A: ToPyObject, B: ToPyObject, C: ToPyObject, D: ToPyObject, E: ToPyObject> ToPyObject for (A, B, C, D, E)[src]

Converts a Rust tuple to a Python tuple.

type ObjectType = PyTuple

impl<A: ToPyObject, B: ToPyObject, C: ToPyObject, D: ToPyObject, E: ToPyObject, F: ToPyObject> ToPyObject for (A, B, C, D, E, F)[src]

Converts a Rust tuple to a Python tuple.

type ObjectType = PyTuple

impl<A: ToPyObject, B: ToPyObject, C: ToPyObject, D: ToPyObject, E: ToPyObject, F: ToPyObject, G: ToPyObject> ToPyObject for (A, B, C, D, E, F, G)[src]

Converts a Rust tuple to a Python tuple.

type ObjectType = PyTuple

impl<A: ToPyObject, B: ToPyObject, C: ToPyObject, D: ToPyObject, E: ToPyObject, F: ToPyObject, G: ToPyObject, H: ToPyObject> ToPyObject for (A, B, C, D, E, F, G, H)[src]

Converts a Rust tuple to a Python tuple.

type ObjectType = PyTuple

impl<A: ToPyObject, B: ToPyObject, C: ToPyObject, D: ToPyObject, E: ToPyObject, F: ToPyObject, G: ToPyObject, H: ToPyObject, I: ToPyObject> ToPyObject for (A, B, C, D, E, F, G, H, I)[src]

Converts a Rust tuple to a Python tuple.

type ObjectType = PyTuple

Loading content...

Implementors

impl ToPyObject for AssertionError[src]

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

type ObjectType = AssertionError

impl ToPyObject for AttributeError[src]

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

type ObjectType = AttributeError

impl ToPyObject for BaseException[src]

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

type ObjectType = BaseException

impl ToPyObject for BufferError[src]

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

type ObjectType = BufferError

impl ToPyObject for EOFError[src]

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

type ObjectType = EOFError

impl ToPyObject for EnvironmentError[src]

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

type ObjectType = EnvironmentError

impl ToPyObject for Exception[src]

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

type ObjectType = Exception

impl ToPyObject for FloatingPointError[src]

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

type ObjectType = FloatingPointError

impl ToPyObject for IOError[src]

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

type ObjectType = IOError

impl ToPyObject for ImportError[src]

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

type ObjectType = ImportError

impl ToPyObject for IndexError[src]

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

type ObjectType = IndexError

impl ToPyObject for KeyError[src]

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

type ObjectType = KeyError

impl ToPyObject for KeyboardInterrupt[src]

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

type ObjectType = KeyboardInterrupt

impl ToPyObject for LookupError[src]

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

type ObjectType = LookupError

impl ToPyObject for MemoryError[src]

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

type ObjectType = MemoryError

impl ToPyObject for NameError[src]

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

type ObjectType = NameError

impl ToPyObject for NotImplementedError[src]

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

type ObjectType = NotImplementedError

impl ToPyObject for OSError[src]

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

type ObjectType = OSError

impl ToPyObject for OverflowError[src]

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

type ObjectType = OverflowError

impl ToPyObject for ReferenceError[src]

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

type ObjectType = ReferenceError

impl ToPyObject for RuntimeError[src]

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

type ObjectType = RuntimeError

impl ToPyObject for StandardError[src]

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

type ObjectType = StandardError

impl ToPyObject for SyntaxError[src]

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

type ObjectType = SyntaxError

impl ToPyObject for SystemError[src]

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

type ObjectType = SystemError

impl ToPyObject for SystemExit[src]

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

type ObjectType = SystemExit

impl ToPyObject for TypeError[src]

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

type ObjectType = TypeError

impl ToPyObject for UnicodeDecodeError[src]

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

type ObjectType = UnicodeDecodeError

impl ToPyObject for UnicodeEncodeError[src]

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

type ObjectType = UnicodeEncodeError

impl ToPyObject for UnicodeTranslateError[src]

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

type ObjectType = UnicodeTranslateError

impl ToPyObject for ValueError[src]

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

type ObjectType = ValueError

impl ToPyObject for ZeroDivisionError[src]

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

type ObjectType = ZeroDivisionError

impl ToPyObject for PyClass[src]

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

type ObjectType = PyClass

impl ToPyObject for PyInstance[src]

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

type ObjectType = PyInstance

impl ToPyObject for NoArgs[src]

Converts NoArgs to an empty Python tuple.

type ObjectType = PyTuple

impl ToPyObject for PyBool[src]

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

type ObjectType = PyBool

impl ToPyObject for PyBytes[src]

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

type ObjectType = PyBytes

impl ToPyObject for PyCapsule[src]

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

type ObjectType = PyCapsule

impl ToPyObject for PyDict[src]

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

type ObjectType = PyDict

impl ToPyObject for PyFloat[src]

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

type ObjectType = PyFloat

impl ToPyObject for PyInt[src]

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

type ObjectType = PyInt

impl ToPyObject for PyList[src]

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

type ObjectType = PyList

impl ToPyObject for PyLong[src]

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

type ObjectType = PyLong

impl ToPyObject for PyModule[src]

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

type ObjectType = PyModule

impl ToPyObject for PyNone[src]

impl ToPyObject for PyObject[src]

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

type ObjectType = PyObject

impl ToPyObject for PySequence[src]

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

type ObjectType = PySequence

impl ToPyObject for PySet[src]

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

type ObjectType = PySet

impl ToPyObject for PyString[src]

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

type ObjectType = PyString

impl ToPyObject for PyTuple[src]

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

type ObjectType = PyTuple

impl ToPyObject for PyType[src]

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

type ObjectType = PyType

impl ToPyObject for PyUnicode[src]

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

type ObjectType = PyUnicode

Loading content...