Skip to content

Commit beff641

Browse files
author
Andrew J Westlake
committed
Made returns generic in local_future_into_py variants
1 parent 7f38bd6 commit beff641

File tree

5 files changed

+47
-37
lines changed

5 files changed

+47
-37
lines changed

pytests/test_async_std_asyncio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ fn sleep<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
2727

2828
pyo3_asyncio::async_std::future_into_py(py, async move {
2929
task::sleep(Duration::from_secs(secs)).await;
30-
Python::with_gil(|py| Ok(py.None()))
30+
Ok(())
3131
})
3232
}
3333

pytests/tokio_asyncio/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn sleep<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
2626

2727
pyo3_asyncio::tokio::future_into_py(py, async move {
2828
tokio::time::sleep(Duration::from_secs(secs)).await;
29-
Python::with_gil(|py| Ok(py.None()))
29+
Ok(())
3030
})
3131
}
3232

src/async_std.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ where
261261
/// pyo3_asyncio::async_std::get_current_loop(py)?,
262262
/// async move {
263263
/// async_std::task::sleep(Duration::from_secs(secs)).await;
264-
/// Python::with_gil(|py| Ok(py.None()))
264+
/// Ok(())
265265
/// }
266266
/// )
267267
/// }
@@ -293,7 +293,7 @@ where
293293
/// let secs = secs.extract()?;
294294
/// pyo3_asyncio::async_std::future_into_py(py, async move {
295295
/// async_std::task::sleep(Duration::from_secs(secs)).await;
296-
/// Python::with_gil(|py| Ok(py.None()))
296+
/// Ok(())
297297
/// })
298298
/// }
299299
/// ```
@@ -327,7 +327,7 @@ where
327327
/// pyo3_asyncio::async_std::get_current_loop(py)?,
328328
/// async move {
329329
/// async_std::task::sleep(Duration::from_secs(*secs)).await;
330-
/// Python::with_gil(|py| Ok(py.None()))
330+
/// Ok(())
331331
/// }
332332
/// )?.into())
333333
/// }
@@ -346,11 +346,12 @@ where
346346
/// # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
347347
/// # fn main() {}
348348
/// ```
349-
pub fn local_future_into_py_with_loop<F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
349+
pub fn local_future_into_py_with_loop<F, T>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
350350
where
351-
F: Future<Output = PyResult<PyObject>> + 'static,
351+
F: Future<Output = PyResult<T>> + 'static,
352+
T: IntoPy<PyObject>,
352353
{
353-
generic::local_future_into_py_with_loop::<AsyncStdRuntime, _>(event_loop, fut)
354+
generic::local_future_into_py_with_loop::<AsyncStdRuntime, _, T>(event_loop, fut)
354355
}
355356

356357
/// Convert a `!Send` Rust Future into a Python awaitable
@@ -373,7 +374,7 @@ where
373374
/// let secs = Rc::new(secs);
374375
/// pyo3_asyncio::async_std::local_future_into_py(py, async move {
375376
/// async_std::task::sleep(Duration::from_secs(*secs)).await;
376-
/// Python::with_gil(|py| Ok(py.None()))
377+
/// Ok(())
377378
/// })
378379
/// }
379380
///
@@ -391,11 +392,12 @@ where
391392
/// # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
392393
/// # fn main() {}
393394
/// ```
394-
pub fn local_future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
395+
pub fn local_future_into_py<F, T>(py: Python, fut: F) -> PyResult<&PyAny>
395396
where
396-
F: Future<Output = PyResult<PyObject>> + 'static,
397+
F: Future<Output = PyResult<T>> + 'static,
398+
T: IntoPy<PyObject>,
397399
{
398-
generic::local_future_into_py::<AsyncStdRuntime, _>(py, fut)
400+
generic::local_future_into_py::<AsyncStdRuntime, _, T>(py, fut)
399401
}
400402

401403
/// Convert a Python `awaitable` into a Rust Future

