@@ -195,64 +195,20 @@ macro_rules! __thread_local_inner {
195
195
}
196
196
}
197
197
198
- /// Indicator of the state of a thread local storage key.
199
- #[ unstable( feature = "thread_local_state" ,
200
- reason = "state querying was recently added" ,
201
- issue = "27716" ) ]
202
- #[ derive( Debug , Eq , PartialEq , Copy , Clone ) ]
203
- pub enum LocalKeyState {
204
- /// All keys are in this state whenever a thread starts. Keys will
205
- /// transition to the `Valid` state once the first call to [`with`] happens
206
- /// and the initialization expression succeeds.
207
- ///
208
- /// Keys in the `Uninitialized` state will yield a reference to the closure
209
- /// passed to [`with`] so long as the initialization routine does not panic.
210
- ///
211
- /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
212
- Uninitialized ,
213
-
214
- /// Once a key has been accessed successfully, it will enter the `Valid`
215
- /// state. Keys in the `Valid` state will remain so until the thread exits,
216
- /// at which point the destructor will be run and the key will enter the
217
- /// `Destroyed` state.
218
- ///
219
- /// Keys in the `Valid` state will be guaranteed to yield a reference to the
220
- /// closure passed to [`with`].
221
- ///
222
- /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
223
- Valid ,
224
-
225
- /// When a thread exits, the destructors for keys will be run (if
226
- /// necessary). While a destructor is running, and possibly after a
227
- /// destructor has run, a key is in the `Destroyed` state.
228
- ///
229
- /// Keys in the `Destroyed` states will trigger a panic when accessed via
230
- /// [`with`].
231
- ///
232
- /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
233
- Destroyed ,
234
- }
235
-
236
198
/// An error returned by [`LocalKey::try_with`](struct.LocalKey.html#method.try_with).
237
- #[ unstable( feature = "thread_local_state" ,
238
- reason = "state querying was recently added" ,
239
- issue = "27716" ) ]
199
+ #[ stable( feature = "thread_local_try_with" , since = "1.26.0" ) ]
240
200
pub struct AccessError {
241
201
_private : ( ) ,
242
202
}
243
203
244
- #[ unstable( feature = "thread_local_state" ,
245
- reason = "state querying was recently added" ,
246
- issue = "27716" ) ]
204
+ #[ stable( feature = "thread_local_try_with" , since = "1.26.0" ) ]
247
205
impl fmt:: Debug for AccessError {
248
206
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
249
207
f. debug_struct ( "AccessError" ) . finish ( )
250
208
}
251
209
}
252
210
253
- #[ unstable( feature = "thread_local_state" ,
254
- reason = "state querying was recently added" ,
255
- issue = "27716" ) ]
211
+ #[ stable( feature = "thread_local_try_with" , since = "1.26.0" ) ]
256
212
impl fmt:: Display for AccessError {
257
213
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
258
214
fmt:: Display :: fmt ( "already destroyed" , f)
@@ -312,64 +268,21 @@ impl<T: 'static> LocalKey<T> {
312
268
( * ptr) . as_ref ( ) . unwrap ( )
313
269
}
314
270
315
- /// Query the current state of this key.
316
- ///
317
- /// A key is initially in the `Uninitialized` state whenever a thread
318
- /// starts. It will remain in this state up until the first call to [`with`]
319
- /// within a thread has run the initialization expression successfully.
320
- ///
321
- /// Once the initialization expression succeeds, the key transitions to the
322
- /// `Valid` state which will guarantee that future calls to [`with`] will
323
- /// succeed within the thread. Some keys might skip the `Uninitialized`
324
- /// state altogether and start in the `Valid` state as an optimization
325
- /// (e.g. keys initialized with a constant expression), but no guarantees
326
- /// are made.
327
- ///
328
- /// When a thread exits, each key will be destroyed in turn, and as keys are
329
- /// destroyed they will enter the `Destroyed` state just before the
330
- /// destructor starts to run. Keys may remain in the `Destroyed` state after
331
- /// destruction has completed. Keys without destructors (e.g. with types
332
- /// that are [`Copy`]), may never enter the `Destroyed` state.
333
- ///
334
- /// Keys in the `Uninitialized` state can be accessed so long as the
335
- /// initialization does not panic. Keys in the `Valid` state are guaranteed
336
- /// to be able to be accessed. Keys in the `Destroyed` state will panic on
337
- /// any call to [`with`].
338
- ///
339
- /// [`with`]: ../../std/thread/struct.LocalKey.html#method.with
340
- /// [`Copy`]: ../../std/marker/trait.Copy.html
341
- #[ unstable( feature = "thread_local_state" ,
342
- reason = "state querying was recently added" ,
343
- issue = "27716" ) ]
344
- pub fn state ( & ' static self ) -> LocalKeyState {
345
- unsafe {
346
- match ( self . inner ) ( ) {
347
- Some ( cell) => {
348
- match * cell. get ( ) {
349
- Some ( ..) => LocalKeyState :: Valid ,
350
- None => LocalKeyState :: Uninitialized ,
351
- }
352
- }
353
- None => LocalKeyState :: Destroyed ,
354
- }
355
- }
356
- }
357
-
358
271
/// Acquires a reference to the value in this TLS key.
359
272
///
360
273
/// This will lazily initialize the value if this thread has not referenced
361
274
/// this key yet. If the key has been destroyed (which may happen if this is called
362
- /// in a destructor), this function will return a ThreadLocalError.
275
+ /// in a destructor), this function will return a ` ThreadLocalError` .
363
276
///
364
277
/// # Panics
365
278
///
366
279
/// This function will still `panic!()` if the key is uninitialized and the
367
280
/// key's initializer panics.
368
- #[ unstable( feature = "thread_local_state" ,
369
- reason = "state querying was recently added" ,
370
- issue = "27716" ) ]
281
+ #[ stable( feature = "thread_local_try_with" , since = "1.26.0" ) ]
371
282
pub fn try_with < F , R > ( & ' static self , f : F ) -> Result < R , AccessError >
372
- where F : FnOnce ( & T ) -> R {
283
+ where
284
+ F : FnOnce ( & T ) -> R ,
285
+ {
373
286
unsafe {
374
287
let slot = ( self . inner ) ( ) . ok_or ( AccessError {
375
288
_private : ( ) ,
@@ -530,7 +443,6 @@ pub mod os {
530
443
mod tests {
531
444
use sync:: mpsc:: { channel, Sender } ;
532
445
use cell:: { Cell , UnsafeCell } ;
533
- use super :: LocalKeyState ;
534
446
use thread;
535
447
536
448
struct Foo ( Sender < ( ) > ) ;
@@ -569,21 +481,13 @@ mod tests {
569
481
struct Foo ;
570
482
impl Drop for Foo {
571
483
fn drop ( & mut self ) {
572
- assert ! ( FOO . state ( ) == LocalKeyState :: Destroyed ) ;
484
+ assert ! ( FOO . try_with ( |_| ( ) ) . is_err ( ) ) ;
573
485
}
574
486
}
575
- fn foo ( ) -> Foo {
576
- assert ! ( FOO . state( ) == LocalKeyState :: Uninitialized ) ;
577
- Foo
578
- }
579
- thread_local ! ( static FOO : Foo = foo( ) ) ;
487
+ thread_local ! ( static FOO : Foo = Foo ) ;
580
488
581
489
thread:: spawn ( || {
582
- assert ! ( FOO . state( ) == LocalKeyState :: Uninitialized ) ;
583
- FOO . with ( |_| {
584
- assert ! ( FOO . state( ) == LocalKeyState :: Valid ) ;
585
- } ) ;
586
- assert ! ( FOO . state( ) == LocalKeyState :: Valid ) ;
490
+ assert ! ( FOO . try_with( |_| ( ) ) . is_ok( ) ) ;
587
491
} ) . join ( ) . ok ( ) . unwrap ( ) ;
588
492
}
589
493
@@ -613,7 +517,7 @@ mod tests {
613
517
fn drop ( & mut self ) {
614
518
unsafe {
615
519
HITS += 1 ;
616
- if K2 . state ( ) == LocalKeyState :: Destroyed {
520
+ if K2 . try_with ( |_| ( ) ) . is_err ( ) {
617
521
assert_eq ! ( HITS , 3 ) ;
618
522
} else {
619
523
if HITS == 1 {
@@ -629,7 +533,7 @@ mod tests {
629
533
fn drop ( & mut self ) {
630
534
unsafe {
631
535
HITS += 1 ;
632
- assert ! ( K1 . state ( ) != LocalKeyState :: Destroyed ) ;
536
+ assert ! ( K1 . try_with ( |_| ( ) ) . is_ok ( ) ) ;
633
537
assert_eq ! ( HITS , 2 ) ;
634
538
K1 . with ( |s| * s. get ( ) = Some ( S1 ) ) ;
635
539
}
@@ -648,7 +552,7 @@ mod tests {
648
552
649
553
impl Drop for S1 {
650
554
fn drop ( & mut self ) {
651
- assert ! ( K1 . state ( ) == LocalKeyState :: Destroyed ) ;
555
+ assert ! ( K1 . try_with ( |_| ( ) ) . is_err ( ) ) ;
652
556
}
653
557
}
654
558
@@ -672,9 +576,7 @@ mod tests {
672
576
fn drop ( & mut self ) {
673
577
let S1 ( ref tx) = * self ;
674
578
unsafe {
675
- if K2 . state ( ) != LocalKeyState :: Destroyed {
676
- K2 . with ( |s| * s. get ( ) = Some ( Foo ( tx. clone ( ) ) ) ) ;
677
- }
579
+ let _ = K2 . try_with ( |s| * s. get ( ) = Some ( Foo ( tx. clone ( ) ) ) ) ;
678
580
}
679
581
}
680
582
}
0 commit comments