Skip to content

Commit c5297d2

Browse files
authored
Ensure context guard is !Send (#239)
1 parent 0841b9a commit c5297d2

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/api/context/mod.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ use std::cell::RefCell;
6868
use std::collections::HashMap;
6969
use std::fmt;
7070
use std::hash::{BuildHasherDefault, Hasher};
71+
use std::marker::PhantomData;
7172
use std::sync::Arc;
7273

7374
#[cfg(feature = "trace")]
@@ -290,10 +291,14 @@ impl Context {
290291
/// assert_eq!(Context::current().get::<ValueA>(), None);
291292
/// ```
292293
pub fn attach(self) -> ContextGuard {
293-
let prior = CURRENT_CONTEXT
294+
let previous_cx = CURRENT_CONTEXT
294295
.try_with(|current| current.replace(self))
295296
.ok();
296-
ContextGuard(prior)
297+
298+
ContextGuard {
299+
previous_cx,
300+
_marker: PhantomData,
301+
}
297302
}
298303
}
299304

@@ -307,11 +312,15 @@ impl fmt::Debug for Context {
307312

308313
/// A guard that resets the current context to the prior context when dropped.
309314
#[allow(missing_debug_implementations)]
310-
pub struct ContextGuard(Option<Context>);
315+
pub struct ContextGuard {
316+
previous_cx: Option<Context>,
317+
// ensure this type is !Send as it relies on thread locals
318+
_marker: PhantomData<*const ()>,
319+
}
311320

312321
impl Drop for ContextGuard {
313322
fn drop(&mut self) {
314-
if let Some(previous_cx) = self.0.take() {
323+
if let Some(previous_cx) = self.previous_cx.take() {
315324
let _ = CURRENT_CONTEXT.try_with(|current| current.replace(previous_cx));
316325
}
317326
}

0 commit comments

Comments
 (0)