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,
{ ... }
}
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
).
Associated Types
type ObjectType: PythonObject
Required methods
fn to_py_object(&self, py: Python<'_>) -> Self::ObjectType
fn to_py_object(&self, py: Python<'_>) -> Self::ObjectType
Converts self into a Python object.
Provided methods
fn into_py_object(self, py: Python<'_>) -> Self::ObjectType where
Self: Sized,
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.
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
.
type ObjectType = T::ObjectType
Option::Some<T>
is converted like T
.
Option::None
is converted to Python None
.
type ObjectType = PyObject
impl<K, V, H> ToPyObject for HashMap<K, V, H> where
K: Hash + Eq + ToPyObject,
V: ToPyObject,
H: BuildHasher,
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
.
type ObjectType = PyDict
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
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
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
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 = PyLong
type ObjectType = PySet
type ObjectType = PySet
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
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
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, B: ToPyObject, C: ToPyObject, D: ToPyObject, E: ToPyObject> ToPyObject for (A, B, C, D, E)
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
.
type ObjectType = PyTuple
impl<A: ToPyObject, B: ToPyObject, C: ToPyObject, D: ToPyObject, E: ToPyObject, F: ToPyObject> ToPyObject for (A, B, C, D, E, F)
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
.
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)
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
.
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)
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
.
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)
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
.
type ObjectType = PyTuple
Implementors
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = AssertionError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = AttributeError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = BaseException
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = BlockingIOError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = BrokenPipeError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = BufferError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = ChildProcessError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = ConnectionAbortedError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = ConnectionError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = ConnectionRefusedError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = ConnectionResetError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = EOFError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = EnvironmentError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = Exception
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = FileExistsError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = FileNotFoundError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = FloatingPointError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = IOError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = ImportError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = IndexError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = InterruptedError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = IsADirectoryError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = KeyError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = KeyboardInterrupt
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = LookupError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = MemoryError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = NameError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = NotADirectoryError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = NotImplementedError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = OSError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = OverflowError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PermissionError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = ProcessLookupError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = ReferenceError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = RuntimeError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = SyntaxError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = SystemError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = SystemExit
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = TimeoutError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = TypeError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = UnicodeDecodeError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = UnicodeEncodeError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = UnicodeTranslateError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = ValueError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = ZeroDivisionError
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyBool
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyBytes
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyCapsule
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyDict
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyFloat
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyLong
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyList
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyModule
type ObjectType = PyObject
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyObject
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PySequence
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PySet
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyTuple
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyType
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.