File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,16 @@ struct Entry<T> {
119
119
value : UnsafeCell < MaybeUninit < T > > ,
120
120
}
121
121
122
+ impl < T > Drop for Entry < T > {
123
+ fn drop ( & mut self ) {
124
+ unsafe {
125
+ if * self . present . get_mut ( ) {
126
+ ptr:: drop_in_place ( ( * self . value . get ( ) ) . as_mut_ptr ( ) ) ;
127
+ }
128
+ }
129
+ }
130
+ }
131
+
122
132
// ThreadLocal is always Sync, even if T isn't
123
133
unsafe impl < T : Send > Sync for ThreadLocal < T > { }
124
134
@@ -602,6 +612,23 @@ mod tests {
602
612
assert_eq ! ( vec![ 1 , 2 , 3 ] , v) ;
603
613
}
604
614
615
+ #[ test]
616
+ fn test_drop ( ) {
617
+ let local = ThreadLocal :: new ( ) ;
618
+ struct Dropped ( Arc < AtomicUsize > ) ;
619
+ impl Drop for Dropped {
620
+ fn drop ( & mut self ) {
621
+ self . 0 . fetch_add ( 1 , Relaxed ) ;
622
+ }
623
+ }
624
+
625
+ let dropped = Arc :: new ( AtomicUsize :: new ( 0 ) ) ;
626
+ local. get_or ( || Dropped ( dropped. clone ( ) ) ) ;
627
+ assert_eq ! ( dropped. load( Relaxed ) , 0 ) ;
628
+ drop ( local) ;
629
+ assert_eq ! ( dropped. load( Relaxed ) , 1 ) ;
630
+ }
631
+
605
632
#[ test]
606
633
fn is_sync ( ) {
607
634
fn foo < T : Sync > ( ) { }
You can’t perform that action at this time.
0 commit comments