Skip to content

Commit cf24a0a

Browse files
author
Andrew J Westlake
committed
Renamed local_future_into_py, changed return type to &PyAny
1 parent 58baa05 commit cf24a0a

File tree

6 files changed

+31
-33
lines changed

6 files changed

+31
-33
lines changed

pytests/test_async_std_asyncio.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,12 @@ async fn test_local_coroutine() -> PyResult<()> {
8484
Python::with_gil(|py| {
8585
let non_send_secs = Rc::new(1);
8686

87-
let py_future = pyo3_asyncio::async_std::into_local_py_future(py, async move {
87+
let py_future = pyo3_asyncio::async_std::local_future_into_py(py, async move {
8888
async_std::task::sleep(Duration::from_secs(*non_send_secs)).await;
8989
Ok(Python::with_gil(|py| py.None()))
9090
})?;
9191

92-
pyo3_asyncio::into_future(py_future.as_ref(py))
92+
pyo3_asyncio::into_future(py_future)
9393
})?
9494
.await?;
9595

pytests/tokio_asyncio/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,12 @@ fn test_local_set_coroutine() -> PyResult<()> {
8989
Python::with_gil(|py| {
9090
let non_send_secs = Rc::new(1);
9191

92-
let py_future = pyo3_asyncio::tokio::into_local_py_future(py, async move {
92+
let py_future = pyo3_asyncio::tokio::local_future_into_py(py, async move {
9393
tokio::time::sleep(Duration::from_secs(*non_send_secs)).await;
9494
Ok(Python::with_gil(|py| py.None()))
9595
})?;
9696

97-
pyo3_asyncio::into_future(py_future.as_ref(py))
97+
pyo3_asyncio::into_future(py_future)
9898
})?
9999
.await?;
100100

src/async_std.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ where
9999
generic::run_until_complete::<AsyncStdRuntime, _>(py, fut)
100100
}
101101

102-
/// Convert a Rust Future into a Python coroutine
102+
/// Convert a Rust Future into a Python awaitable
103103
///
104104
/// # Arguments
105105
/// * `py` - The current PyO3 GIL guard
@@ -130,7 +130,7 @@ where
130130
generic::into_coroutine::<AsyncStdRuntime, _>(py, fut)
131131
}
132132

133-
/// Convert a `!Send` Rust Future into a Python coroutine
133+
/// Convert a `!Send` Rust Future into a Python awaitable
134134
///
135135
/// # Arguments
136136
/// * `py` - The current PyO3 GIL guard
@@ -145,11 +145,11 @@ where
145145
///
146146
/// /// Awaitable non-send sleep function
147147
/// #[pyfunction]
148-
/// fn sleep_for(py: Python, secs: u64) -> PyResult<PyObject> {
148+
/// fn sleep_for(py: Python, secs: u64) -> PyResult<&PyAny> {
149149
/// // Rc is non-send so it cannot be passed into pyo3_asyncio::tokio::into_coroutine
150150
/// let secs = Rc::new(secs);
151151
///
152-
/// pyo3_asyncio::async_std::into_local_py_future(py, async move {
152+
/// pyo3_asyncio::async_std::local_future_into_py(py, async move {
153153
/// async_std::task::sleep(Duration::from_secs(*secs)).await;
154154
/// Python::with_gil(|py| Ok(py.None()))
155155
/// })
@@ -160,7 +160,7 @@ where
160160
/// async fn main() -> PyResult<()> {
161161
/// Python::with_gil(|py| {
162162
/// let py_future = sleep_for(py, 1)?;
163-
/// pyo3_asyncio::into_future(py_future.as_ref(py))
163+
/// pyo3_asyncio::into_future(py_future)
164164
/// })?
165165
/// .await?;
166166
///
@@ -169,9 +169,9 @@ where
169169
/// # #[cfg(not(all(feature = "async-std-runtime", feature = "attributes")))]
170170
/// # fn main() {}
171171
/// ```
172-
pub fn into_local_py_future<F>(py: Python, fut: F) -> PyResult<PyObject>
172+
pub fn local_future_into_py<F>(py: Python, fut: F) -> PyResult<&PyAny>
173173
where
174174
F: Future<Output = PyResult<PyObject>> + 'static,
175175
{
176-
generic::into_local_py_future::<AsyncStdRuntime, _>(py, fut)
176+
generic::local_future_into_py::<AsyncStdRuntime, _>(py, fut)
177177
}

src/generic.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ fn set_result(py: Python, future: &PyAny, result: PyResult<PyObject>) -> PyResul
134134
Ok(())
135135
}
136136

137-
/// Convert a Rust Future into a Python coroutine with a generic runtime
137+
/// Convert a Rust Future into a Python awaitable with a generic runtime
138138
///
139139
/// # Arguments
140140
/// * `py` - The current PyO3 GIL guard
@@ -245,7 +245,7 @@ where
245245
Ok(future_rx)
246246
}
247247

248-
/// Convert a `!Send` Rust Future into a Python coroutine with a generic runtime
248+
/// Convert a `!Send` Rust Future into a Python awaitable with a generic runtime
249249
///
250250
/// # Arguments
251251
/// * `py` - The current PyO3 GIL guard
@@ -311,23 +311,21 @@ where
311311
///
312312
/// /// Awaitable sleep function
313313
/// #[pyfunction]
314-
/// fn sleep_for(py: Python, secs: &PyAny) -> PyResult<PyObject> {
315-
/// let secs = secs.extract()?;
316-
///
317-
/// pyo3_asyncio::generic::into_local_py_future::<MyCustomRuntime, _>(py, async move {
314+
/// fn sleep_for(py: Python, secs: u64) -> PyResult<&PyAny> {
315+
/// pyo3_asyncio::generic::local_future_into_py::<MyCustomRuntime, _>(py, async move {
318316
/// MyCustomRuntime::sleep(Duration::from_secs(secs)).await;
319317
/// Python::with_gil(|py| Ok(py.None()))
320318
/// })
321319
/// }
322320
/// ```
323-
pub fn into_local_py_future<R, F>(py: Python, fut: F) -> PyResult<PyObject>
321+
pub fn local_future_into_py<R, F>(py: Python, fut: F) -> PyResult<&PyAny>
324322
where
325323
R: SpawnLocalExt,
326324
F: Future<Output = PyResult<PyObject>> + 'static,
327325
{
328-
let future_rx = CREATE_FUTURE.get().expect(EXPECT_INIT).call0(py)?;
329-
let future_tx1 = future_rx.clone();
330-
let future_tx2 = future_rx.clone();
326+
let future_rx = CREATE_FUTURE.get().expect(EXPECT_INIT).as_ref(py).call0()?;
327+
let future_tx1 = PyObject::from(future_rx);
328+
let future_tx2 = future_tx1.clone();
331329

332330
R::spawn_local(async move {
333331
if let Err(e) = R::spawn_local(async move {

src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
//!
4646
//! ```toml
4747
//! [dependencies.pyo3-asyncio]
48-
//! version = "0.13.0"
48+
//! version = "0.13"
4949
//! features = ["attributes"]
5050
//! ```
5151
//!
@@ -58,7 +58,7 @@
5858
//!
5959
//! ```toml
6060
//! [dependencies.pyo3-asyncio]
61-
//! version = "0.13.0"
61+
//! version = "0.13"
6262
//! features = ["async-std-runtime"]
6363
//! ```
6464
//!
@@ -71,7 +71,7 @@
7171
//!
7272
//! ```toml
7373
//! [dependencies.pyo3-asyncio]
74-
//! version = "0.13.0"
74+
//! version = "0.13"
7575
//! features = ["tokio-runtime"]
7676
//! ```
7777
//!
@@ -84,7 +84,7 @@
8484
//!
8585
//! ```toml
8686
//! [dependencies.pyo3-asyncio]
87-
//! version = "0.13.0"
87+
//! version = "0.13"
8888
//! features = ["testing"]
8989
//! ```
9090

src/tokio.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ where
186186
generic::run_until_complete::<TokioRuntime, _>(py, fut)
187187
}
188188

189-
/// Convert a Rust Future into a Python coroutine
189+
/// Convert a Rust Future into a Python awaitable
190190
///
191191
/// # Arguments
192192
/// * `py` - The current PyO3 GIL guard
@@ -217,7 +217,7 @@ where
217217
generic::into_coroutine::<TokioRuntime, _>(py, fut)
218218
}
219219

220-
/// Convert a `!Send` Rust Future into a Python coroutine
220+
/// Convert a `!Send` Rust Future into a Python awaitable
221221
///
222222
/// # Arguments
223223
/// * `py` - The current PyO3 GIL guard
@@ -232,11 +232,11 @@ where
232232
///
233233
/// /// Awaitable non-send sleep function
234234
/// #[pyfunction]
235-
/// fn sleep_for(py: Python, secs: u64) -> PyResult<PyObject> {
235+
/// fn sleep_for(py: Python, secs: u64) -> PyResult<&PyAny> {
236236
/// // Rc is non-send so it cannot be passed into pyo3_asyncio::tokio::into_coroutine
237237
/// let secs = Rc::new(secs);
238238
///
239-
/// pyo3_asyncio::tokio::into_local_py_future(py, async move {
239+
/// pyo3_asyncio::tokio::local_future_into_py(py, async move {
240240
/// tokio::time::sleep(Duration::from_secs(*secs)).await;
241241
/// Python::with_gil(|py| Ok(py.None()))
242242
/// })
@@ -249,11 +249,11 @@ where
249249
/// // we use spawn_blocking in order to use LocalSet::block_on
250250
/// tokio::task::spawn_blocking(|| {
251251
/// // LocalSet allows us to work with !Send futures within tokio. Without it, any calls to
252-
/// // pyo3_asyncio::tokio::into_local_py_future will panic.
252+
/// // pyo3_asyncio::tokio::local_future_into_py will panic.
253253
/// tokio::task::LocalSet::new().block_on(pyo3_asyncio::tokio::get_runtime(), async {
254254
/// Python::with_gil(|py| {
255255
/// let py_future = sleep_for(py, 1)?;
256-
/// pyo3_asyncio::into_future(py_future.as_ref(py))
256+
/// pyo3_asyncio::into_future(py_future)
257257
/// })?
258258
/// .await?;
259259
///
@@ -264,9 +264,9 @@ where
264264
/// # #[cfg(not(all(feature = "tokio-runtime", feature = "attributes")))]
265265
/// # fn main() {}
266266
/// ```
267-
pub fn into_local_py_future<F>(py: Python, fut: F) -> PyResult<PyObject>
267+
pub fn local_future_into_py<'p, F>(py: Python<'p>, fut: F) -> PyResult<&'p PyAny>
268268
where
269269
F: Future<Output = PyResult<PyObject>> + 'static,
270270
{
271-
generic::into_local_py_future::<TokioRuntime, _>(py, fut)
271+
generic::local_future_into_py::<TokioRuntime, _>(py, fut)
272272
}

0 commit comments

Comments
 (0)