# pyo3::conversion::PyTryFrom - Rust
Trait implemented by Python object types that allow a checked downcast. If `T` implements `PyTryFrom`, we can convert `&PyAny` to `&T`.
Keywords: rust, rustlang, rust-lang, PyTryFrom

# Trait pyo3::conversion::PyTryFrom [−] [src]

    pub trait PyTryFrom<'v>: Sized + PyNativeType {
        fn try_from<V: Into<&'v PyAny>>(
            value: V
        ) -> Result<&'v Self, PyDowncastError<'v>>;
        fn try_from_exact<V: Into<&'v PyAny>>(
            value: V
        ) -> Result<&'v Self, PyDowncastError<'v>>;
        unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self;
    }

Trait implemented by Python object types that allow a checked downcast. If `T` implements `PyTryFrom`, we can convert `&PyAny` to `&T`.

This trait is similar to `std::convert::TryFrom`

## Required methods

### `fn try_from<V: Into<&'v PyAny>>(
value: V
) -> Result<&'v Self, PyDowncastError<'v>>` [src]

Cast from a concrete Python object type to PyObject.

### `fn try_from_exact<V: Into<&'v PyAny>>(
value: V
) -> Result<&'v Self, PyDowncastError<'v>>` [src]

Cast from a concrete Python object type to PyObject. With exact type check.

### `unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self` [src]

Cast a PyAny to a specific type of PyObject. The caller must have already verified the reference is for this type.

# Safety

Callers must ensure that the type is valid or risk type confusion.

Loading content...

## Implementors

### `impl<'v> PyTryFrom<'v> for PyIterator` [src]

#### `fn try_from<V: Into<&'v PyAny>>(
value: V
) -> Result<&'v PyIterator, PyDowncastError<'v>>` [src]

#### `fn try_from_exact<V: Into<&'v PyAny>>(
value: V
) -> Result<&'v PyIterator, PyDowncastError<'v>>` [src]

#### `unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v PyIteratorⓘ

### Notable traits for &'p PyIterator

`impl<'p> Iterator for &'p PyIterator type Item = PyResult<&'p PyAny>;`
` [src]

### `impl<'v> PyTryFrom<'v> for PySequence` [src]

#### `fn try_from<V: Into<&'v PyAny>>(
value: V
) -> Result<&'v PySequence, PyDowncastError<'v>>` [src]

#### `fn try_from_exact<V: Into<&'v PyAny>>(
value: V
) -> Result<&'v PySequence, PyDowncastError<'v>>` [src]

#### `unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v PySequence` [src]

### `impl<'v, T> PyTryFrom<'v> for PyCell<T> where
T: 'v + PyClass,` [src]

#### `fn try_from<V: Into<&'v PyAny>>(
value: V
) -> Result<&'v Self, PyDowncastError<'v>>` [src]

#### `fn try_from_exact<V: Into<&'v PyAny>>(
value: V
) -> Result<&'v Self, PyDowncastError<'v>>` [src]

#### `unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self` [src]

### `impl<'v, T> PyTryFrom<'v> for T where
T: PyTypeInfo + PyNativeType,` [src]

#### `fn try_from<V: Into<&'v PyAny>>(
value: V
) -> Result<&'v Self, PyDowncastError<'v>>` [src]

#### `fn try_from_exact<V: Into<&'v PyAny>>(
value: V
) -> Result<&'v Self, PyDowncastError<'v>>` [src]

#### `unsafe fn try_from_unchecked<V: Into<&'v PyAny>>(value: V) -> &'v Self` [src]

Loading content...