pub trait FromPyObject<'s>: Sized {
// Required method
fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<Self>;
}
Expand description
FromPyObject is implemented by various types that can be extracted from a Python object.
Normal usage is through the PyObject::extract
helper method:
let value = obj.extract::<TargetType>(py)?;
Each target type for this conversion supports a different Python objects as input.
Calls with an unsupported input object will result in an exception (usually a TypeError
).
This trait is also used by the py_fn!
and py_class!
and py_argparse!
macros
in order to translate from Python objects to the expected Rust parameter types.
For example, the parameter x
in def method(self, x: i32)
will use
impl FromPyObject for i32
to convert the input Python object into a Rust i32
.
When these macros are used with reference parameters (x: &str
), the trait
RefFromPyObject
is used instead.
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementations on Foreign Types§
Source§impl<'s> FromPyObject<'s> for Cow<'s, str>
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.
In Python 2.7, str
is expected to be UTF-8 encoded.
impl<'s> FromPyObject<'s> for Cow<'s, str>
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.
In Python 2.7, str
is expected to be UTF-8 encoded.
Returns a UnicodeDecodeError
if the input is not valid unicode
(containing unpaired surrogates, or a Python 2.7 byte string that is
not valid UTF-8).
Source§impl<'s> FromPyObject<'s> for Cow<'s, [u8]>
For Python bytes
, returns a reference to the existing immutable string data.
If the Python object is a single-dimensional buffer of format c
or B
(C: char
or unsigned char
),
returns an owned copy of the data in the buffer.
Otherwise, uses the sequence protocol and converts each individual element
via impl FromPyObject for u8
.
impl<'s> FromPyObject<'s> for Cow<'s, [u8]>
For Python bytes
, returns a reference to the existing immutable string data.
If the Python object is a single-dimensional buffer of format c
or B
(C: char
or unsigned char
),
returns an owned copy of the data in the buffer.
Otherwise, uses the sequence protocol and converts each individual element
via impl FromPyObject for u8
.
Source§impl<'s> FromPyObject<'s> for bool
impl<'s> FromPyObject<'s> for bool
Source§impl<'s> FromPyObject<'s> for f32
impl<'s> FromPyObject<'s> for f32
Source§impl<'s> FromPyObject<'s> for f64
impl<'s> FromPyObject<'s> for f64
Source§impl<'s> FromPyObject<'s> for i8
impl<'s> FromPyObject<'s> for i8
Source§impl<'s> FromPyObject<'s> for i16
impl<'s> FromPyObject<'s> for i16
Source§impl<'s> FromPyObject<'s> for i32
impl<'s> FromPyObject<'s> for i32
Source§impl<'s> FromPyObject<'s> for i64
impl<'s> FromPyObject<'s> for i64
Source§impl<'s> FromPyObject<'s> for isize
impl<'s> FromPyObject<'s> for isize
Source§impl<'s> FromPyObject<'s> for u8
impl<'s> FromPyObject<'s> for u8
Source§impl<'s> FromPyObject<'s> for u16
impl<'s> FromPyObject<'s> for u16
Source§impl<'s> FromPyObject<'s> for u32
impl<'s> FromPyObject<'s> for u32
Source§impl<'s> FromPyObject<'s> for u64
Converts Python integers to Rust integers.
impl<'s> FromPyObject<'s> for u64
Converts Python integers to Rust integers.
Returns OverflowError if the input integer does not fit the Rust type; or TypeError if the input is not an integer.
Source§impl<'s> FromPyObject<'s> for usize
impl<'s> FromPyObject<'s> for usize
Source§impl<'s> FromPyObject<'s> for String
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.
In Python 2.7, str
is expected to be UTF-8 encoded.
impl<'s> FromPyObject<'s> for String
Allows extracting strings from Python objects.
Accepts Python str
and unicode
objects.
In Python 2.7, str
is expected to be UTF-8 encoded.
Returns a UnicodeDecodeError
if the input is not valid unicode
(containing unpaired surrogates, or a Python 2.7 byte string that is
not valid UTF-8).
Source§impl<'s, A: FromPyObject<'s>> FromPyObject<'s> for (A,)
Converts a Python tuple
to a Rust tuple.
impl<'s, A: FromPyObject<'s>> FromPyObject<'s> for (A,)
Converts a Python tuple
to a Rust tuple.
Note: only accepts Python tuple
(or derived classes);
other types are not accepted.
Source§impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>> FromPyObject<'s> for (A, B)
Converts a Python tuple
to a Rust tuple.
impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>> FromPyObject<'s> for (A, B)
Converts a Python tuple
to a Rust tuple.
Note: only accepts Python tuple
(or derived classes);
other types are not accepted.
Source§impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>> FromPyObject<'s> for (A, B, C)
Converts a Python tuple
to a Rust tuple.
impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>> FromPyObject<'s> for (A, B, C)
Converts a Python tuple
to a Rust tuple.
Note: only accepts Python tuple
(or derived classes);
other types are not accepted.
Source§impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D)
Converts a Python tuple
to a Rust tuple.
impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D)
Converts a Python tuple
to a Rust tuple.
Note: only accepts Python tuple
(or derived classes);
other types are not accepted.
Source§impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E)
Converts a Python tuple
to a Rust tuple.
impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E)
Converts a Python tuple
to a Rust tuple.
Note: only accepts Python tuple
(or derived classes);
other types are not accepted.
Source§impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F)
Converts a Python tuple
to a Rust tuple.
impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F)
Converts a Python tuple
to a Rust tuple.
Note: only accepts Python tuple
(or derived classes);
other types are not accepted.
Source§impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>, G: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F, G)
Converts a Python tuple
to a Rust tuple.
impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>, G: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F, G)
Converts a Python tuple
to a Rust tuple.
Note: only accepts Python tuple
(or derived classes);
other types are not accepted.
Source§impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>, G: FromPyObject<'s>, H: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F, G, H)
Converts a Python tuple
to a Rust tuple.
impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>, G: FromPyObject<'s>, H: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F, G, H)
Converts a Python tuple
to a Rust tuple.
Note: only accepts Python tuple
(or derived classes);
other types are not accepted.
Source§impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>, G: FromPyObject<'s>, H: FromPyObject<'s>, I: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F, G, H, I)
Converts a Python tuple
to a Rust tuple.
impl<'s, A: FromPyObject<'s>, B: FromPyObject<'s>, C: FromPyObject<'s>, D: FromPyObject<'s>, E: FromPyObject<'s>, F: FromPyObject<'s>, G: FromPyObject<'s>, H: FromPyObject<'s>, I: FromPyObject<'s>> FromPyObject<'s> for (A, B, C, D, E, F, G, H, I)
Converts a Python tuple
to a Rust tuple.
Note: only accepts Python tuple
(or derived classes);
other types are not accepted.
Source§impl<'s, T> FromPyObject<'s> for Option<T>where
T: FromPyObject<'s>,
If the python value is None, returns Option::None
.
Otherwise, converts the python value to T
and returns Some(T)
.
impl<'s, T> FromPyObject<'s> for Option<T>where
T: FromPyObject<'s>,
If the python value is None, returns Option::None
.
Otherwise, converts the python value to T
and returns Some(T)
.
Source§impl<'s, T> FromPyObject<'s> for Vec<T>where
for<'a> T: FromPyObject<'a>,
Uses the sequence protocol and converts each individual element
via impl FromPyObject for T
.
impl<'s, T> FromPyObject<'s> for Vec<T>where
for<'a> T: FromPyObject<'a>,
Uses the sequence protocol and converts each individual element
via impl FromPyObject for T
.
Note: when using --features nightly
, a specialization of this impl
may use the buffer protocol to perform a more efficient bulk copy.