Trait cpython::ToPyObject

source ·
pub trait ToPyObject {
    type ObjectType: PythonObject;

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

    // Provided methods
    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§

source

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

Converts self into a Python object.

Provided Methods§

source

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

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.

source

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

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.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl ToPyObject for bool

Converts a rust bool to a Python bool.

§

type ObjectType = PyBool

source§

fn to_py_object(&self, py: Python<'_>) -> PyBool

source§

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

source§

impl ToPyObject for f32

Conversion of Rust f32 to Python float.

source§

impl ToPyObject for f64

Conversion of Rust f64 to Python float.

source§

impl ToPyObject for i8

Conversion of Rust integer to Python int.

source§

impl ToPyObject for i16

Conversion of Rust integer to Python int.

source§

impl ToPyObject for i32

Conversion of Rust integer to Python int.

source§

impl ToPyObject for i64

Conversion of Rust integer to Python int.

source§

impl ToPyObject for isize

Conversion of Rust integer to Python int.

source§

impl ToPyObject for str

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.

source§

impl ToPyObject for u8

Conversion of Rust integer to Python int.

source§

impl ToPyObject for u16

Conversion of Rust integer to Python int.

source§

impl ToPyObject for u32

Conversion of Rust integer to Python int.

source§

impl ToPyObject for usize

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.

source§

impl ToPyObject for String

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.

source§

impl<'a> ToPyObject for Cow<'a, str>

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.

source§

impl<'a, T> ToPyObject for &'a T
where T: ToPyObject + ?Sized,

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

§

type ObjectType = <T as ToPyObject>::ObjectType

source§

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

source§

fn into_py_object(self, py: Python<'_>) -> T::ObjectType

source§

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

source§

impl<'p> ToPyObject for u64

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.

source§

impl<A: ToPyObject> ToPyObject for (A,)

Converts a Rust tuple to a Python tuple.

source§

impl<A: ToPyObject, B: ToPyObject> ToPyObject for (A, B)

Converts a Rust tuple to a Python tuple.

source§

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

Converts a Rust tuple to a Python tuple.

source§

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

Converts a Rust tuple to a Python tuple.

source§

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

Converts a Rust tuple to a Python tuple.

source§

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

Converts a Rust tuple to a Python tuple.

source§

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

Converts a Rust tuple to a Python tuple.

source§

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)

Converts a Rust tuple to a Python tuple.

source§

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)

Converts a Rust tuple to a Python tuple.

source§

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

Converts a Rust BTreeMap to a Python dict.

source§

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

Converts a Rust HashMap to a Python dict.

source§

impl<T> ToPyObject for Option<T>
where T: ToPyObject,

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

source§

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

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.

source§

impl<T> ToPyObject for Vec<T>
where T: ToPyObject,

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.

source§

impl<V> ToPyObject for BTreeSet<V>
where V: Eq + ToPyObject,

§

type ObjectType = PySet

source§

fn to_py_object(&self, py: Python<'_>) -> PySet

source§

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

§

type ObjectType = PySet

source§

fn to_py_object(&self, py: Python<'_>) -> PySet

Implementors§

source§

impl ToPyObject for AssertionError

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

source§

impl ToPyObject for AttributeError

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

source§

impl ToPyObject for BaseException

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

source§

impl ToPyObject for BlockingIOError

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

source§

impl ToPyObject for BrokenPipeError

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

source§

impl ToPyObject for BufferError

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

source§

impl ToPyObject for ChildProcessError

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

source§

impl ToPyObject for ConnectionAbortedError

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

source§

impl ToPyObject for ConnectionError

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

source§

impl ToPyObject for ConnectionRefusedError

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

source§

impl ToPyObject for ConnectionResetError

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

source§

impl ToPyObject for EOFError

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

source§

impl ToPyObject for EnvironmentError

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

source§

impl ToPyObject for Exception

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

source§

impl ToPyObject for FileExistsError

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

source§

impl ToPyObject for FileNotFoundError

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

source§

impl ToPyObject for FloatingPointError

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

source§

impl ToPyObject for IOError

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

source§

impl ToPyObject for ImportError

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

source§

impl ToPyObject for IndexError

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

source§

impl ToPyObject for InterruptedError

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

source§

impl ToPyObject for IsADirectoryError

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

source§

impl ToPyObject for KeyError

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

source§

impl ToPyObject for KeyboardInterrupt

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

source§

impl ToPyObject for LookupError

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

source§

impl ToPyObject for MemoryError

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

source§

impl ToPyObject for NameError

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

source§

impl ToPyObject for NotADirectoryError

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

source§

impl ToPyObject for NotImplementedError

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

source§

impl ToPyObject for OSError

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

source§

impl ToPyObject for OverflowError

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

source§

impl ToPyObject for PermissionError

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

source§

impl ToPyObject for ProcessLookupError

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

source§

impl ToPyObject for ReferenceError

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

source§

impl ToPyObject for RuntimeError

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

source§

impl ToPyObject for SyntaxError

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

source§

impl ToPyObject for SystemError

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

source§

impl ToPyObject for SystemExit

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

source§

impl ToPyObject for TimeoutError

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

source§

impl ToPyObject for TypeError

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

source§

impl ToPyObject for UnicodeDecodeError

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

source§

impl ToPyObject for UnicodeEncodeError

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

source§

impl ToPyObject for UnicodeTranslateError

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

source§

impl ToPyObject for ValueError

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

source§

impl ToPyObject for ZeroDivisionError

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

source§

impl ToPyObject for NoArgs

Converts NoArgs to an empty Python tuple.

source§

impl ToPyObject for PyBool

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

source§

impl ToPyObject for PyBytes

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

source§

impl ToPyObject for PyCapsule

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

source§

impl ToPyObject for PyDict

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

source§

impl ToPyObject for PyFloat

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

source§

impl ToPyObject for PyLong

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

source§

impl ToPyObject for PyList

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

source§

impl ToPyObject for PyModule

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

source§

impl ToPyObject for PyNone

source§

impl ToPyObject for PyObject

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

source§

impl ToPyObject for PySequence

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

source§

impl ToPyObject for PySet

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

source§

impl ToPyObject for PyTuple

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

source§

impl ToPyObject for PyType

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

source§

impl ToPyObject for PyString

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