@@ -413,4 +413,46 @@ mod tests {
413
413
true
414
414
} ) ) ;
415
415
}
416
+
417
+ #[ test]
418
+ #[ ignore = "overlapping contexts are not supported yet" ]
419
+ fn overlapping_contexts ( ) {
420
+ #[ derive( Debug , PartialEq ) ]
421
+ struct ValueA ( & ' static str ) ;
422
+ #[ derive( Debug , PartialEq ) ]
423
+ struct ValueB ( u64 ) ;
424
+
425
+ let outer_guard = Context :: new ( ) . with_value ( ValueA ( "a" ) ) . attach ( ) ;
426
+
427
+ // Only value `a` is set
428
+ let current = Context :: current ( ) ;
429
+ assert_eq ! ( current. get( ) , Some ( & ValueA ( "a" ) ) ) ;
430
+ assert_eq ! ( current. get:: <ValueB >( ) , None ) ;
431
+
432
+ let inner_guard = Context :: current_with_value ( ValueB ( 42 ) ) . attach ( ) ;
433
+ // Both values are set in inner context
434
+ let current = Context :: current ( ) ;
435
+ assert_eq ! ( current. get( ) , Some ( & ValueA ( "a" ) ) ) ;
436
+ assert_eq ! ( current. get( ) , Some ( & ValueB ( 42 ) ) ) ;
437
+
438
+ assert ! ( Context :: map_current( |cx| {
439
+ assert_eq!( cx. get( ) , Some ( & ValueA ( "a" ) ) ) ;
440
+ assert_eq!( cx. get( ) , Some ( & ValueB ( 42 ) ) ) ;
441
+ true
442
+ } ) ) ;
443
+
444
+ drop ( outer_guard) ;
445
+
446
+ // `inner_guard` is still alive so both `ValueA` and `ValueB` should still be accessible
447
+ let current = Context :: current ( ) ;
448
+ assert_eq ! ( current. get( ) , Some ( & ValueA ( "a" ) ) ) ;
449
+ assert_eq ! ( current. get( ) , Some ( & ValueB ( 42 ) ) ) ;
450
+
451
+ drop ( inner_guard) ;
452
+
453
+ // Both guards are dropped and neither value should be accessible.
454
+ let current = Context :: current ( ) ;
455
+ assert_eq ! ( current. get:: <ValueA >( ) , None ) ;
456
+ assert_eq ! ( current. get:: <ValueB >( ) , None ) ;
457
+ }
416
458
}
0 commit comments