Skip to content

Commit eb0870e

Browse files
author
Andrew J Westlake
committed
Merged in #43
2 parents 6e80625 + 3876cc6 commit eb0870e

File tree

6 files changed

+146
-100
lines changed

6 files changed

+146
-100
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ use pyo3::{prelude::*, wrap_pyfunction};
142142
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
143143
pyo3_asyncio::async_std::future_into_py(py, async {
144144
async_std::task::sleep(std::time::Duration::from_secs(1)).await;
145-
Ok(Python::with_gil(|py| py.None()))
145+
Ok(())
146146
})
147147
}
148148

@@ -166,7 +166,7 @@ use pyo3::{prelude::*, wrap_pyfunction};
166166
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
167167
pyo3_asyncio::tokio::future_into_py(py, async {
168168
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
169-
Ok(Python::with_gil(|py| py.None()))
169+
Ok(())
170170
})
171171
}
172172

@@ -283,7 +283,7 @@ async fn rust_sleep() {
283283
fn call_rust_sleep(py: Python) -> PyResult<&PyAny> {
284284
pyo3_asyncio::async_std::future_into_py(py, async move {
285285
rust_sleep().await;
286-
Ok(Python::with_gil(|py| py.None()))
286+
Ok(())
287287
})
288288
}
289289
```
@@ -433,7 +433,7 @@ use pyo3::{prelude::*, wrap_pyfunction};
433433
fn rust_sleep(py: Python) -> PyResult<&PyAny> {
434434
pyo3_asyncio::tokio::future_into_py(py, async {
435435
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
436-
Ok(Python::with_gil(|py| py.None()))
436+
Ok(())
437437
})
438438
}
439439

pytests/test_async_std_asyncio.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn sleep<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
2121

2222
pyo3_asyncio::async_std::future_into_py(py, async move {
2323
task::sleep(Duration::from_secs(secs)).await;
24-
Python::with_gil(|py| Ok(py.None()))
24+
Ok(())
2525
})
2626
}
2727

@@ -92,9 +92,10 @@ async fn test_other_awaitables() -> PyResult<()> {
9292
#[pyo3_asyncio::async_std::test]
9393
async fn test_panic() -> PyResult<()> {
9494
let fut = Python::with_gil(|py| -> PyResult<_> {
95-
pyo3_asyncio::async_std::into_future(pyo3_asyncio::async_std::future_into_py(py, async {
96-
panic!("this panic was intentional!")
97-
})?)
95+
pyo3_asyncio::async_std::into_future(pyo3_asyncio::async_std::future_into_py::<_, ()>(
96+
py,
97+
async { panic!("this panic was intentional!") },
98+
)?)
9899
})?;
99100

100101
match fut.await {

pytests/tokio_asyncio/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn sleep<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
2020

2121
pyo3_asyncio::tokio::future_into_py(py, async move {
2222
tokio::time::sleep(Duration::from_secs(secs)).await;
23-
Python::with_gil(|py| Ok(py.None()))
23+
Ok(())
2424
})
2525
}
2626

@@ -113,7 +113,7 @@ fn test_local_future_into_py(event_loop: PyObject) -> PyResult<()> {
113113
#[pyo3_asyncio::tokio::test]
114114
async fn test_panic() -> PyResult<()> {
115115
let fut = Python::with_gil(|py| -> PyResult<_> {
116-
pyo3_asyncio::tokio::into_future(pyo3_asyncio::tokio::future_into_py(py, async {
116+
pyo3_asyncio::tokio::into_future(pyo3_asyncio::tokio::future_into_py::<_, ()>(py, async {
117117
panic!("this panic was intentional!")
118118
})?)
119119
})?;

src/async_std.rs

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,12 @@ pub fn get_current_locals(py: Python) -> PyResult<TaskLocals> {
165165
/// # Ok(())
166166
/// # }).unwrap();
167167
/// ```
168-
pub fn run_until_complete<F>(event_loop: &PyAny, fut: F) -> PyResult<()>
168+
pub fn run_until_complete<F, T>(event_loop: &PyAny, fut: F) -> PyResult<T>
169169
where
170-
F: Future<Output = PyResult<()>> + Send + 'static,
170+
F: Future<Output = PyResult<T>> + Send + 'static,
171+
T: Send + Sync + 'static,
171172
{
172-
generic::run_until_complete::<AsyncStdRuntime, _>(event_loop, fut)
173+
generic::run_until_complete::<AsyncStdRuntime, _, T>(event_loop, fut)
173174
}
174175

175176
/// Run the event loop until the given Future completes
@@ -201,11 +202,12 @@ where
201202
/// })
202203
/// }
203204
/// ```
204-
pub fn run<F>(py: Python, fut: F) -> PyResult<()>
205+
pub fn run<F, T>(py: Python, fut: F) -> PyResult<T>
205206
where
206-
F: Future<Output = PyResult<()>> + Send + 'static,
207+
F: Future<Output = PyResult<T>> + Send + 'static,
208+
T: Send + Sync + 'static,
207209
{
208-
generic::run::<AsyncStdRuntime, F>(py, fut)
210+
generic::run::<AsyncStdRuntime, F, T>(py, fut)
209211
}
210212

211213
/// Convert a Rust Future into a Python awaitable
@@ -273,11 +275,12 @@ where
273275
/// )
274276
/// }
275277
/// ```
276-
pub fn future_into_py_with_locals<F>(py: Python, locals: TaskLocals, fut: F) -> PyResult<&PyAny>
278+
pub fn future_into_py_with_locals<F, T>(py: Python, locals: TaskLocals, fut: F) -> PyResult<&PyAny>
277279
where
278-
F: Future<Output = PyResult<PyObject>> + Send + 'static,
280+
F: Future<Output = PyResult<T>> + Send + 'static,
281+
T: IntoPy<PyObject>,
279282
{
280-
generic::future_into_py_with_locals::<AsyncStdRuntime, F>(py, locals, fut)
283+
generic::future_into_py_with_locals::<AsyncStdRuntime, F, T>(py, locals, fut)
281284
}
282285

283286
/// Convert a Rust Future into a Python awaitable
@@ -308,7 +311,7 @@ where
308311
/// pyo3_asyncio::async_std::get_current_loop(py)?,
309312
/// async move {
310313
/// async_std::task::sleep(Duration::from_secs(secs)).await;
311-
/// Python::with_gil(|py| Ok(py.None()))
314+
/// Ok(Python::with_gil(|py| py.None()))
312315
/// }
313316
/// )
314317
/// }
@@ -344,15 +347,16 @@ where
344347
/// let secs = secs.extract()?;
345348
/// pyo3_asyncio::async_std::future_into_py(py, async move {
346349
/// async_std::task::sleep(Duration::from_secs(secs)).await;
347-
/// Python::with_gil(|py| Ok(py.None()))
350+
/// Ok(())
348351
/// })
349352
/// }
350353
/// ```
351-
pub fn future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
354+
pub fn future_into_py<F, T>(py: Python, fut: F) -> PyResult<&PyAny>
352355
where
353-
F: Future<Output = PyResult<PyObject>> + Send + 'static,
356+
F: Future<Output = PyResult<T>> + Send + 'static,
357+
T: IntoPy<PyObject>,
354358
{
355-
generic::future_into_py::<AsyncStdRuntime, _>(py, fut)
359+
generic::future_into_py::<AsyncStdRuntime, _, T>(py, fut)
356360
}
357361

358362
/// Convert a Rust Future into a Python awaitable
@@ -419,7 +423,7 @@ where
419423
/// pyo3_asyncio::async_std::get_current_loop(py)?,
420424
/// async move {
421425
/// async_std::task::sleep(Duration::from_secs(*secs)).await;
422-
/// Python::with_gil(|py| Ok(py.None()))
426+
/// Ok(Python::with_gil(|py| py.None()))
423427
/// }
424428
/// )?.into())
425429
/// }
@@ -492,15 +496,16 @@ where
492496
/// # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
493497
/// # fn main() {}
494498
/// ```
495-
pub fn local_future_into_py_with_locals<F>(
499+
pub fn local_future_into_py_with_locals<F, T>(
496500
py: Python,
497501
locals: TaskLocals,
498502
fut: F,
499503
) -> PyResult<&PyAny>
500504
where
501-
F: Future<Output = PyResult<PyObject>> + 'static,
505+
F: Future<Output = PyResult<T>> + 'static,
506+
T: IntoPy<PyObject>,
502507
{
503-
generic::local_future_into_py_with_locals::<AsyncStdRuntime, _>(py, locals, fut)
508+
generic::local_future_into_py_with_locals::<AsyncStdRuntime, _, T>(py, locals, fut)
504509
}
505510

506511
/// Convert a `!Send` Rust Future into a Python awaitable
@@ -583,7 +588,7 @@ where
583588
/// let secs = Rc::new(secs);
584589
/// pyo3_asyncio::async_std::local_future_into_py(py, async move {
585590
/// async_std::task::sleep(Duration::from_secs(*secs)).await;
586-
/// Python::with_gil(|py| Ok(py.None()))
591+
/// Ok(())
587592
/// })
588593
/// }
589594
///
@@ -601,11 +606,12 @@ where
601606
/// # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
602607
/// # fn main() {}
603608
/// ```
604-
pub fn local_future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
609+
pub fn local_future_into_py<F, T>(py: Python, fut: F) -> PyResult<&PyAny>
605610
where
606-
F: Future<Output = PyResult<PyObject>> + 'static,
611+
F: Future<Output = PyResult<T>> + 'static,
612+
T: IntoPy<PyObject>,
607613
{
608-
generic::local_future_into_py::<AsyncStdRuntime, _>(py, fut)
614+
generic::local_future_into_py::<AsyncStdRuntime, _, T>(py, fut)
609615
}
610616

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

0 commit comments

Comments
 (0)