
If the input is neither a string nor an integer, the error message will be: Let error = thing.extract::(py).unwrap_err() StringIntTuple(String, usize), // input is a 2-tuple with String and intĬoordinates3d ", other), IntTuple(usize, usize), // input is a 2-tuple with positive ints The transparentĪttribute can be applied to single-field-variants. a tuple variant assumes that the input is a Python tuple, and a struct variant defaults toĮxtracting fields as attributes but can be configured in the same manner. The same customizations and restrictions described for struct derivations apply to enum variants, This makes it possible to extract Python union types like str | int. As soon as a variant can be extracted successfully, that variant is returned.
#Rust downcast trait object code
The FromPyObject derivation for enums generates code that tries to extract the variants in the Let stru: RustyTransparentStruct = s.extract()? Let tup: RustyTransparentTupleStruct = s.extract()? Struct RustyTransparentTupleStruct(String) This behaviour is enabled per default for newtype structs and tuple-variants obj.extract(), rather than trying to accessĪn item or attribute. In extracting directly from the input object, i.e. The pyo3(transparent) attribute can be used on structs with exactly one field. Specify the struct as use pyo3::prelude::* To override this behaviour and ensure that the input is in fact a tuple, Tuple structs with a single field are treated as wrapper types which are described in theįollowing section. Let rustytuple: RustyTuple = tuple.extract()? Is extracted from the nth item in the Python tuple. The input isĪlways assumed to be a Python tuple with the same length as the Rust type, the nth field Tuple structs are also supported but do not allow customizing the extraction. Non-empty string literals while item can take any valid literal that implements The arguments for attribute are restricted to This tries to extract string_attr from the attribute name and string_in_mappingįrom a mapping with the key "key". The argument passed to getattr and get_item can also be configured: use pyo3::prelude::* Īssert_eq!(rustystruct.string_attr, "test") Īssert_eq!(rustystruct.string_in_mapping, "test2")

Let rustystruct: RustyStruct = dict.extract()? Let rustystruct: RustyStruct = instance.extract()? Īssert_eq!(rustystruct.my_string, "test") īy setting the # attribute on the field, PyO3 will attempt to extract the value by calling the get_item method on the Python object. obj.getattr("my_string"), and call extract() on the attribute. The derivation generates code that will attempt to access the attribute my_string on Derivation for empty enums, enum variants and If the member types themselves implement FromPyObject. Deriving FromPyObjectįromPyObject can be automatically derived for many kinds of structs and enums Std::cell::RefCell and ensure (at runtime) that Rust borrows are allowed. Mutable references, you have to extract the PyO3 reference wrappers PyRefĪnd PyRefMut. There, in order to both beĪble to operate on mutable references and satisfy Rust's rules of non-aliasing Variety of Rust types, which you can check out in the implementor list ofįromPyObject is also implemented for your own Rust types wrapped as Python This method is available for many Python object types, and can produce a wide It returns a PyResult with a type error if the conversionįails, so usually you will use something like use pyo3::prelude::* The easiest way to convert a Python object to a Rust value is using PyO3 provides some handy traits to convert between Python types and Rust types.extract() and the FromPyObject trait