src/generic.rs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -437,11 +437,11 @@ where
437437
/// #[pyfunction]
438438
/// fn sleep_for<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
439439
/// let secs = secs.extract()?;
440-
/// pyo3_asyncio::generic::future_into_py_with_loop::<MyCustomRuntime, _>(
440+
/// pyo3_asyncio::generic::future_into_py_with_loop::<MyCustomRuntime, _, _>(
441441
/// pyo3_asyncio::generic::get_current_loop::<MyCustomRuntime>(py)?,
442442
/// async move {
443443
/// MyCustomRuntime::sleep(Duration::from_secs(secs)).await;
444-
/// Python::with_gil(|py| Ok(py.None()))
444+
/// Ok(())
445445
/// }
446446
/// )
447447
/// }
@@ -574,9 +574,9 @@ where
574574
/// #[pyfunction]
575575
/// fn sleep_for<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
576576
/// let secs = secs.extract()?;
577-
/// pyo3_asyncio::generic::future_into_py::<MyCustomRuntime, _>(py, async move {
577+
/// pyo3_asyncio::generic::future_into_py::<MyCustomRuntime, _, _>(py, async move {
578578
/// MyCustomRuntime::sleep(Duration::from_secs(secs)).await;
579-
/// Python::with_gil(|py| Ok(py.None()))
579+
/// Ok(())
580580
/// })
581581
/// }
582582
/// ```
@@ -764,19 +764,20 @@ where
764764
/// // Rc is !Send so it cannot be passed into pyo3_asyncio::generic::future_into_py
765765
/// let secs = Rc::new(secs);
766766
///
767-
/// pyo3_asyncio::generic::local_future_into_py_with_loop::<MyCustomRuntime, _>(
767+
/// pyo3_asyncio::generic::local_future_into_py_with_loop::<MyCustomRuntime, _, _>(
768768
/// pyo3_asyncio::get_running_loop(py)?,
769769
/// async move {
770770
/// MyCustomRuntime::sleep(Duration::from_secs(*secs)).await;
771-
/// Python::with_gil(|py| Ok(py.None()))
771+
/// Ok(())
772772
/// }
773773
/// )
774774
/// }
775775
/// ```
776-
pub fn local_future_into_py_with_loop<R, F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
776+
pub fn local_future_into_py_with_loop<R, F, T>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
777777
where
778778
R: SpawnLocalExt,
779-
F: Future<Output = PyResult<PyObject>> + 'static,
779+
F: Future<Output = PyResult<T>> + 'static,
780+
T: IntoPy<PyObject>,
780781
{
781782
let future_rx = create_future(event_loop)?;
782783
let future_tx1 = PyObject::from(future_rx);
@@ -798,8 +799,12 @@ where
798799
return;
799800
}
800801

801-
let _ = set_result(event_loop2.as_ref(py), future_tx1.as_ref(py), result)
802-
.map_err(dump_err(py));
802+
let _ = set_result(
803+
event_loop2.as_ref(py),
804+
future_tx1.as_ref(py),
805+
result.map(|val| val.into_py(py)),
806+
)
807+
.map_err(dump_err(py));
803808
});
804809
})
805810
.await
@@ -914,16 +919,17 @@ where
914919
/// // Rc is !Send so it cannot be passed into pyo3_asyncio::generic::future_into_py
915920
/// let secs = Rc::new(secs);
916921
///
917-
/// pyo3_asyncio::generic::local_future_into_py::<MyCustomRuntime, _>(py, async move {
922+
/// pyo3_asyncio::generic::local_future_into_py::<MyCustomRuntime, _, _>(py, async move {
918923
/// MyCustomRuntime::sleep(Duration::from_secs(*secs)).await;
919-
/// Python::with_gil(|py| Ok(py.None()))
924+
/// Ok(())
920925
/// })
921926
/// }
922927
/// ```
923-
pub fn local_future_into_py<R, F>(py: Python, fut: F) -> PyResult<&PyAny>
928+
pub fn local_future_into_py<R, F, T>(py: Python, fut: F) -> PyResult<&PyAny>
924929
where
925930
R: SpawnLocalExt,
926-
F: Future<Output = PyResult<PyObject>> + 'static,
931+
F: Future<Output = PyResult<T>> + 'static,
932+
T: IntoPy<PyObject>,
927933
{
928-
local_future_into_py_with_loop::<R, F>(get_current_loop::<R>(py)?, fut)
934+
local_future_into_py_with_loop::<R, F, T>(get_current_loop::<R>(py)?, fut)
929935
}

