pub trait TryFromObject: Sized {
// Required method
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>;
}
Expand description
Implemented by any type that can be created from a Python object.
Any type that implements TryFromObject
is automatically FromArgs
, and
so can be accepted as a argument to a built-in function.
Required Methods§
Sourcefn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
Attempt to convert a Python object to a value of this type.
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 TryFromObject for Option<Gid>
impl TryFromObject for Option<Gid>
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
Source§impl TryFromObject for Option<Uid>
impl TryFromObject for Option<Uid>
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
Source§impl TryFromObject for f32
impl TryFromObject for f32
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
Source§impl TryFromObject for f64
impl TryFromObject for f64
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
Source§impl TryFromObject for Duration
impl TryFromObject for Duration
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
Source§impl TryFromObject for BorrowedFd<'_>
impl TryFromObject for BorrowedFd<'_>
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
Source§impl<T: TryFromObject> TryFromObject for Option<T>
impl<T: TryFromObject> TryFromObject for Option<T>
fn try_from_object(vm: &VirtualMachine, obj: PyObjectRef) -> PyResult<Self>
Implementors§
impl TryFromObject for ExceptionCtor
impl TryFromObject for ArgAsciiBuffer
impl TryFromObject for ArgStrOrBytesLike
impl TryFromObject for FsPath
impl TryFromObject for ArgCallable
impl TryFromObject for ArgIndex
impl TryFromObject for ArgIntoBool
impl TryFromObject for ArgIntoComplex
impl TryFromObject for ArgIntoFloat
impl TryFromObject for ArgMapping
impl TryFromObject for PyObjectRef
impl TryFromObject for OsPath
impl TryFromObject for PyIter<PyObjectRef>
impl TryFromObject for Fildes
impl<A, B> TryFromObject for Either<A, B>where
A: TryFromObject,
B: TryFromObject,
This allows a builtin method to accept arguments that may be one of two
types, raising a TypeError
if it is neither.
§Example
use rustpython_vm::VirtualMachine;
use rustpython_vm::builtins::{PyStrRef, PyIntRef};
use rustpython_vm::function::Either;
fn do_something(arg: Either<PyIntRef, PyStrRef>, vm: &VirtualMachine) {
match arg {
Either::A(int)=> {
// do something with int
}
Either::B(string) => {
// do something with string
}
}
}
impl<T> TryFromObject for ArgIterable<T>where
T: TryFromObject,
impl<T> TryFromObject for ArgPrimitiveIndex<T>
impl<T> TryFromObject for PyRef<T>where
T: PyPayload,
impl<T: PyPayload> TryFromObject for PyRefExact<T>
impl<T: TryFromObject> TryFromObject for PyArithmeticValue<T>
impl<T: TryFromObject> TryFromObject for ArgSequence<T>
impl<T: for<'a> TryFromBorrowedObject<'a>> TryFromObject for T
Rust-side only version of TryFromObject to reduce unnecessary Rc::clone