Skip to content

Commit 4ee9e9a

Browse files
author
Andrew J Westlake
committed
Added some missing functions
1 parent 6131e5d commit 4ee9e9a

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

src/async_std.rs

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,40 @@ where
288288
generic::future_into_py_with_loop::<AsyncStdRuntime, F>(event_loop, fut)
289289
}
290290

291+
/// Convert a Rust Future into a Python awaitable
292+
///
293+
/// # Arguments
294+
/// * `event_loop` - The Python event loop that the awaitable should be attached to
295+
/// * `fut` - The Rust future to be converted
296+
///
297+
/// # Examples
298+
///
299+
/// ```
300+
/// use std::time::Duration;
301+
///
302+
/// use pyo3::prelude::*;
303+
///
304+
/// /// Awaitable sleep function
305+
/// #[pyfunction]
306+
/// fn sleep_for<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
307+
/// let secs = secs.extract()?;
308+
/// pyo3_asyncio::async_std::future_into_py_with_locals(
309+
/// py,
310+
/// pyo3_asyncio::async_std::get_current_locals(py)?,
311+
/// async move {
312+
/// async_std::task::sleep(Duration::from_secs(secs)).await;
313+
/// Python::with_gil(|py| Ok(py.None()))
314+
/// }
315+
/// )
316+
/// }
317+
/// ```
318+
pub fn future_into_py_with_locals<F>(py: Python, locals: TaskLocals, fut: F) -> PyResult<&PyAny>
319+
where
320+
F: Future<Output = PyResult<PyObject>> + Send + 'static,
321+
{
322+
generic::future_into_py_with_locals::<AsyncStdRuntime, F>(py, locals, fut)
323+
}
324+
291325
/// Convert a Rust Future into a Python awaitable
292326
///
293327
/// Unlike [`future_into_py_with_loop`], this function will stop the Rust future from running when
@@ -458,6 +492,59 @@ where
458492
generic::local_future_into_py_with_loop::<AsyncStdRuntime, _>(event_loop, fut)
459493
}
460494

495+
/// Convert a `!Send` Rust Future into a Python awaitable
496+
///
497+
/// # Arguments
498+
/// * `event_loop` - The Python event loop that the awaitable should be attached to
499+
/// * `fut` - The Rust future to be converted
500+
///
501+
/// # Examples
502+
///
503+
/// ```
504+
/// use std::{rc::Rc, time::Duration};
505+
///
506+
/// use pyo3::prelude::*;
507+
///
508+
/// /// Awaitable non-send sleep function
509+
/// #[pyfunction]
510+
/// fn sleep_for(py: Python, secs: u64) -> PyResult<&PyAny> {
511+
/// // Rc is non-send so it cannot be passed into pyo3_asyncio::async_std::future_into_py
512+
/// let secs = Rc::new(secs);
513+
/// Ok(pyo3_asyncio::async_std::local_future_into_py_with_locals(
514+
/// py,
515+
/// pyo3_asyncio::async_std::get_current_locals(py)?,
516+
/// async move {
517+
/// async_std::task::sleep(Duration::from_secs(*secs)).await;
518+
/// Python::with_gil(|py| Ok(py.None()))
519+
/// }
520+
/// )?.into())
521+
/// }
522+
///
523+
/// # #[cfg(all(feature = "async-std-runtime", feature = "attributes"))]
524+
/// #[pyo3_asyncio::async_std::main]
525+
/// async fn main() -> PyResult<()> {
526+
/// Python::with_gil(|py| {
527+
/// let py_future = sleep_for(py, 1)?;
528+
/// pyo3_asyncio::async_std::into_future(py_future)
529+
/// })?
530+
/// .await?;
531+
///
532+
/// Ok(())
533+
/// }
534+
/// # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
535+
/// # fn main() {}
536+
/// ```
537+
pub fn local_future_into_py_with_locals<F>(
538+
py: Python,
539+
locals: TaskLocals,
540+
fut: F,
541+
) -> PyResult<&PyAny>
542+
where
543+
F: Future<Output = PyResult<PyObject>> + 'static,
544+
{
545+
generic::local_future_into_py_with_locals::<AsyncStdRuntime, _>(py, locals, fut)
546+
}
547+
461548
/// Convert a `!Send` Rust Future into a Python awaitable
462549
///
463550
/// Unlike [`local_future_into_py_with_loop`], this function will stop the Rust future from running when

src/tokio.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,40 @@ where
304304
generic::future_into_py_with_loop::<TokioRuntime, F>(event_loop, fut)
305305
}
306306

307+
/// Convert a Rust Future into a Python awaitable
308+
///
309+
/// # Arguments
310+
/// * `event_loop` - The Python event loop that the awaitable should be attached to
311+
/// * `fut` - The Rust future to be converted
312+
///
313+
/// # Examples
314+
///
315+
/// ```
316+
/// use std::time::Duration;
317+
///
318+
/// use pyo3::prelude::*;
319+
///
320+
/// /// Awaitable sleep function
321+
/// #[pyfunction]
322+
/// fn sleep_for<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
323+
/// let secs = secs.extract()?;
324+
/// pyo3_asyncio::tokio::future_into_py_with_locals(
325+
/// py,
326+
/// pyo3_asyncio::tokio::get_current_locals(py)?,
327+
/// async move {
328+
/// tokio::time::sleep(Duration::from_secs(secs)).await;
329+
/// Python::with_gil(|py| Ok(py.None()))
330+
/// }
331+
/// )
332+
/// }
333+
/// ```
334+
pub fn future_into_py_with_locals<F>(py: Python, locals: TaskLocals, fut: F) -> PyResult<&PyAny>
335+
where
336+
F: Future<Output = PyResult<PyObject>> + Send + 'static,
337+
{
338+
generic::future_into_py_with_locals::<TokioRuntime, F>(py, locals, fut)
339+
}
340+
307341
/// Convert a Rust Future into a Python awaitable
308342
///
309343
/// Unlike [`future_into_py_with_loop`], this function will stop the Rust future from running when

0 commit comments

Comments
 (0)