113
113
//! });
114
114
//!
115
115
//! pyo3_asyncio::tokio::future_into_py_with_loop(
116
- //! locals.event_loop.clone().into_ref (py),
116
+ //! locals.event_loop(py),
117
117
//! // Store the current loop in task-local data
118
118
//! pyo3_asyncio::tokio::scope(locals.clone(), async move {
119
119
//! let py_sleep = Python::with_gil(|py| {
139
139
//! let locals = pyo3_asyncio::tokio::get_current_locals(py)?;
140
140
//!
141
141
//! pyo3_asyncio::tokio::future_into_py_with_loop(
142
- //! locals.event_loop.clone().into_ref (py),
142
+ //! locals.event_loop(py),
143
143
//! // Store the current loop in task-local data
144
144
//! pyo3_asyncio::tokio::scope(locals.clone(), async move {
145
145
//! let py_sleep = Python::with_gil(|py| {
@@ -522,12 +522,11 @@ fn copy_context(py: Python) -> PyResult<Option<&PyAny>> {
522
522
523
523
/// Task-local data to store for Python conversions.
524
524
#[ derive( Debug , Clone ) ]
525
- #[ non_exhaustive]
526
525
pub struct TaskLocals {
527
526
/// Track the event loop of the Python task
528
- pub event_loop : PyObject ,
527
+ event_loop : PyObject ,
529
528
/// Track the contextvars of the Python task
530
- pub context : PyObject ,
529
+ context : PyObject ,
531
530
}
532
531
533
532
impl TaskLocals {
@@ -540,7 +539,7 @@ impl TaskLocals {
540
539
}
541
540
542
541
/// Manually provide the contextvars for the current task.
543
- pub fn context ( self , context : & PyAny ) -> Self {
542
+ pub fn with_context ( self , context : & PyAny ) -> Self {
544
543
Self {
545
544
context : context. into ( ) ,
546
545
..self
@@ -551,11 +550,21 @@ impl TaskLocals {
551
550
pub fn copy_context ( self , py : Python ) -> PyResult < Self > {
552
551
// No-op if context cannot be copied (Python 3.6 fallback)
553
552
if let Some ( cx) = copy_context ( py) ? {
554
- Ok ( self . context ( cx) )
553
+ Ok ( self . with_context ( cx) )
555
554
} else {
556
555
Ok ( self )
557
556
}
558
557
}
558
+
559
+ /// Get a reference to the event loop
560
+ pub fn event_loop < ' p > ( & self , py : Python < ' p > ) -> & ' p PyAny {
561
+ self . event_loop . clone ( ) . into_ref ( py)
562
+ }
563
+
564
+ /// Get a reference to the python context
565
+ pub fn context < ' p > ( & self , py : Python < ' p > ) -> & ' p PyAny {
566
+ self . context . clone ( ) . into_ref ( py)
567
+ }
559
568
}
560
569
561
570
/// Get a reference to the Python event loop cached by `try_init` (0.13 behaviour)
@@ -779,8 +788,8 @@ pub fn into_future_with_locals(
779
788
let ( tx, rx) = oneshot:: channel ( ) ;
780
789
781
790
call_soon_threadsafe (
782
- locals. event_loop . as_ref ( py) ,
783
- locals. context . as_ref ( py) ,
791
+ locals. event_loop ( py) ,
792
+ locals. context ( py) ,
784
793
( PyEnsureFuture {
785
794
awaitable : awaitable. into ( ) ,
786
795
tx : Some ( tx) ,
0 commit comments