Convenience methods for common usages of [`IntoPyObject`]. Every type that implements [`IntoPyObject`] also implements this trait. These methods: - Drop type information from the output, returning a `PyAny` object. - Always convert the `Error` type to `PyErr`, which may incur a performance penalty but it more convenient in contexts where the `?` operator would produce a `PyErr` anyway.
| 220 | /// - Always convert the `Error` type to `PyErr`, which may incur a performance penalty but it |
| 221 | /// more convenient in contexts where the `?` operator would produce a `PyErr` anyway. |
| 222 | pub trait IntoPyObjectExt<'py>: IntoPyObject<'py> + into_pyobject_ext::Sealed { |
| 223 | /// Converts `self` into an owned Python object, dropping type information. |
| 224 | #[inline] |
| 225 | fn into_bound_py_any(self, py: Python<'py>) -> PyResult<Bound<'py, PyAny>> { |
| 226 | match self.into_pyobject(py) { |
| 227 | Ok(obj) => Ok(obj.into_any().into_bound()), |
| 228 | Err(err) => Err(err.into()), |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /// Converts `self` into an owned Python object, dropping type information and unbinding it |
| 233 | /// from the `'py` lifetime. |
| 234 | #[inline] |
| 235 | fn into_py_any(self, py: Python<'py>) -> PyResult<Py<PyAny>> { |
| 236 | match self.into_pyobject(py) { |
| 237 | Ok(obj) => Ok(obj.into_any().unbind()), |
| 238 | Err(err) => Err(err.into()), |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /// Converts `self` into a Python object. |
| 243 | /// |
| 244 | /// This is equivalent to calling [`into_pyobject`][IntoPyObject::into_pyobject] followed |
| 245 | /// with `.map_err(Into::into)` to convert the error type to [`PyErr`]. This is helpful |
| 246 | /// for generic code which wants to make use of the `?` operator. |
| 247 | #[inline] |
| 248 | fn into_pyobject_or_pyerr(self, py: Python<'py>) -> PyResult<Self::Output> { |
| 249 | match self.into_pyobject(py) { |
| 250 | Ok(obj) => Ok(obj), |
| 251 | Err(err) => Err(err.into()), |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | impl<'py, T> IntoPyObjectExt<'py> for T where T: IntoPyObject<'py> {} |
| 257 |
no outgoing calls
no test coverage detected