1use crate::{types::any::PyAnyMethods, Bound, PyAny, PyResult, PyTypeCheck};
2
3pub(crate) trait PyResultExt<'py>: crate::sealed::Sealed {
4 fn downcast_into<T: PyTypeCheck>(self) -> PyResult<Bound<'py, T>>;
5 unsafe fn downcast_into_unchecked<T>(self) -> PyResult<Bound<'py, T>>;
6}
7
8impl<'py> PyResultExt<'py> for PyResult<Bound<'py, PyAny>> {
9 #[inline]
10 fn downcast_into<T: PyTypeCheck>(self) -> PyResult<Bound<'py, T>> where {
11 self.and_then(|instance| instance.downcast_into().map_err(Into::into))
12 }
13
14 #[inline]
15 unsafe fn downcast_into_unchecked<T>(self) -> PyResult<Bound<'py, T>> {
16 self.map(|instance| instance.downcast_into_unchecked())
17 }
18}