From 81b71f6c49b87be5335fe6a2ab87a7dd792b6d2c Mon Sep 17 00:00:00 2001 From: David Hewitt Date: Wed, 20 Mar 2024 20:00:41 +0000 Subject: [PATCH] fix proto method extract failure for option --- src/impl_/extract_argument.rs | 25 +++++++++++++++++++------ tests/ui/invalid_cancel_handle.stderr | 1 + 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/impl_/extract_argument.rs b/src/impl_/extract_argument.rs index 82285b3d50c..4dcef02c5e6 100644 --- a/src/impl_/extract_argument.rs +++ b/src/impl_/extract_argument.rs @@ -41,14 +41,27 @@ impl<'a, 'py, T: 'py> PyFunctionArgument<'a, 'py> for &'a Bound<'py, T> where T: PyTypeCheck, { - type Holder = Option<&'a Bound<'py, T>>; + type Holder = Option<()>; #[inline] - fn extract( - obj: &'a Bound<'py, PyAny>, - holder: &'a mut Option<&'a Bound<'py, T>>, - ) -> PyResult { - Ok(holder.insert(obj.downcast()?)) + fn extract(obj: &'a Bound<'py, PyAny>, _: &'a mut Option<()>) -> PyResult { + obj.downcast().map_err(Into::into) + } +} + +impl<'a, 'py, T: 'py> PyFunctionArgument<'a, 'py> for Option<&'a Bound<'py, T>> +where + T: PyTypeCheck, +{ + type Holder = (); + + #[inline] + fn extract(obj: &'a Bound<'py, PyAny>, _: &'a mut ()) -> PyResult { + if obj.is_none() { + Ok(None) + } else { + Ok(Some(obj.downcast()?)) + } } } diff --git a/tests/ui/invalid_cancel_handle.stderr b/tests/ui/invalid_cancel_handle.stderr index 9e617bca988..04f55ede4a5 100644 --- a/tests/ui/invalid_cancel_handle.stderr +++ b/tests/ui/invalid_cancel_handle.stderr @@ -58,6 +58,7 @@ error[E0277]: the trait bound `CancelHandle: Clone` is not satisfied | ^^^^ the trait `Clone` is not implemented for `CancelHandle` | = help: the following other types implement trait `PyFunctionArgument<'a, 'py>`: + Option<&'a pyo3::Bound<'py, T>> &'a pyo3::Bound<'py, T> &'a pyo3::coroutine::Coroutine &'a mut pyo3::coroutine::Coroutine