Skip to content

Commit cc58d95

Browse files
committed
Make TaskLocals properly cloneable.
1 parent 0ee77f2 commit cc58d95

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

src/async_std.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ impl ContextExt for AsyncStdRuntime {
9494
.try_with(|c| {
9595
c.borrow()
9696
.as_ref()
97-
.map(|locals| locals.clone_ref())
97+
.map(|locals| locals.clone())
9898
})
9999
.unwrap_or_default()
100100
}

src/generic.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,11 @@ where
597597
let future_tx2 = future_tx1.clone_ref(py);
598598

599599
R::spawn(async move {
600-
let locals2 = locals.clone_ref();
600+
let locals2 = locals.clone();
601601

602602
if let Err(e) = R::spawn(async move {
603603
let result = R::scope(
604-
locals2.clone_ref(),
604+
locals2.clone(),
605605
Cancellable::new_with_cancel_rx(fut, cancel_rx),
606606
)
607607
.await;
@@ -1002,11 +1002,11 @@ where
10021002
let future_tx2 = future_tx1.clone_ref(py);
10031003

10041004
R::spawn_local(async move {
1005-
let locals2 = locals.clone_ref();
1005+
let locals2 = locals.clone();
10061006

10071007
if let Err(e) = R::spawn_local(async move {
10081008
let result = R::scope_local(
1009-
locals2.clone_ref(),
1009+
locals2.clone(),
10101010
Cancellable::new_with_cancel_rx(fut, cancel_rx),
10111011
)
10121012
.await;
@@ -1510,7 +1510,7 @@ impl SenderGlue {
15101510
self.tx
15111511
.lock()
15121512
.unwrap()
1513-
.send(py, self.locals.clone_ref(), item)
1513+
.send(py, self.locals.clone(), item)
15141514
})
15151515
}
15161516
pub fn close(&mut self) -> PyResult<()> {

src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
//! let locals = pyo3_async_runtimes::TaskLocals::with_running_loop(py)?.copy_context(py)?;
9494
//!
9595
//! // Convert the async move { } block to a Python awaitable
96-
//! pyo3_async_runtimes::tokio::future_into_py_with_locals(py, locals.clone_ref(), async move {
96+
//! pyo3_async_runtimes::tokio::future_into_py_with_locals(py, locals.clone(), async move {
9797
//! let py_sleep = Python::attach(|py| {
9898
//! // Sometimes we need to call other async Python functions within
9999
//! // this future. In order for this to work, we need to track the
@@ -162,9 +162,9 @@
162162
//!
163163
//! pyo3_async_runtimes::tokio::future_into_py_with_locals(
164164
//! py,
165-
//! locals.clone_ref(),
165+
//! locals.clone(),
166166
//! // Store the current locals in task-local data
167-
//! pyo3_async_runtimes::tokio::scope(locals.clone_ref(), async move {
167+
//! pyo3_async_runtimes::tokio::scope(locals.clone(), async move {
168168
//! let py_sleep = Python::attach(|py| {
169169
//! pyo3_async_runtimes::into_future_with_locals(
170170
//! // Now we can get the current locals through task-local data
@@ -189,9 +189,9 @@
189189
//!
190190
//! pyo3_async_runtimes::tokio::future_into_py_with_locals(
191191
//! py,
192-
//! locals.clone_ref(),
192+
//! locals.clone(),
193193
//! // Store the current locals in task-local data
194-
//! pyo3_async_runtimes::tokio::scope(locals.clone_ref(), async move {
194+
//! pyo3_async_runtimes::tokio::scope(locals.clone(), async move {
195195
//! let py_sleep = Python::attach(|py| {
196196
//! pyo3_async_runtimes::into_future_with_locals(
197197
//! &pyo3_async_runtimes::tokio::get_current_locals(py)?,
@@ -514,10 +514,12 @@ impl TaskLocals {
514514
pub fn context<'p>(&self, py: Python<'p>) -> Bound<'p, PyAny> {
515515
self.context.clone_ref(py).into_bound(py)
516516
}
517+
}
517518

519+
impl Clone for TaskLocals {
518520
/// Create a clone of the TaskLocals by incrementing the reference counters of the event loop and
519521
/// contextvars.
520-
pub fn clone_ref(&self) -> Self {
522+
fn clone(&self) -> Self {
521523
Self {
522524
event_loop: self.event_loop.clone(),
523525
context: self.context.clone(),

src/tokio.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl ContextExt for TokioRuntime {
111111
TASK_LOCALS
112112
.try_with(|c| {
113113
c.get()
114-
.map(|locals| locals.clone_ref())
114+
.map(|locals| locals.clone())
115115
})
116116
.unwrap_or_default()
117117
}

0 commit comments

Comments
 (0)