6 static K1: StaticKey = StaticKey::new(None);
7 static K2: StaticKey = StaticKey::new(None);
10 assert!(K1.get().is_null());
11 assert!(K2.get().is_null());
12 K1.set(ptr::invalid_mut(1));
13 K2.set(ptr::invalid_mut(2));
14 assert_eq!(K1.get() as usize, 1);
15 assert_eq!(K2.get() as usize, 2);
21 use crate::mem::ManuallyDrop;
25 unsafe extern "C" fn destruct(ptr: *mut u8) {
26 drop(Arc::from_raw(ptr as *const ()));
29 static KEY: StaticKey = StaticKey::new(Some(destruct));
31 let shared1 = Arc::new(());
32 let shared2 = Arc::clone(&shared1);
35 assert!(KEY.get().is_null());
36 KEY.set(Arc::into_raw(shared1) as *mut u8);
39 thread::spawn(move || unsafe {
40 assert!(KEY.get().is_null());
41 KEY.set(Arc::into_raw(shared2) as *mut u8);
46 // Leak the Arc, let the TLS destructor clean it up.
47 let shared1 = unsafe { ManuallyDrop::new(Arc::from_raw(KEY.get() as *const ())) };
49 Arc::strong_count(&shared1),
51 "destructor should have dropped the other reference on thread exit"