Skip to content

Commit 98dee5d

Browse files
authored
Merge pull request #8 from cjdsellers/main
Fix formatting and clippy lints
2 parents e0f7ed6 + 12b0f9e commit 98dee5d

File tree

4 files changed

+61
-55
lines changed

4 files changed

+61
-55
lines changed

pytests/test_async_std_asyncio.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ async fn test_other_awaitables() -> PyResult<()> {
9696
#[pyo3_async_runtimes::async_std::test]
9797
async fn test_panic() -> PyResult<()> {
9898
let fut = Python::with_gil(|py| -> PyResult<_> {
99-
pyo3_async_runtimes::async_std::into_future(pyo3_async_runtimes::async_std::future_into_py::<
100-
_,
101-
(),
102-
>(py, async {
103-
panic!("this panic was intentional!")
104-
})?)
99+
pyo3_async_runtimes::async_std::into_future(
100+
pyo3_async_runtimes::async_std::future_into_py::<_, ()>(py, async {
101+
panic!("this panic was intentional!")
102+
})?,
103+
)
105104
})?;
106105

107106
match fut.await {
@@ -385,5 +384,7 @@ fn test_contextvars() -> PyResult<()> {
385384
fn main() -> pyo3::PyResult<()> {
386385
pyo3::prepare_freethreaded_python();
387386

388-
Python::with_gil(|py| pyo3_async_runtimes::async_std::run(py, pyo3_async_runtimes::testing::main()))
387+
Python::with_gil(|py| {
388+
pyo3_async_runtimes::async_std::run(py, pyo3_async_runtimes::testing::main())
389+
})
389390
}

pytests/tokio_asyncio/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,11 @@ fn test_local_future_into_py(event_loop: PyObject) -> PyResult<()> {
122122
#[pyo3_async_runtimes::tokio::test]
123123
async fn test_panic() -> PyResult<()> {
124124
let fut = Python::with_gil(|py| -> PyResult<_> {
125-
pyo3_async_runtimes::tokio::into_future(pyo3_async_runtimes::tokio::future_into_py::<_, ()>(
126-
py,
127-
async { panic!("this panic was intentional!") },
128-
)?)
125+
pyo3_async_runtimes::tokio::into_future(
126+
pyo3_async_runtimes::tokio::future_into_py::<_, ()>(py, async {
127+
panic!("this panic was intentional!")
128+
})?,
129+
)
129130
})?;
130131

131132
match fut.await {

src/generic.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! class="module-item stab portability"
66
//! style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
77
//! ><code>unstable-streams</code></span>
8-
//! are only available when the `unstable-streams` Cargo feature is enabled:
8+
//! > are only available when the `unstable-streams` Cargo feature is enabled:
99
//!
1010
//! ```toml
1111
//! [dependencies.pyo3-asyncio-0-21]
@@ -478,13 +478,13 @@ where
478478
/// via [`into_future`] (new behaviour in `v0.15`).
479479
///
480480
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
481-
/// unfortunately fail to resolve them when called within the Rust future. This is because the
482-
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
481+
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
482+
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
483483
/// >
484484
/// > As a workaround, you can get the `contextvars` from the current task locals using
485-
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
486-
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
487-
/// synchronous function, and restore the previous context when it returns or raises an exception.
485+
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
486+
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
487+
/// > synchronous function, and restore the previous context when it returns or raises an exception.
488488
///
489489
/// # Arguments
490490
/// * `py` - PyO3 GIL guard
@@ -655,7 +655,7 @@ fn get_panic_message(any: &dyn std::any::Any) -> &str {
655655
if let Some(str_slice) = any.downcast_ref::<&str>() {
656656
str_slice
657657
} else if let Some(string) = any.downcast_ref::<String>() {
658-
string
658+
string.as_str()
659659
} else {
660660
"unknown error"
661661
}
@@ -751,13 +751,13 @@ impl PyDoneCallback {
751751
/// via [`into_future`] (new behaviour in `v0.15`).
752752
///
753753
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
754-
/// unfortunately fail to resolve them when called within the Rust future. This is because the
755-
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
754+
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
755+
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
756756
/// >
757757
/// > As a workaround, you can get the `contextvars` from the current task locals using
758-
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
759-
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
760-
/// synchronous function, and restore the previous context when it returns or raises an exception.
758+
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
759+
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
760+
/// > synchronous function, and restore the previous context when it returns or raises an exception.
761761
///
762762
/// # Arguments
763763
/// * `py` - The current PyO3 GIL guard
@@ -859,13 +859,13 @@ where
859859
/// via [`into_future`] (new behaviour in `v0.15`).
860860
///
861861
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
862-
/// unfortunately fail to resolve them when called within the Rust future. This is because the
863-
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
862+
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
863+
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
864864
/// >
865865
/// > As a workaround, you can get the `contextvars` from the current task locals using
866-
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
867-
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
868-
/// synchronous function, and restore the previous context when it returns or raises an exception.
866+
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
867+
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
868+
/// > synchronous function, and restore the previous context when it returns or raises an exception.
869869
///
870870
/// # Arguments
871871
/// * `py` - PyO3 GIL guard
@@ -1065,13 +1065,13 @@ where
10651065
/// via [`into_future`] (new behaviour in `v0.15`).
10661066
///
10671067
/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
1068-
/// unfortunately fail to resolve them when called within the Rust future. This is because the
1069-
/// function is being called from a Rust thread, not inside an actual Python coroutine context.
1068+
/// > unfortunately fail to resolve them when called within the Rust future. This is because the
1069+
/// > function is being called from a Rust thread, not inside an actual Python coroutine context.
10701070
/// >
10711071
/// > As a workaround, you can get the `contextvars` from the current task locals using
1072-
/// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
1073-
/// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
1074-
/// synchronous function, and restore the previous context when it returns or raises an exception.
1072+
/// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
1073+
/// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
1074+
/// > synchronous function, and restore the previous context when it returns or raises an exception.
10751075
///
10761076
/// # Arguments
10771077
/// * `py` - The current PyO3 GIL guard
@@ -1446,11 +1446,14 @@ where
14461446
into_stream_with_locals_v1::<R>(get_current_locals::<R>(gen.py())?, gen)
14471447
}
14481448

1449+
#[allow(dead_code)]
14491450
fn py_true() -> PyObject {
14501451
static TRUE: OnceCell<PyObject> = OnceCell::new();
14511452
TRUE.get_or_init(|| Python::with_gil(|py| true.into_py(py)))
14521453
.clone()
14531454
}
1455+
1456+
#[allow(dead_code)]
14541457
fn py_false() -> PyObject {
14551458
static FALSE: OnceCell<PyObject> = OnceCell::new();
14561459
FALSE
@@ -1463,6 +1466,7 @@ trait Sender: Send + 'static {
14631466
fn close(&mut self) -> PyResult<()>;
14641467
}
14651468

1469+
#[allow(dead_code)]
14661470
struct GenericSender<R>
14671471
where
14681472
R: Runtime,

src/lib.rs

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
//! library needs to be able to preserve `contextvars` during conversions.
4242
//!
4343
//! > The core conversions we've mentioned so far in the README should insulate you from these
44-
//! concerns in most cases. For the edge cases where they don't, this section should provide you
45-
//! with the information you need to solve these problems.
44+
//! > concerns in most cases. For the edge cases where they don't, this section should provide you
45+
//! > with the information you need to solve these problems.
4646
//!
4747
//! ### The Main Dilemma
4848
//!
@@ -74,9 +74,9 @@
7474
//!
7575
//! - `pyo3_async_runtimes::into_future_with_locals` - Convert a Python awaitable into a Rust future.
7676
//! - `pyo3_async_runtimes::<runtime>::future_into_py_with_locals` - Convert a Rust future into a Python
77-
//! awaitable.
77+
//! awaitable.
7878
//! - `pyo3_async_runtimes::<runtime>::local_future_into_py_with_locals` - Convert a `!Send` Rust future
79-
//! into a Python awaitable.
79+
//! into a Python awaitable.
8080
//!
8181
//! One clear disadvantage to this approach is that the Rust application has to explicitly track
8282
//! these references. In native libraries, we can't make any assumptions about the underlying event
@@ -119,12 +119,12 @@
119119
//! ```
120120
//!
121121
//! > A naive solution to this tracking problem would be to cache a global reference to the asyncio
122-
//! event loop that all PyO3 Asyncio conversions can use. In fact this is what we did in PyO3
123-
//! Asyncio `v0.13`. This works well for applications, but it soon became clear that this is not
124-
//! so ideal for libraries. Libraries usually have no direct control over how the event loop is
125-
//! managed, they're just expected to work with any event loop at any point in the application.
126-
//! This problem is compounded further when multiple event loops are used in the application since
127-
//! the global reference will only point to one.
122+
//! > event loop that all PyO3 Asyncio conversions can use. In fact this is what we did in PyO3
123+
//! > Asyncio `v0.13`. This works well for applications, but it soon became clear that this is not
124+
//! > so ideal for libraries. Libraries usually have no direct control over how the event loop is
125+
//! > managed, they're just expected to work with any event loop at any point in the application.
126+
//! > This problem is compounded further when multiple event loops are used in the application since
127+
//! > the global reference will only point to one.
128128
//!
129129
//! Another disadvantage to this explicit approach that is less obvious is that we can no longer
130130
//! call our `#[pyfunction] fn sleep` on a Rust runtime since `asyncio.get_running_loop` only works
@@ -146,7 +146,7 @@
146146
//!
147147
//! - `pyo3_async_runtimes::<runtime>::scope` - Store the task-local data when executing the given Future.
148148
//! - `pyo3_async_runtimes::<runtime>::scope_local` - Store the task-local data when executing the given
149-
//! `!Send` Future.
149+
//! `!Send` Future.
150150
//!
151151
//! With these new functions, we can make our previous example more correct:
152152
//!
@@ -222,15 +222,15 @@
222222
//!
223223
//! - `pyo3_async_runtimes::<runtime>::into_future`
224224
//! > Convert a Python awaitable into a Rust future (using
225-
//! `pyo3_async_runtimes::<runtime>::get_current_locals`)
225+
//! > `pyo3_async_runtimes::<runtime>::get_current_locals`)
226226
//! - `pyo3_async_runtimes::<runtime>::future_into_py`
227227
//! > Convert a Rust future into a Python awaitable (using
228-
//! `pyo3_async_runtimes::<runtime>::get_current_locals` and `pyo3_async_runtimes::<runtime>::scope` to set the
229-
//! task-local event loop for the given Rust future)
228+
//! > `pyo3_async_runtimes::<runtime>::get_current_locals` and `pyo3_async_runtimes::<runtime>::scope` to set the
229+
//! > task-local event loop for the given Rust future)
230230
//! - `pyo3_async_runtimes::<runtime>::local_future_into_py`
231231
//! > Convert a `!Send` Rust future into a Python awaitable (using
232-
//! `pyo3_async_runtimes::<runtime>::get_current_locals` and `pyo3_async_runtimes::<runtime>::scope_local` to
233-
//! set the task-local event loop for the given Rust future).
232+
//! > `pyo3_async_runtimes::<runtime>::get_current_locals` and `pyo3_async_runtimes::<runtime>::scope_local` to
233+
//! > set the task-local event loop for the given Rust future).
234234
//!
235235
//! __These are the functions that we recommend using__. With these functions, the previous example
236236
//! can be rewritten to be more compact:
@@ -278,7 +278,7 @@
278278
//! ```
279279
//!
280280
//! > A special thanks to [@ShadowJonathan](https://github.com/ShadowJonathan) for helping with the
281-
//! design and review of these changes!
281+
//! > design and review of these changes!
282282
//!
283283
//! ## Rust's Event Loop
284284
//!
@@ -287,7 +287,7 @@
287287
//! with the [`generic`] module)!
288288
//!
289289
//! > _In the future, we may implement first class support for more Rust runtimes. Contributions are
290-
//! welcome as well!_
290+
//! > welcome as well!_
291291
//!
292292
//! ## Features
293293
//!
@@ -296,7 +296,7 @@
296296
//! class="module-item stab portability"
297297
//! style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
298298
//! ><code>attributes</code></span>
299-
//! are only available when the `attributes` Cargo feature is enabled:
299+
//! > are only available when the `attributes` Cargo feature is enabled:
300300
//!
301301
//! ```toml
302302
//! [dependencies.pyo3-asyncio-0-21]
@@ -309,7 +309,7 @@
309309
//! class="module-item stab portability"
310310
//! style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
311311
//! ><code>async-std-runtime</code></span>
312-
//! are only available when the `async-std-runtime` Cargo feature is enabled:
312+
//! > are only available when the `async-std-runtime` Cargo feature is enabled:
313313
//!
314314
//! ```toml
315315
//! [dependencies.pyo3-asyncio-0-21]
@@ -322,7 +322,7 @@
322322
//! class="module-item stab portability"
323323
//! style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
324324
//! ><code>tokio-runtime</code></span>
325-
//! are only available when the `tokio-runtime` Cargo feature is enabled:
325+
//! > are only available when the `tokio-runtime` Cargo feature is enabled:
326326
//!
327327
//! ```toml
328328
//! [dependencies.pyo3-asyncio-0-21]
@@ -335,7 +335,7 @@
335335
//! class="module-item stab portability"
336336
//! style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"
337337
//! ><code>testing</code></span>
338-
//! are only available when the `testing` Cargo feature is enabled:
338+
//! > are only available when the `testing` Cargo feature is enabled:
339339
//!
340340
//! ```toml
341341
//! [dependencies.pyo3-asyncio-0-21]

0 commit comments

Comments
 (0)