1313//! features = ["unstable-streams"]
1414//! ```
1515
16- use std:: { any:: Any , cell:: RefCell , future:: Future , panic:: AssertUnwindSafe , pin:: Pin } ;
17-
1816use async_std:: task;
1917use futures:: FutureExt ;
2018use pyo3:: prelude:: * ;
19+ use std:: { any:: Any , cell:: RefCell , future:: Future , panic:: AssertUnwindSafe , pin:: Pin } ;
2120
2221use crate :: {
2322 generic:: { self , ContextExt , JoinError , LocalContextExt , Runtime , SpawnLocalExt } ,
@@ -34,13 +33,13 @@ pub mod re_exports {
3433
3534/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>attributes</code></span> Provides the boilerplate for the `async-std` runtime and runs an async fn as main
3635#[ cfg( feature = "attributes" ) ]
37- pub use pyo3_asyncio_macros_0_21 :: async_std_main as main;
36+ pub use pyo3_async_runtimes_macros :: async_std_main as main;
3837
3938/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>attributes</code></span>
4039/// <span class="module-item stab portability" style="display: inline; border-radius: 3px; padding: 2px; font-size: 80%; line-height: 1.2;"><code>testing</code></span>
4140/// Registers an `async-std` test with the `pyo3-asyncio` test harness
4241#[ cfg( all( feature = "attributes" , feature = "testing" ) ) ]
43- pub use pyo3_asyncio_macros_0_21 :: async_std_test as test;
42+ pub use pyo3_async_runtimes_macros :: async_std_test as test;
4443
4544struct AsyncStdJoinErr ( Box < dyn Any + Send + ' static > ) ;
4645
@@ -71,7 +70,7 @@ impl Runtime for AsyncStdRuntime {
7170 AssertUnwindSafe ( fut)
7271 . catch_unwind ( )
7372 . await
74- . map_err ( |e| AsyncStdJoinErr ( e ) )
73+ . map_err ( AsyncStdJoinErr )
7574 } )
7675 }
7776}
@@ -90,10 +89,13 @@ impl ContextExt for AsyncStdRuntime {
9089 }
9190
9291 fn get_task_locals ( ) -> Option < TaskLocals > {
93- match TASK_LOCALS . try_with ( |c| c. borrow ( ) . clone ( ) ) {
94- Ok ( locals) => locals,
95- Err ( _) => None ,
96- }
92+ TASK_LOCALS
93+ . try_with ( |c| {
94+ c. borrow ( )
95+ . as_ref ( )
96+ . map ( |locals| Python :: with_gil ( |py| locals. clone_ref ( py) ) )
97+ } )
98+ . unwrap_or_default ( )
9799 }
98100}
99101
@@ -236,13 +238,13 @@ where
236238/// via [`into_future`] (new behaviour in `v0.15`).
237239///
238240/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
239- /// unfortunately fail to resolve them when called within the Rust future. This is because the
240- /// function is being called from a Rust thread, not inside an actual Python coroutine context.
241+ /// > unfortunately fail to resolve them when called within the Rust future. This is because the
242+ /// > function is being called from a Rust thread, not inside an actual Python coroutine context.
241243/// >
242244/// > As a workaround, you can get the `contextvars` from the current task locals using
243- /// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
244- /// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
245- /// synchronous function, and restore the previous context when it returns or raises an exception.
245+ /// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
246+ /// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
247+ /// > synchronous function, and restore the previous context when it returns or raises an exception.
246248///
247249/// # Arguments
248250/// * `py` - PyO3 GIL guard
@@ -291,13 +293,13 @@ where
291293/// via [`into_future`] (new behaviour in `v0.15`).
292294///
293295/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
294- /// unfortunately fail to resolve them when called within the Rust future. This is because the
295- /// function is being called from a Rust thread, not inside an actual Python coroutine context.
296+ /// > unfortunately fail to resolve them when called within the Rust future. This is because the
297+ /// > function is being called from a Rust thread, not inside an actual Python coroutine context.
296298/// >
297299/// > As a workaround, you can get the `contextvars` from the current task locals using
298- /// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
299- /// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
300- /// synchronous function, and restore the previous context when it returns or raises an exception.
300+ /// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
301+ /// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
302+ /// > synchronous function, and restore the previous context when it returns or raises an exception.
301303///
302304/// # Arguments
303305/// * `py` - The current PyO3 GIL guard
@@ -337,13 +339,13 @@ where
337339/// via [`into_future`] (new behaviour in `v0.15`).
338340///
339341/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
340- /// unfortunately fail to resolve them when called within the Rust future. This is because the
341- /// function is being called from a Rust thread, not inside an actual Python coroutine context.
342+ /// > unfortunately fail to resolve them when called within the Rust future. This is because the
343+ /// > function is being called from a Rust thread, not inside an actual Python coroutine context.
342344/// >
343345/// > As a workaround, you can get the `contextvars` from the current task locals using
344- /// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
345- /// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
346- /// synchronous function, and restore the previous context when it returns or raises an exception.
346+ /// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
347+ /// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
348+ /// > synchronous function, and restore the previous context when it returns or raises an exception.
347349///
348350/// # Arguments
349351/// * `py` - PyO3 GIL guard
@@ -412,13 +414,13 @@ where
412414/// via [`into_future`] (new behaviour in `v0.15`).
413415///
414416/// > Although `contextvars` are preserved for async Python functions, synchronous functions will
415- /// unfortunately fail to resolve them when called within the Rust future. This is because the
416- /// function is being called from a Rust thread, not inside an actual Python coroutine context.
417+ /// > unfortunately fail to resolve them when called within the Rust future. This is because the
418+ /// > function is being called from a Rust thread, not inside an actual Python coroutine context.
417419/// >
418420/// > As a workaround, you can get the `contextvars` from the current task locals using
419- /// [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
420- /// synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
421- /// synchronous function, and restore the previous context when it returns or raises an exception.
421+ /// > [`get_current_locals`] and [`TaskLocals::context`](`crate::TaskLocals::context`), then wrap your
422+ /// > synchronous function in a call to `contextvars.Context.run`. This will set the context, call the
423+ /// > synchronous function, and restore the previous context when it returns or raises an exception.
422424///
423425/// # Arguments
424426/// * `py` - The current PyO3 GIL guard
@@ -573,8 +575,8 @@ pub fn into_future(
573575/// # fn main() {}
574576/// ```
575577#[ cfg( feature = "unstable-streams" ) ]
576- pub fn into_stream_v1 < ' p > (
577- gen : Bound < ' p , PyAny > ,
578+ pub fn into_stream_v1 (
579+ gen : Bound < ' _ , PyAny > ,
578580) -> PyResult < impl futures:: Stream < Item = PyResult < PyObject > > + ' static > {
579581 generic:: into_stream_v1 :: < AsyncStdRuntime > ( gen)
580582}
@@ -633,9 +635,9 @@ pub fn into_stream_v1<'p>(
633635/// # fn main() {}
634636/// ```
635637#[ cfg( feature = "unstable-streams" ) ]
636- pub fn into_stream_with_locals_v1 < ' p > (
638+ pub fn into_stream_with_locals_v1 (
637639 locals : TaskLocals ,
638- gen : Bound < ' p , PyAny > ,
640+ gen : Bound < ' _ , PyAny > ,
639641) -> PyResult < impl futures:: Stream < Item = PyResult < PyObject > > + ' static > {
640642 generic:: into_stream_with_locals_v1 :: < AsyncStdRuntime > ( locals, gen)
641643}
@@ -694,9 +696,9 @@ pub fn into_stream_with_locals_v1<'p>(
694696/// # fn main() {}
695697/// ```
696698#[ cfg( feature = "unstable-streams" ) ]
697- pub fn into_stream_with_locals_v2 < ' p > (
699+ pub fn into_stream_with_locals_v2 (
698700 locals : TaskLocals ,
699- gen : Bound < ' p , PyAny > ,
701+ gen : Bound < ' _ , PyAny > ,
700702) -> PyResult < impl futures:: Stream < Item = PyObject > + ' static > {
701703 generic:: into_stream_with_locals_v2 :: < AsyncStdRuntime > ( locals, gen)
702704}
@@ -751,8 +753,8 @@ pub fn into_stream_with_locals_v2<'p>(
751753/// # fn main() {}
752754/// ```
753755#[ cfg( feature = "unstable-streams" ) ]
754- pub fn into_stream_v2 < ' p > (
755- gen : Bound < ' p , PyAny > ,
756+ pub fn into_stream_v2 (
757+ gen : Bound < ' _ , PyAny > ,
756758) -> PyResult < impl futures:: Stream < Item = PyObject > + ' static > {
757759 generic:: into_stream_v2 :: < AsyncStdRuntime > ( gen)
758760}
0 commit comments