1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
use crate::{types::any::PyAnyMethods, Bound, PyAny, PyResult, PyTypeCheck};

pub(crate) trait PyResultExt<'py>: crate::sealed::Sealed {
    fn downcast_into<T: PyTypeCheck>(self) -> PyResult<Bound<'py, T>>;
    unsafe fn downcast_into_unchecked<T>(self) -> PyResult<Bound<'py, T>>;
}

impl<'py> PyResultExt<'py> for PyResult<Bound<'py, PyAny>> {
    #[inline]
    fn downcast_into<T: PyTypeCheck>(self) -> PyResult<Bound<'py, T>> where {
        self.and_then(|instance| instance.downcast_into().map_err(Into::into))
    }

    #[inline]
    unsafe fn downcast_into_unchecked<T>(self) -> PyResult<Bound<'py, T>> {
        self.map(|instance| instance.downcast_into_unchecked())
    }
}
⚠️ Internal Docs ⚠️ Not Public API 👉 Official Docs Here