Skip to content

Commit b56f111

Browse files
author
Andrew J Westlake
committed
Finished adding missing local_cancellable_* functions to generic module
1 parent 4a4655a commit b56f111

File tree

1 file changed

+146
-5
lines changed

1 file changed

+146
-5
lines changed

src/generic.rs

Lines changed: 146 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,10 @@ impl PyDoneCallback {
590590
/// Unlike [`future_into_py_with_loop`], this function will stop the Rust future from running when
591591
/// the `asyncio.Future` is cancelled from Python.
592592
///
593+
/// __This function will be deprecated in favor of [`future_into_py_with_loop`] in `v0.15` because
594+
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
595+
/// replaced with [`future_into_py_with_loop`].__
596+
///
593597
/// # Arguments
594598
/// * `event_loop` - The Python event loop that the awaitable should be attached to
595599
/// * `fut` - The Rust future to be converted
@@ -776,6 +780,10 @@ where
776780
/// Unlike [`future_into_py`], this function will stop the Rust future from running when the
777781
/// `asyncio.Future` is cancelled from Python.
778782
///
783+
/// __This function will be deprecated in favor of [`future_into_py`] in `v0.15` because
784+
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
785+
/// replaced with [`future_into_py`].__
786+
///
779787
/// # Arguments
780788
/// * `py` - The current PyO3 GIL guard
781789
/// * `fut` - The Rust future to be converted
@@ -1097,9 +1105,13 @@ where
10971105

10981106
/// Convert a `!Send` Rust Future into a Python awaitable with a generic runtime
10991107
///
1100-
/// Unlike [`local_future_into_py_with_loop`], this function will stop the Rust future from running
1108+
/// Unlike [`local_future_into_py_with_loop`], this function will stop the Rust future from running
11011109
/// when the `asyncio.Future` is cancelled from Python.
11021110
///
1111+
/// __This function will be deprecated in favor of [`local_future_into_py_with_loop`] in `v0.15` because
1112+
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
1113+
/// replaced with [`local_future_into_py_with_loop`].__
1114+
///
11031115
/// # Arguments
11041116
/// * `event_loop` - The Python event loop that the awaitable should be attached to
11051117
/// * `fut` - The Rust future to be converted
@@ -1109,7 +1121,7 @@ where
11091121
/// ```no_run
11101122
/// # use std::{task::{Context, Poll}, pin::Pin, future::Future};
11111123
/// #
1112-
/// # use pyo3_asyncio::generic::{JoinError, Runtime};
1124+
/// # use pyo3_asyncio::generic::{JoinError, SpawnLocalExt, Runtime};
11131125
/// #
11141126
/// # struct MyCustomJoinError;
11151127
/// #
@@ -1159,13 +1171,29 @@ where
11591171
/// # }
11601172
/// # }
11611173
/// #
1162-
/// use std::time::Duration;
1174+
/// # impl SpawnLocalExt for MyCustomRuntime {
1175+
/// # fn scope_local<F, R>(_event_loop: PyObject, fut: F) -> Pin<Box<dyn Future<Output = R>>>
1176+
/// # where
1177+
/// # F: Future<Output = R> + 'static
1178+
/// # {
1179+
/// # unreachable!()
1180+
/// # }
1181+
/// #
1182+
/// # fn spawn_local<F>(fut: F) -> Self::JoinHandle
1183+
/// # where
1184+
/// # F: Future<Output = ()> + 'static
1185+
/// # {
1186+
/// # unreachable!()
1187+
/// # }
1188+
/// # }
1189+
/// #
1190+
/// use std::{rc::Rc, time::Duration};
11631191
///
11641192
/// use pyo3::prelude::*;
11651193
///
11661194
/// /// Awaitable sleep function
11671195
/// #[pyfunction]
1168-
/// fn sleep_for<'p>(py: Python<'p>, secs: &'p PyAny) -> PyResult<&'p PyAny> {
1196+
/// fn sleep_for<'p>(py: Python<'p>, secs: u64) -> PyResult<&'p PyAny> {
11691197
/// // Rc is !Send so it cannot be passed into pyo3_asyncio::generic::future_into_py
11701198
/// let secs = Rc::new(secs);
11711199
///
@@ -1178,7 +1206,10 @@ where
11781206
/// )
11791207
/// }
11801208
/// ```
1181-
pub fn local_cancellable_future_into_py_with_loop<R, F>(event_loop: &PyAny, fut: F) -> PyResult<&PyAny>
1209+
pub fn local_cancellable_future_into_py_with_loop<R, F>(
1210+
event_loop: &PyAny,
1211+
fut: F,
1212+
) -> PyResult<&PyAny>
11821213
where
11831214
R: Runtime + SpawnLocalExt,
11841215
F: Future<Output = PyResult<PyObject>> + 'static,
@@ -1300,3 +1331,113 @@ where
13001331
{
13011332
local_future_into_py_with_loop::<R, F>(get_current_loop::<R>(py)?, fut)
13021333
}
1334+
/// Convert a Rust Future into a Python awaitable with a generic runtime
1335+
///
1336+
/// Unlike [`future_into_py`], this function will stop the Rust future from running when the
1337+
/// `asyncio.Future` is cancelled from Python.
1338+
///
1339+
/// __This function will be deprecated in favor of [`local_future_into_py`] in `v0.15` because
1340+
/// it will become the default behaviour. In `v0.15`, any calls to this function can be seamlessly
1341+
/// replaced with [`local_future_into_py`].__
1342+
///
1343+
/// # Arguments
1344+
/// * `py` - The current PyO3 GIL guard
1345+
/// * `fut` - The Rust future to be converted
1346+
///
1347+
/// # Examples
1348+
///
1349+
/// ```no_run
1350+
/// # use std::{task::{Context, Poll}, pin::Pin, future::Future};
1351+
/// #
1352+
/// # use pyo3_asyncio::generic::{JoinError, SpawnLocalExt, Runtime};
1353+
/// #
1354+
/// # struct MyCustomJoinError;
1355+
/// #
1356+
/// # impl JoinError for MyCustomJoinError {
1357+
/// # fn is_panic(&self) -> bool {
1358+
/// # unreachable!()
1359+
/// # }
1360+
/// # }
1361+
/// #
1362+
/// # struct MyCustomJoinHandle;
1363+
/// #
1364+
/// # impl Future for MyCustomJoinHandle {
1365+
/// # type Output = Result<(), MyCustomJoinError>;
1366+
/// #
1367+
/// # fn poll(self: Pin<&mut Self>, _cx: &mut Context) -> Poll<Self::Output> {
1368+
/// # unreachable!()
1369+
/// # }
1370+
/// # }
1371+
/// #
1372+
/// # struct MyCustomRuntime;
1373+
/// #
1374+
/// # impl MyCustomRuntime {
1375+
/// # async fn sleep(_: Duration) {
1376+
/// # unreachable!()
1377+
/// # }
1378+
/// # }
1379+
/// #
1380+
/// # impl Runtime for MyCustomRuntime {
1381+
/// # type JoinError = MyCustomJoinError;
1382+
/// # type JoinHandle = MyCustomJoinHandle;
1383+
/// #
1384+
/// # fn scope<F, R>(_event_loop: PyObject, fut: F) -> Pin<Box<dyn Future<Output = R> + Send>>
1385+
/// # where
1386+
/// # F: Future<Output = R> + Send + 'static
1387+
/// # {
1388+
/// # unreachable!()
1389+
/// # }
1390+
/// # fn get_task_event_loop(py: Python) -> Option<&PyAny> {
1391+
/// # unreachable!()
1392+
/// # }
1393+
/// #
1394+
/// # fn spawn<F>(fut: F) -> Self::JoinHandle
1395+
/// # where
1396+
/// # F: Future<Output = ()> + Send + 'static
1397+
/// # {
1398+
/// # unreachable!()
1399+
/// # }
1400+
/// # }
1401+
/// #
1402+
/// # impl SpawnLocalExt for MyCustomRuntime {
1403+
/// # fn scope_local<F, R>(_event_loop: PyObject, fut: F) -> Pin<Box<dyn Future<Output = R>>>
1404+
/// # where
1405+
/// # F: Future<Output = R> + 'static
1406+
/// # {
1407+
/// # unreachable!()
1408+
/// # }
1409+
/// #
1410+
/// # fn spawn_local<F>(fut: F) -> Self::JoinHandle
1411+
/// # where
1412+
/// # F: Future<Output = ()> + 'static
1413+
/// # {
1414+
/// # unreachable!()
1415+
/// # }
1416+
/// # }
1417+
/// #
1418+
/// use std::{rc::Rc, time::Duration};
1419+
///
1420+
/// use pyo3::prelude::*;
1421+
///
1422+
/// /// Awaitable sleep function
1423+
/// #[pyfunction]
1424+
/// fn sleep_for(py: Python, secs: u64) -> PyResult<&PyAny> {
1425+
/// // Rc is !Send so it cannot be passed into pyo3_asyncio::generic::future_into_py
1426+
/// let secs = Rc::new(secs);
1427+
///
1428+
/// pyo3_asyncio::generic::local_cancellable_future_into_py::<MyCustomRuntime, _>(
1429+
/// py,
1430+
/// async move {
1431+
/// MyCustomRuntime::sleep(Duration::from_secs(*secs)).await;
1432+
/// Python::with_gil(|py| Ok(py.None()))
1433+
/// }
1434+
/// )
1435+
/// }
1436+
/// ```
1437+
pub fn local_cancellable_future_into_py<R, F>(py: Python, fut: F) -> PyResult<&PyAny>
1438+
where
1439+
R: Runtime + SpawnLocalExt,
1440+
F: Future<Output = PyResult<PyObject>> + 'static,
1441+
{
1442+
local_cancellable_future_into_py_with_loop::<R, F>(get_current_loop::<R>(py)?, fut)
1443+
}

0 commit comments

Comments
 (0)