Skip to content

Commit e90eeef

Browse files
author
Andrew J Westlake
committed
Made returns generic for run and run_until_complete variants
1 parent beff641 commit e90eeef

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

src/async_std.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,12 @@ pub fn get_current_loop(py: Python) -> PyResult<&PyAny> {
161161
/// # .unwrap();
162162
/// # });
163163
/// ```
164-
pub fn run_until_complete<F>(event_loop: &PyAny, fut: F) -> PyResult<()>
164+
pub fn run_until_complete<F, T>(event_loop: &PyAny, fut: F) -> PyResult<T>
165165
where
166-
F: Future<Output = PyResult<()>> + Send + 'static,
166+
F: Future<Output = PyResult<T>> + Send + 'static,
167+
T: Send + Sync + 'static,
167168
{
168-
generic::run_until_complete::<AsyncStdRuntime, _>(event_loop, fut)
169+
generic::run_until_complete::<AsyncStdRuntime, _, T>(event_loop, fut)
169170
}
170171

171172
/// Run the event loop until the given Future completes
@@ -197,11 +198,12 @@ where
197198
/// })
198199
/// }
199200
/// ```
200-
pub fn run<F>(py: Python, fut: F) -> PyResult<()>
201+
pub fn run<F, T>(py: Python, fut: F) -> PyResult<T>
201202
where
202-
F: Future<Output = PyResult<()>> + Send + 'static,
203+
F: Future<Output = PyResult<T>> + Send + 'static,
204+
T: Send + Sync + 'static,
203205
{
204-
generic::run::<AsyncStdRuntime, F>(py, fut)
206+
generic::run::<AsyncStdRuntime, F, T>(py, fut)
205207
}
206208

207209
/// Convert a Rust Future into a Python awaitable

src/generic.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use std::{future::Future, pin::Pin};
1+
use std::{
2+
future::Future,
3+
pin::Pin,
4+
sync::{Arc, Mutex},
5+
};
26

37
use pyo3::{prelude::*, PyNativeType};
48

@@ -129,7 +133,7 @@ where
129133
/// # pyo3_asyncio::with_runtime(py, || {
130134
/// # let event_loop = py.import("asyncio")?.call_method0("new_event_loop")?;
131135
/// # #[cfg(feature = "tokio-runtime")]
132-
/// pyo3_asyncio::generic::run_until_complete::<MyCustomRuntime, _>(event_loop, async move {
136+
/// pyo3_asyncio::generic::run_until_complete::<MyCustomRuntime, _, _>(event_loop, async move {
133137
/// tokio::time::sleep(Duration::from_secs(1)).await;
134138
/// Ok(())
135139
/// })?;
@@ -141,19 +145,26 @@ where
141145
/// # .unwrap();
142146
/// # });
143147
/// ```
144-
pub fn run_until_complete<R, F>(event_loop: &PyAny, fut: F) -> PyResult<()>
148+
pub fn run_until_complete<R, F, T>(event_loop: &PyAny, fut: F) -> PyResult<T>
145149
where
146150
R: Runtime,
147-
F: Future<Output = PyResult<()>> + Send + 'static,
151+
F: Future<Output = PyResult<T>> + Send + 'static,
152+
T: Send + Sync + 'static,
148153
{
154+
let result_tx = Arc::new(Mutex::new(None));
155+
let result_rx = Arc::clone(&result_tx);
149156
let coro = future_into_py_with_loop::<R, _, ()>(event_loop, async move {
150-
fut.await?;
157+
let val = fut.await?;
158+
if let Ok(mut result) = result_tx.lock() {
159+
*result = Some(val);
160+
}
151161
Ok(())
152162
})?;
153163

154164
event_loop.call_method1("run_until_complete", (coro,))?;
155165

156-
Ok(())
166+
let result = result_rx.lock().unwrap().take().unwrap();
167+
Ok(result)
157168
}
158169

159170
/// Run the event loop until the given Future completes
@@ -218,7 +229,7 @@ where
218229
/// #
219230
/// fn main() {
220231
/// Python::with_gil(|py| {
221-
/// pyo3_asyncio::generic::run::<MyCustomRuntime, _>(py, async move {
232+
/// pyo3_asyncio::generic::run::<MyCustomRuntime, _, _>(py, async move {
222233
/// custom_sleep(Duration::from_secs(1)).await;
223234
/// Ok(())
224235
/// })
@@ -229,14 +240,15 @@ where
229240
/// })
230241
/// }
231242
/// ```
232-
pub fn run<R, F>(py: Python, fut: F) -> PyResult<()>
243+
pub fn run<R, F, T>(py: Python, fut: F) -> PyResult<T>
233244
where
234245
R: Runtime,
235-
F: Future<Output = PyResult<()>> + Send + 'static,
246+
F: Future<Output = PyResult<T>> + Send + 'static,
247+
T: Send + Sync + 'static,
236248
{
237249
let event_loop = asyncio(py)?.call_method0("new_event_loop")?;
238250

239-
let result = run_until_complete::<R, F>(event_loop, fut);
251+
let result = run_until_complete::<R, F, T>(event_loop, fut);
240252

241253
close(event_loop)?;
242254

src/tokio.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,12 @@ fn multi_thread() -> Builder {
178178
/// # .unwrap();
179179
/// # });
180180
/// ```
181-
pub fn run_until_complete<F>(event_loop: &PyAny, fut: F) -> PyResult<()>
181+
pub fn run_until_complete<F, T>(event_loop: &PyAny, fut: F) -> PyResult<T>
182182
where
183-
F: Future<Output = PyResult<()>> + Send + 'static,
183+
F: Future<Output = PyResult<T>> + Send + 'static,
184+
T: Send + Sync + 'static,
184185
{
185-
generic::run_until_complete::<TokioRuntime, _>(event_loop, fut)
186+
generic::run_until_complete::<TokioRuntime, _, T>(event_loop, fut)
186187
}
187188

188189
/// Run the event loop until the given Future completes
@@ -211,11 +212,12 @@ where
211212
/// })
212213
/// }
213214
/// ```
214-
pub fn run<F>(py: Python, fut: F) -> PyResult<()>
215+
pub fn run<F, T>(py: Python, fut: F) -> PyResult<T>
215216
where
216-
F: Future<Output = PyResult<()>> + Send + 'static,
217+
F: Future<Output = PyResult<T>> + Send + 'static,
218+
T: Send + Sync + 'static,
217219
{
218-
generic::run::<TokioRuntime, F>(py, fut)
220+
generic::run::<TokioRuntime, F, T>(py, fut)
219221
}
220222

221223
/// Convert a Rust Future into a Python awaitable

0 commit comments

Comments
 (0)