diff --git a/src/async_std.rs b/src/async_std.rs index 1bd868a..16930ad 100644 --- a/src/async_std.rs +++ b/src/async_std.rs @@ -272,17 +272,16 @@ where /// ) /// } /// ``` -pub fn future_into_py_with_locals( +pub fn future_into_py_with_locals( py: Python, locals: TaskLocals, fut: F, ) -> PyResult> where F: Future> + Send + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { - generic::future_into_py_with_locals::(py, locals, fut) + generic::future_into_py_with_locals::(py, locals, fut) } /// Convert a Rust Future into a Python awaitable @@ -323,13 +322,12 @@ where /// }) /// } /// ``` -pub fn future_into_py(py: Python, fut: F) -> PyResult> +pub fn future_into_py(py: Python, fut: F) -> PyResult> where F: Future> + Send + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { - generic::future_into_py::(py, fut) + generic::future_into_py::(py, fut) } /// Convert a `!Send` Rust Future into a Python awaitable @@ -395,17 +393,16 @@ where note = "Questionable whether these conversions have real-world utility (see https://github.com/awestlake87/pyo3-asyncio/issues/59#issuecomment-1008038497 and let me know if you disagree!)" )] #[allow(deprecated)] -pub fn local_future_into_py_with_locals( +pub fn local_future_into_py_with_locals( py: Python, locals: TaskLocals, fut: F, ) -> PyResult> where F: Future> + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { - generic::local_future_into_py_with_locals::(py, locals, fut) + generic::local_future_into_py_with_locals::(py, locals, fut) } /// Convert a `!Send` Rust Future into a Python awaitable @@ -466,13 +463,12 @@ where note = "Questionable whether these conversions have real-world utility (see https://github.com/awestlake87/pyo3-asyncio/issues/59#issuecomment-1008038497 and let me know if you disagree!)" )] #[allow(deprecated)] -pub fn local_future_into_py(py: Python, fut: F) -> PyResult> +pub fn local_future_into_py(py: Python, fut: F) -> PyResult> where F: Future> + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { - generic::local_future_into_py::(py, fut) + generic::local_future_into_py::(py, fut) } /// Convert a Python `awaitable` into a Rust Future diff --git a/src/generic.rs b/src/generic.rs index ed06302..0579146 100644 --- a/src/generic.rs +++ b/src/generic.rs @@ -201,7 +201,7 @@ where let py = event_loop.py(); let result_tx = Arc::new(Mutex::new(None)); let result_rx = Arc::clone(&result_tx); - let coro = future_into_py_with_locals::( + let coro = future_into_py_with_locals::( py, TaskLocals::new(event_loop.clone()).copy_context(py)?, async move { @@ -578,7 +578,7 @@ where /// } /// ``` #[allow(unused_must_use)] -pub fn future_into_py_with_locals( +pub fn future_into_py_with_locals( py: Python, locals: TaskLocals, fut: F, @@ -586,8 +586,7 @@ pub fn future_into_py_with_locals( where R: Runtime + ContextExt, F: Future> + Send + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { let (cancel_tx, cancel_rx) = oneshot::channel(); @@ -623,8 +622,9 @@ where let _ = set_result( &locals2.event_loop(py), future_tx1.bind(py), - result.and_then(|val| { - Ok(val.into_pyobject(py).map_err(Into::into)?.unbind().into()) + result.and_then(|val| match val.into_pyobject(py) { + Ok(obj) => Ok(obj.into_any().unbind()), + Err(err) => Err(err.into()), }), ) .map_err(dump_err(py)); @@ -848,14 +848,13 @@ impl PyDoneCallback { /// }) /// } /// ``` -pub fn future_into_py(py: Python, fut: F) -> PyResult> +pub fn future_into_py(py: Python, fut: F) -> PyResult> where R: Runtime + ContextExt, F: Future> + Send + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { - future_into_py_with_locals::(py, get_current_locals::(py)?, fut) + future_into_py_with_locals::(py, get_current_locals::(py)?, fut) } /// Convert a `!Send` Rust Future into a Python awaitable with a generic runtime and manual @@ -987,7 +986,7 @@ where note = "Questionable whether these conversions have real-world utility (see https://github.com/awestlake87/pyo3-asyncio/issues/59#issuecomment-1008038497 and let me know if you disagree!)" )] #[allow(unused_must_use)] -pub fn local_future_into_py_with_locals( +pub fn local_future_into_py_with_locals( py: Python, locals: TaskLocals, fut: F, @@ -995,8 +994,7 @@ pub fn local_future_into_py_with_locals( where R: Runtime + SpawnLocalExt + LocalContextExt, F: Future> + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { let (cancel_tx, cancel_rx) = oneshot::channel(); @@ -1032,8 +1030,9 @@ where let _ = set_result( locals2.event_loop.bind(py), future_tx1.bind(py), - result.and_then(|val| { - Ok(val.into_pyobject(py).map_err(Into::into)?.unbind().into()) + result.and_then(|val| match val.into_pyobject(py) { + Ok(obj) => Ok(obj.into_any().unbind()), + Err(err) => Err(err.into()), }), ) .map_err(dump_err(py)); @@ -1191,14 +1190,13 @@ where note = "Questionable whether these conversions have real-world utility (see https://github.com/awestlake87/pyo3-asyncio/issues/59#issuecomment-1008038497 and let me know if you disagree!)" )] #[allow(deprecated)] -pub fn local_future_into_py(py: Python, fut: F) -> PyResult> +pub fn local_future_into_py(py: Python, fut: F) -> PyResult> where R: Runtime + ContextExt + SpawnLocalExt + LocalContextExt, F: Future> + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { - local_future_into_py_with_locals::(py, get_current_locals::(py)?, fut) + local_future_into_py_with_locals::(py, get_current_locals::(py)?, fut) } /// unstable-streams Convert an async generator into a stream @@ -1480,34 +1478,34 @@ where { fn send(&mut self, py: Python, locals: TaskLocals, item: PyObject) -> PyResult { match self.tx.try_send(item.clone_ref(py)) { - Ok(_) => Ok(true.into_pyobject(py)?.as_ref().unbind()), + Ok(_) => Ok(true.into_pyobject(py)?.into_any().unbind()), Err(e) => { if e.is_full() { let mut tx = self.tx.clone(); Python::with_gil(move |py| { - Ok(future_into_py_with_locals::( - py, - locals, - async move { + Ok( + future_into_py_with_locals::(py, locals, async move { if tx.flush().await.is_err() { // receiving side disconnected return Python::with_gil(|py| { - Ok(false.into_pyobject(py)?.as_ref().unbind()) + Ok(false.into_pyobject(py)?.into_any().unbind()) }); } if tx.send(item).await.is_err() { // receiving side disconnected return Python::with_gil(|py| { - Ok(false.into_pyobject(py)?.as_ref().unbind()) + Ok(false.into_pyobject(py)?.into_any().unbind()) }); } - Python::with_gil(|py| Ok(true.into_pyobject(py)?.as_ref().unbind())) - }, - )? - .into()) + Python::with_gil(|py| { + Ok(true.into_pyobject(py)?.into_any().unbind()) + }) + })? + .into(), + ) }) } else { - Ok(false.into_pyobject(py)?.as_ref().unbind()) + Ok(false.into_pyobject(py)?.into_any().unbind()) } } } diff --git a/src/tokio.rs b/src/tokio.rs index 452f640..69f6c63 100644 --- a/src/tokio.rs +++ b/src/tokio.rs @@ -315,17 +315,16 @@ where /// ) /// } /// ``` -pub fn future_into_py_with_locals( +pub fn future_into_py_with_locals( py: Python, locals: TaskLocals, fut: F, ) -> PyResult> where F: Future> + Send + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { - generic::future_into_py_with_locals::(py, locals, fut) + generic::future_into_py_with_locals::(py, locals, fut) } /// Convert a Rust Future into a Python awaitable @@ -366,13 +365,12 @@ where /// }) /// } /// ``` -pub fn future_into_py(py: Python, fut: F) -> PyResult> +pub fn future_into_py(py: Python, fut: F) -> PyResult> where F: Future> + Send + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { - generic::future_into_py::(py, fut) + generic::future_into_py::(py, fut) } /// Convert a `!Send` Rust Future into a Python awaitable @@ -454,17 +452,16 @@ where note = "Questionable whether these conversions have real-world utility (see https://github.com/awestlake87/pyo3-asyncio/issues/59#issuecomment-1008038497 and let me know if you disagree!)" )] #[allow(deprecated)] -pub fn local_future_into_py_with_locals( +pub fn local_future_into_py_with_locals( py: Python, locals: TaskLocals, fut: F, ) -> PyResult> where F: Future> + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { - generic::local_future_into_py_with_locals::(py, locals, fut) + generic::local_future_into_py_with_locals::(py, locals, fut) } /// Convert a `!Send` Rust Future into a Python awaitable @@ -540,13 +537,12 @@ where note = "Questionable whether these conversions have real-world utility (see https://github.com/awestlake87/pyo3-asyncio/issues/59#issuecomment-1008038497 and let me know if you disagree!)" )] #[allow(deprecated)] -pub fn local_future_into_py(py: Python, fut: F) -> PyResult> +pub fn local_future_into_py(py: Python, fut: F) -> PyResult> where F: Future> + 'static, - T: for<'py> IntoPyObject<'py, Target = P>, - P: AsRef, + T: for<'py> IntoPyObject<'py>, { - generic::local_future_into_py::(py, fut) + generic::local_future_into_py::(py, fut) } /// Convert a Python `awaitable` into a Rust Future