| 1 | use crate::{types::any::PyAnyMethods, Bound, PyAny, PyResult, PyTypeCheck}; |
| 2 | |
| 3 | pub(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 | |
| 8 | impl<'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: Bound<'py, PyAny>| instance.downcast_into().map_err(op:Into::into)) |
| 12 | } |
| 13 | |
| 14 | #[inline ] |
| 15 | unsafe fn downcast_into_unchecked<T>(self) -> PyResult<Bound<'py, T>> { |
| 16 | self.map(|instance: Bound<'py, PyAny>| unsafe { instance.downcast_into_unchecked() }) |
| 17 | } |
| 18 | } |
| 19 | |