@@ -68,6 +68,7 @@ use std::cell::RefCell;
68
68
use std:: collections:: HashMap ;
69
69
use std:: fmt;
70
70
use std:: hash:: { BuildHasherDefault , Hasher } ;
71
+ use std:: marker:: PhantomData ;
71
72
use std:: sync:: Arc ;
72
73
73
74
#[ cfg( feature = "trace" ) ]
@@ -290,10 +291,14 @@ impl Context {
290
291
/// assert_eq!(Context::current().get::<ValueA>(), None);
291
292
/// ```
292
293
pub fn attach ( self ) -> ContextGuard {
293
- let prior = CURRENT_CONTEXT
294
+ let previous_cx = CURRENT_CONTEXT
294
295
. try_with ( |current| current. replace ( self ) )
295
296
. ok ( ) ;
296
- ContextGuard ( prior)
297
+
298
+ ContextGuard {
299
+ previous_cx,
300
+ _marker : PhantomData ,
301
+ }
297
302
}
298
303
}
299
304
@@ -307,11 +312,15 @@ impl fmt::Debug for Context {
307
312
308
313
/// A guard that resets the current context to the prior context when dropped.
309
314
#[ 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
+ }
311
320
312
321
impl Drop for ContextGuard {
313
322
fn drop ( & mut self ) {
314
- if let Some ( previous_cx) = self . 0 . take ( ) {
323
+ if let Some ( previous_cx) = self . previous_cx . take ( ) {
315
324
let _ = CURRENT_CONTEXT . try_with ( |current| current. replace ( previous_cx) ) ;
316
325
}
317
326
}
0 commit comments