Skip to content

Commit 2b110e5

Browse files
author
Andrew J Westlake
committed
Changed TaskLocals to use private fields and accessors instead of non_exhaustive
1 parent 9c60360 commit 2b110e5

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/lib.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
//! });
114114
//!
115115
//! pyo3_asyncio::tokio::future_into_py_with_loop(
116-
//! locals.event_loop.clone().into_ref(py),
116+
//! locals.event_loop(py),
117117
//! // Store the current loop in task-local data
118118
//! pyo3_asyncio::tokio::scope(locals.clone(), async move {
119119
//! let py_sleep = Python::with_gil(|py| {
@@ -139,7 +139,7 @@
139139
//! let locals = pyo3_asyncio::tokio::get_current_locals(py)?;
140140
//!
141141
//! pyo3_asyncio::tokio::future_into_py_with_loop(
142-
//! locals.event_loop.clone().into_ref(py),
142+
//! locals.event_loop(py),
143143
//! // Store the current loop in task-local data
144144
//! pyo3_asyncio::tokio::scope(locals.clone(), async move {
145145
//! let py_sleep = Python::with_gil(|py| {
@@ -522,12 +522,11 @@ fn copy_context(py: Python) -> PyResult<Option<&PyAny>> {
522522

523523
/// Task-local data to store for Python conversions.
524524
#[derive(Debug, Clone)]
525-
#[non_exhaustive]
526525
pub struct TaskLocals {
527526
/// Track the event loop of the Python task
528-
pub event_loop: PyObject,
527+
event_loop: PyObject,
529528
/// Track the contextvars of the Python task
530-
pub context: PyObject,
529+
context: PyObject,
531530
}
532531

533532
impl TaskLocals {
@@ -540,7 +539,7 @@ impl TaskLocals {
540539
}
541540

542541
/// 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 {
544543
Self {
545544
context: context.into(),
546545
..self
@@ -551,11 +550,21 @@ impl TaskLocals {
551550
pub fn copy_context(self, py: Python) -> PyResult<Self> {
552551
// No-op if context cannot be copied (Python 3.6 fallback)
553552
if let Some(cx) = copy_context(py)? {
554-
Ok(self.context(cx))
553+
Ok(self.with_context(cx))
555554
} else {
556555
Ok(self)
557556
}
558557
}
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+
}
559568
}
560569

561570
/// 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(
779788
let (tx, rx) = oneshot::channel();
780789

781790
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),
784793
(PyEnsureFuture {
785794
awaitable: awaitable.into(),
786795
tx: Some(tx),

0 commit comments

Comments
 (0)