PyAny::downcast(): relax lifetime bounds

This commit is contained in:
Georg Brandl 2022-11-16 08:30:11 +01:00
parent c489809938
commit 7d4dfc32b3
3 changed files with 7 additions and 6 deletions

View file

@ -1,2 +1,3 @@
`PyAny::cast_as()` and `Py::cast_as()` are now deprecated in favor of `PyAny::cast_as()` and `Py::cast_as()` are now deprecated in favor of
`PyAny::downcast()` and the new `Py::downcast()`. `PyAny::downcast()` and the new `Py::downcast()`. The `PyAny::downcast()`
lifetime bounds where slightly relaxed.

View file

@ -997,7 +997,7 @@ impl PyObject {
/// flexible alternative, see [`Py::extract`](struct.Py.html#method.extract). /// flexible alternative, see [`Py::extract`](struct.Py.html#method.extract).
pub fn downcast<'p, T>(&'p self, py: Python<'p>) -> Result<&T, PyDowncastError<'_>> pub fn downcast<'p, T>(&'p self, py: Python<'p>) -> Result<&T, PyDowncastError<'_>>
where where
for<'py> T: PyTryFrom<'py>, T: PyTryFrom<'p>,
{ {
<T as PyTryFrom<'_>>::try_from(self.as_ref(py)) <T as PyTryFrom<'_>>::try_from(self.as_ref(py))
} }
@ -1008,7 +1008,7 @@ impl PyObject {
where where
D: PyTryFrom<'p>, D: PyTryFrom<'p>,
{ {
<D as PyTryFrom<'_>>::try_from(self.as_ref(py)) self.downcast(py)
} }
} }

View file

@ -752,7 +752,7 @@ impl PyAny {
where where
D: PyTryFrom<'a>, D: PyTryFrom<'a>,
{ {
<D as PyTryFrom<'_>>::try_from(self) self.downcast()
} }
/// Converts this `PyAny` to a concrete Python type. /// Converts this `PyAny` to a concrete Python type.
@ -773,9 +773,9 @@ impl PyAny {
/// assert!(any.downcast::<PyList>().is_err()); /// assert!(any.downcast::<PyList>().is_err());
/// }); /// });
/// ``` /// ```
pub fn downcast<T>(&self) -> Result<&T, PyDowncastError<'_>> pub fn downcast<'p, T>(&'p self) -> Result<&'p T, PyDowncastError<'_>>
where where
for<'py> T: PyTryFrom<'py>, T: PyTryFrom<'p>,
{ {
<T as PyTryFrom>::try_from(self) <T as PyTryFrom>::try_from(self)
} }