src/tokio.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ where
274274
/// pyo3_asyncio::tokio::get_current_loop(py)?,
275275
/// async move {
276276
/// tokio::time::sleep(Duration::from_secs(secs)).await;
277-
/// Python::with_gil(|py| Ok(py.None()))
277+
/// Ok(())
278278
/// }
279279
/// )
280280
/// }
@@ -306,7 +306,7 @@ where
306306
/// let secs = secs.extract()?;
307307
/// pyo3_asyncio::tokio::future_into_py(py, async move {
308308
/// tokio::time::sleep(Duration::from_secs(secs)).await;
309-
/// Python::with_gil(|py| Ok(py.None()))
309+
/// Ok(())
310310
/// })
311311
/// }
312312
/// ```
@@ -341,7 +341,7 @@ where
341341
/// pyo3_asyncio::tokio::get_current_loop(py)?,
342342
/// async move {
343343
/// tokio::time::sleep(Duration::from_secs(*secs)).await;
344-
/// Python::with_gil(|py| Ok(py.None()))
344+
/// Ok(())
345345
/// }
346346
/// )
347347
/// }
@@ -375,11 +375,12 @@ where
375375
/// # #[cfg(not(all(feature = "tokio-runtime", feature = "attributes")))]
376376
/// # fn main() {}
377377
/// ```
378-
pub fn local_future_into_py_with_loop<'p, F>(event_loop: &'p PyAny, fut: F) -> PyResult<&PyAny>
378+
pub fn local_future_into_py_with_loop<'p, F, T>(event_loop: &'p PyAny, fut: F) -> PyResult<&PyAny>
379379
where
380-
F: Future<Output = PyResult<PyObject>> + 'static,
380+
F: Future<Output = PyResult<T>> + 'static,
381+
T: IntoPy<PyObject>,
381382
{
382-
generic::local_future_into_py_with_loop::<TokioRuntime, _>(event_loop, fut)
383+
generic::local_future_into_py_with_loop::<TokioRuntime, _, T>(event_loop, fut)
383384
}
384385

385386
/// Convert a `!Send` Rust Future into a Python awaitable
@@ -402,7 +403,7 @@ where
402403
/// let secs = Rc::new(secs);
403404
/// pyo3_asyncio::tokio::local_future_into_py(py, async move {
404405
/// tokio::time::sleep(Duration::from_secs(*secs)).await;
405-
/// Python::with_gil(|py| Ok(py.None()))
406+
/// Ok(())
406407
/// })
407408
/// }
408409
///
@@ -435,11 +436,12 @@ where
435436
/// # #[cfg(not(all(feature = "tokio-runtime", feature = "attributes")))]
436437
/// # fn main() {}
437438
/// ```
438-
pub fn local_future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
439+
pub fn local_future_into_py<F, T>(py: Python, fut: F) -> PyResult<&PyAny>
439440
where
440-
F: Future<Output = PyResult<PyObject>> + 'static,
441+
F: Future<Output = PyResult<T>> + 'static,
442+
T: IntoPy<PyObject>,
441443
{
442-
generic::local_future_into_py::<TokioRuntime, _>(py, fut)
444+
generic::local_future_into_py::<TokioRuntime, _, T>(py, fut)
443445
}
444446

445447
/// Convert a Python `awaitable` into a Rust Future

0 commit comments

Comments
 (0)