Skip to content

Commit 7f38bd6

Browse files
committed
Made return types generic for future_into_py variants
1 parent 97d8c01 commit 7f38bd6

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

pytests/test_async_std_asyncio.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,10 @@ async fn test_other_awaitables() -> PyResult<()> {
135135
#[pyo3_asyncio::async_std::test]
136136
async fn test_panic() -> PyResult<()> {
137137
let fut = Python::with_gil(|py| -> PyResult<_> {
138-
pyo3_asyncio::async_std::into_future(pyo3_asyncio::async_std::future_into_py(py, async {
139-
panic!("this panic was intentional!")
140-
})?)
138+
pyo3_asyncio::async_std::into_future(pyo3_asyncio::async_std::future_into_py::<_, ()>(
139+
py,
140+
async { panic!("this panic was intentional!") },
141+
)?)
141142
})?;
142143

143144
match fut.await {

pytests/tokio_asyncio/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ fn test_local_future_into_py(event_loop: PyObject) -> PyResult<()> {
149149
#[pyo3_asyncio::tokio::test]
150150
async fn test_panic() -> PyResult<()> {
151151
let fut = Python::with_gil(|py| -> PyResult<_> {
152-
pyo3_asyncio::tokio::into_future(pyo3_asyncio::tokio::future_into_py(py, async {
152+
pyo3_asyncio::tokio::into_future(pyo3_asyncio::tokio::future_into_py::<_, ()>(py, async {
153153
panic!("this panic was intentional!")
154154
})?)
155155
})?;

src/async_std.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,11 +266,12 @@ where
266266
/// )
267267
/// }
268268
/// ```
269-
pub fn future_into_py_with_loop<F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
269+
pub fn future_into_py_with_loop<F, T>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
270270
where
271-
F: Future<Output = PyResult<PyObject>> + Send + 'static,
271+
F: Future<Output = PyResult<T>> + Send + 'static,
272+
T: IntoPy<PyObject>,
272273
{
273-
generic::future_into_py_with_loop::<AsyncStdRuntime, F>(event_loop, fut)
274+
generic::future_into_py_with_loop::<AsyncStdRuntime, F, T>(event_loop, fut)
274275
}
275276

276277
/// Convert a Rust Future into a Python awaitable
@@ -296,11 +297,12 @@ where
296297
/// })
297298
/// }
298299
/// ```
299-
pub fn future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
300+
pub fn future_into_py<F, T>(py: Python, fut: F) -> PyResult<&PyAny>
300301
where
301-
F: Future<Output = PyResult<PyObject>> + Send + 'static,
302+
F: Future<Output = PyResult<T>> + Send + 'static,
303+
T: IntoPy<PyObject>,
302304
{
303-
generic::future_into_py::<AsyncStdRuntime, _>(py, fut)
305+
generic::future_into_py::<AsyncStdRuntime, _, T>(py, fut)
304306
}
305307

306308
/// Convert a `!Send` Rust Future into a Python awaitable

src/generic.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ where
146146
R: Runtime,
147147
F: Future<Output = PyResult<()>> + Send + 'static,
148148
{
149-
let coro = future_into_py_with_loop::<R, _>(event_loop, async move {
149+
let coro = future_into_py_with_loop::<R, _, ()>(event_loop, async move {
150150
fut.await?;
151-
Ok(Python::with_gil(|py| py.None()))
151+
Ok(())
152152
})?;
153153

154154
event_loop.call_method1("run_until_complete", (coro,))?;
@@ -446,10 +446,11 @@ where
446446
/// )
447447
/// }
448448
/// ```
449-
pub fn future_into_py_with_loop<R, F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
449+
pub fn future_into_py_with_loop<R, F, T>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
450450
where
451451
R: Runtime,
452-
F: Future<Output = PyResult<PyObject>> + Send + 'static,
452+
F: Future<Output = PyResult<T>> + Send + 'static,
453+
T: IntoPy<PyObject>,
453454
{
454455
let future_rx = create_future(event_loop)?;
455456
let future_tx1 = PyObject::from(future_rx);
@@ -471,8 +472,12 @@ where
471472
return;
472473
}
473474

474-
let _ = set_result(event_loop2.as_ref(py), future_tx1.as_ref(py), result)
475-
.map_err(dump_err(py));
475+
let _ = set_result(
476+
event_loop2.as_ref(py),
477+
future_tx1.as_ref(py),
478+
result.map(|val| val.into_py(py)),
479+
)
480+
.map_err(dump_err(py));
476481
});
477482
})
478483
.await
@@ -575,12 +580,13 @@ where
575580
/// })
576581
/// }
577582
/// ```
578-
pub fn future_into_py<R, F>(py: Python, fut: F) -> PyResult<&PyAny>
583+
pub fn future_into_py<R, F, T>(py: Python, fut: F) -> PyResult<&PyAny>
579584
where
580585
R: Runtime,
581-
F: Future<Output = PyResult<PyObject>> + Send + 'static,
586+
F: Future<Output = PyResult<T>> + Send + 'static,
587+
T: IntoPy<PyObject>,
582588
{
583-
future_into_py_with_loop::<R, F>(get_current_loop::<R>(py)?, fut)
589+
future_into_py_with_loop::<R, F, T>(get_current_loop::<R>(py)?, fut)
584590
}
585591

586592
/// Convert a Rust Future into a Python awaitable with a generic runtime
@@ -668,7 +674,7 @@ where
668674
R: Runtime,
669675
F: Future<Output = PyResult<PyObject>> + Send + 'static,
670676
{
671-
Ok(future_into_py_with_loop::<R, F>(get_event_loop(py), fut)?.into())
677+
Ok(future_into_py_with_loop::<R, F, PyObject>(get_event_loop(py), fut)?.into())
672678
}
673679

674680
/// Convert a `!Send` Rust Future into a Python awaitable with a generic runtime

src/tokio.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -279,11 +279,12 @@ where
279279
/// )
280280
/// }
281281
/// ```
282-
pub fn future_into_py_with_loop<F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
282+
pub fn future_into_py_with_loop<F, T>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
283283
where
284-
F: Future<Output = PyResult<PyObject>> + Send + 'static,
284+
F: Future<Output = PyResult<T>> + Send + 'static,
285+
T: IntoPy<PyObject>,
285286
{
286-
generic::future_into_py_with_loop::<TokioRuntime, F>(event_loop, fut)
287+
generic::future_into_py_with_loop::<TokioRuntime, F, T>(event_loop, fut)
287288
}
288289

289290
/// Convert a Rust Future into a Python awaitable
@@ -309,11 +310,12 @@ where
309310
/// })
310311
/// }
311312
/// ```
312-
pub fn future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
313+
pub fn future_into_py<F, T>(py: Python, fut: F) -> PyResult<&PyAny>
313314
where
314-
F: Future<Output = PyResult<PyObject>> + Send + 'static,
315+
F: Future<Output = PyResult<T>> + Send + 'static,
316+
T: IntoPy<PyObject>,
315317
{
316-
generic::future_into_py::<TokioRuntime, _>(py, fut)
318+
generic::future_into_py::<TokioRuntime, _, T>(py, fut)
317319
}
318320

319321
/// Convert a `!Send` Rust Future into a Python awaitable

0 commit comments

Comments
 (0)