Trait caffe2_imports::FromPyObject
source · pub trait FromPyObject<'source>: Sized {
// Required method
fn extract(ob: &'source PyAny) -> Result<Self, PyErr>;
}
Expand description
Extract a type from a Python object.
Normal usage is through the extract
methods on Py
and PyAny
, which forward to this trait.
Examples
use pyo3::prelude::*;
use pyo3::types::PyString;
Python::with_gil(|py| {
let obj: Py<PyString> = PyString::new(py, "blah").into();
// Straight from an owned reference
let s: &str = obj.extract(py)?;
// Or from a borrowed reference
let obj: &PyString = obj.as_ref(py);
let s: &str = obj.extract()?;
})
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 which case applies depends on the runtime type of the Python object,
both the obj
and prepared
variables must outlive the resulting string slice.
The trait’s conversion method takes a &PyAny
argument but is called
FromPyObject
for historical reasons.
Required Methods§
Implementations on Foreign Types§
source§impl<'s, T0, T1, T2, T3> FromPyObject<'s> for (T0, T1, T2, T3)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
T2: FromPyObject<'s>,
T3: FromPyObject<'s>,
impl<'s, T0, T1, T2, T3> FromPyObject<'s> for (T0, T1, T2, T3)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>,
source§impl<'s, T0, T1> FromPyObject<'s> for (T0, T1)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
impl<'s, T0, T1> FromPyObject<'s> for (T0, T1)where T0: FromPyObject<'s>, T1: FromPyObject<'s>,
source§impl<'source> FromPyObject<'source> for u32
impl<'source> FromPyObject<'source> for u32
source§impl<'source> FromPyObject<'source> for u8
impl<'source> FromPyObject<'source> for u8
source§impl<'s, T0, T1, T2, T3, T4, T5, T6> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
T2: FromPyObject<'s>,
T3: FromPyObject<'s>,
T4: FromPyObject<'s>,
T5: FromPyObject<'s>,
T6: FromPyObject<'s>,
impl<'s, T0, T1, T2, T3, T4, T5, T6> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>,
source§impl<'source> FromPyObject<'source> for bool
impl<'source> FromPyObject<'source> for bool
Converts a Python bool
to a Rust bool
.
Fails with TypeError
if the input is not a Python bool
.
source§impl<'source> FromPyObject<'source> for i8
impl<'source> FromPyObject<'source> for i8
source§impl<'source> FromPyObject<'source> for u16
impl<'source> FromPyObject<'source> for u16
source§impl<'a, T, const N: usize> FromPyObject<'a> for [T; N]where
T: FromPyObject<'a>,
impl<'a, T, const N: usize> FromPyObject<'a> for [T; N]where T: FromPyObject<'a>,
source§impl<'s, T0> FromPyObject<'s> for (T0,)where
T0: FromPyObject<'s>,
impl<'s, T0> FromPyObject<'s> for (T0,)where T0: FromPyObject<'s>,
source§impl<'source> FromPyObject<'source> for i16
impl<'source> FromPyObject<'source> for i16
source§impl<'source> FromPyObject<'source> for i64
impl<'source> FromPyObject<'source> for i64
source§impl<'source> FromPyObject<'source> for u128
impl<'source> FromPyObject<'source> for u128
source§impl<'s, T0, T1, T2> FromPyObject<'s> for (T0, T1, T2)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
T2: FromPyObject<'s>,
impl<'s, T0, T1, T2> FromPyObject<'s> for (T0, T1, T2)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>,
source§impl<'source> FromPyObject<'source> for u64
impl<'source> FromPyObject<'source> for u64
source§impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
T2: FromPyObject<'s>,
T3: FromPyObject<'s>,
T4: FromPyObject<'s>,
T5: FromPyObject<'s>,
T6: FromPyObject<'s>,
T7: FromPyObject<'s>,
T8: FromPyObject<'s>,
impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>,
source§impl<'a> FromPyObject<'a> for &'a [u8]
impl<'a> FromPyObject<'a> for &'a [u8]
source§impl<'source> FromPyObject<'source> for i32
impl<'source> FromPyObject<'source> for i32
source§impl<'source> FromPyObject<'source> for &'source str
impl<'source> FromPyObject<'source> for &'source str
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.
source§impl<'s, T0, T1, T2, T3, T4, T5, T6, T7> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
T2: FromPyObject<'s>,
T3: FromPyObject<'s>,
T4: FromPyObject<'s>,
T5: FromPyObject<'s>,
T6: FromPyObject<'s>,
T7: FromPyObject<'s>,
impl<'s, T0, T1, T2, T3, T4, T5, T6, T7> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>,
source§impl<'source> FromPyObject<'source> for f64
impl<'source> FromPyObject<'source> for f64
source§impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
T2: FromPyObject<'s>,
T3: FromPyObject<'s>,
T4: FromPyObject<'s>,
T5: FromPyObject<'s>,
T6: FromPyObject<'s>,
T7: FromPyObject<'s>,
T8: FromPyObject<'s>,
T9: FromPyObject<'s>,
T10: FromPyObject<'s>,
T11: FromPyObject<'s>,
impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>, T10: FromPyObject<'s>, T11: FromPyObject<'s>,
source§impl<'source> FromPyObject<'source> for f32
impl<'source> FromPyObject<'source> for f32
source§impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
T2: FromPyObject<'s>,
T3: FromPyObject<'s>,
T4: FromPyObject<'s>,
T5: FromPyObject<'s>,
T6: FromPyObject<'s>,
T7: FromPyObject<'s>,
T8: FromPyObject<'s>,
T9: FromPyObject<'s>,
T10: FromPyObject<'s>,
impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>, T10: FromPyObject<'s>,
source§impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
T2: FromPyObject<'s>,
T3: FromPyObject<'s>,
T4: FromPyObject<'s>,
T5: FromPyObject<'s>,
T6: FromPyObject<'s>,
T7: FromPyObject<'s>,
T8: FromPyObject<'s>,
T9: FromPyObject<'s>,
impl<'s, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5, T6, T7, T8, T9)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>, T6: FromPyObject<'s>, T7: FromPyObject<'s>, T8: FromPyObject<'s>, T9: FromPyObject<'s>,
source§impl<'source> FromPyObject<'source> for isize
impl<'source> FromPyObject<'source> for isize
source§impl<'s, T0, T1, T2, T3, T4> FromPyObject<'s> for (T0, T1, T2, T3, T4)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
T2: FromPyObject<'s>,
T3: FromPyObject<'s>,
T4: FromPyObject<'s>,
impl<'s, T0, T1, T2, T3, T4> FromPyObject<'s> for (T0, T1, T2, T3, T4)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>,
source§impl<'a, T> FromPyObject<'a> for Vec<T, Global>where
T: FromPyObject<'a>,
impl<'a, T> FromPyObject<'a> for Vec<T, Global>where T: FromPyObject<'a>,
source§impl<'s, T0, T1, T2, T3, T4, T5> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5)where
T0: FromPyObject<'s>,
T1: FromPyObject<'s>,
T2: FromPyObject<'s>,
T3: FromPyObject<'s>,
T4: FromPyObject<'s>,
T5: FromPyObject<'s>,
impl<'s, T0, T1, T2, T3, T4, T5> FromPyObject<'s> for (T0, T1, T2, T3, T4, T5)where T0: FromPyObject<'s>, T1: FromPyObject<'s>, T2: FromPyObject<'s>, T3: FromPyObject<'s>, T4: FromPyObject<'s>, T5: FromPyObject<'s>,
source§impl<'source> FromPyObject<'source> for usize
impl<'source> FromPyObject<'source> for usize
source§impl<'source> FromPyObject<'source> for i128
impl<'source> FromPyObject<'source> for i128
source§impl<'source> FromPyObject<'source> for Cow<'source, [u8]>
impl<'source> FromPyObject<'source> for Cow<'source, [u8]>
Special-purpose trait impl to efficiently handle both bytes
and bytearray
If the source object is a bytes
object, the Cow
will be borrowed and
pointing into the source object, and no copying or heap allocations will happen.
If it is a bytearray
, its contents will be copied to an owned Cow
.
source§impl FromPyObject<'_> for String
impl FromPyObject<'_> for String
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.