Skip to content

Commit 906aeba

Browse files
committed
Wrap TaskLocals in a single Arc.
1 parent 86d209f commit 906aeba

File tree

2 files changed

+25
-24
lines changed

2 files changed

+25
-24
lines changed

src/generic.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ where
9191
R: ContextExt,
9292
{
9393
if let Some(locals) = R::get_task_locals() {
94-
Ok(locals.event_loop.clone_ref(py).into_bound(py))
94+
Ok(locals.0.event_loop.clone_ref(py).into_bound(py))
9595
} else {
9696
get_running_loop(py)
9797
}
@@ -585,7 +585,7 @@ where
585585
{
586586
let (cancel_tx, cancel_rx) = oneshot::channel();
587587

588-
let py_fut = create_future(locals.event_loop.bind(py).clone())?;
588+
let py_fut = create_future(locals.0.event_loop.bind(py).clone())?;
589589
py_fut.call_method1(
590590
"add_done_callback",
591591
(PyDoneCallback {
@@ -638,7 +638,7 @@ where
638638
get_panic_message(&e.into_panic())
639639
);
640640
let _ = set_result(
641-
locals.event_loop.bind(py),
641+
locals.0.event_loop.bind(py),
642642
future_tx2.bind(py),
643643
Err(RustPanic::new_err(panic_message)),
644644
)
@@ -990,7 +990,7 @@ where
990990
{
991991
let (cancel_tx, cancel_rx) = oneshot::channel();
992992

993-
let py_fut = create_future(locals.event_loop.clone_ref(py).into_bound(py))?;
993+
let py_fut = create_future(locals.0.event_loop.clone_ref(py).into_bound(py))?;
994994
py_fut.call_method1(
995995
"add_done_callback",
996996
(PyDoneCallback {
@@ -1020,7 +1020,7 @@ where
10201020
}
10211021

10221022
let _ = set_result(
1023-
locals2.event_loop.bind(py),
1023+
locals2.0.event_loop.bind(py),
10241024
future_tx1.bind(py),
10251025
result.and_then(|val| val.into_py_any(py)),
10261026
)
@@ -1043,7 +1043,7 @@ where
10431043
get_panic_message(&e.into_panic())
10441044
);
10451045
let _ = set_result(
1046-
locals.event_loop.bind(py),
1046+
locals.0.event_loop.bind(py),
10471047
future_tx2.bind(py),
10481048
Err(RustPanic::new_err(panic_message)),
10491049
)

src/lib.rs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -469,22 +469,26 @@ fn copy_context(py: Python) -> PyResult<Bound<PyAny>> {
469469
contextvars(py)?.call_method0("copy_context")
470470
}
471471

472-
/// Task-local data to store for Python conversions.
472+
/// Task-local inner structure.
473473
#[derive(Debug)]
474-
pub struct TaskLocals {
474+
struct TaskLocalsInner {
475475
/// Track the event loop of the Python task
476-
event_loop: Arc<Py<PyAny>>,
476+
event_loop: Py<PyAny>,
477477
/// Track the contextvars of the Python task
478-
context: Arc<Py<PyAny>>,
478+
context: Py<PyAny>,
479479
}
480480

481+
/// Task-local data to store for Python conversions.
482+
#[derive(Debug)]
483+
pub struct TaskLocals(Arc<TaskLocalsInner>);
484+
481485
impl TaskLocals {
482486
/// At a minimum, TaskLocals must store the event loop.
483487
pub fn new(event_loop: Bound<PyAny>) -> Self {
484-
Self {
485-
context: Arc::new(event_loop.py().None()),
486-
event_loop: Arc::new(event_loop.into()),
487-
}
488+
Self(Arc::new(TaskLocalsInner {
489+
context: event_loop.py().None(),
490+
event_loop: event_loop.into(),
491+
}))
488492
}
489493

490494
/// Construct TaskLocals with the event loop returned by `get_running_loop`
@@ -494,10 +498,10 @@ impl TaskLocals {
494498

495499
/// Manually provide the contextvars for the current task.
496500
pub fn with_context(self, context: Bound<PyAny>) -> Self {
497-
Self {
498-
context: Arc::new(context.into()),
499-
..self
500-
}
501+
Self(Arc::new(TaskLocalsInner {
502+
event_loop: self.0.event_loop.clone_ref(context.py()),
503+
context: context.into(),
504+
}))
501505
}
502506

503507
/// Capture the current task's contextvars
@@ -507,12 +511,12 @@ impl TaskLocals {
507511

508512
/// Get a reference to the event loop
509513
pub fn event_loop<'p>(&self, py: Python<'p>) -> Bound<'p, PyAny> {
510-
self.event_loop.clone_ref(py).into_bound(py)
514+
self.0.event_loop.clone_ref(py).into_bound(py)
511515
}
512516

513517
/// Get a reference to the python context
514518
pub fn context<'p>(&self, py: Python<'p>) -> Bound<'p, PyAny> {
515-
self.context.clone_ref(py).into_bound(py)
519+
self.0.context.clone_ref(py).into_bound(py)
516520
}
517521

518522
/// Create a clone of the TaskLocals. No longer uses the runtime, use `clone` instead.
@@ -526,10 +530,7 @@ impl Clone for TaskLocals {
526530
/// Create a clone of the TaskLocals by incrementing the reference counters of the event loop and
527531
/// contextvars.
528532
fn clone(&self) -> Self {
529-
Self {
530-
event_loop: self.event_loop.clone(),
531-
context: self.context.clone(),
532-
}
533+
Self(self.0.clone())
533534
}
534535
}
535536

0 commit comments

Comments
 (0)