@@ -276,7 +276,33 @@ pub enum LocalKeyState {
276
276
reason = "state querying was recently added" ,
277
277
issue = "27716" ) ]
278
278
pub struct AccessError {
279
- _private : ( ) ,
279
+ // whether the error was due to the key being in the Initializing
280
+ // state - false means the key was in the Destroyed state
281
+ init : bool ,
282
+ }
283
+
284
+ impl AccessError {
285
+ /// Determines whether the `AccessError` was due to the key being initialized.
286
+ ///
287
+ /// If `is_initializing` returns true, this `AccessError` was returned because
288
+ /// the key was in the `Initializing` state when it was accessed.
289
+ #[ unstable( feature = "thread_local_state" ,
290
+ reason = "state querying was recently added" ,
291
+ issue = "27716" ) ]
292
+ pub fn is_initializing ( & self ) -> bool {
293
+ self . init
294
+ }
295
+
296
+ /// Determines whether the `AccessError` was due to the key being destroyed.
297
+ ///
298
+ /// If `is_destroyed` returns true, this `AccessError` was returned because
299
+ /// the key was in the `Destroyed` state when it was accessed.
300
+ #[ unstable( feature = "thread_local_state" ,
301
+ reason = "state querying was recently added" ,
302
+ issue = "27716" ) ]
303
+ pub fn is_destroyed ( & self ) -> bool {
304
+ !self . init
305
+ }
280
306
}
281
307
282
308
#[ unstable( feature = "thread_local_state" ,
@@ -293,7 +319,11 @@ impl fmt::Debug for AccessError {
293
319
issue = "27716" ) ]
294
320
impl fmt:: Display for AccessError {
295
321
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
296
- fmt:: Display :: fmt ( "already destroyed" , f)
322
+ if self . init {
323
+ fmt:: Display :: fmt ( "currently being initialized" , f)
324
+ } else {
325
+ fmt:: Display :: fmt ( "already destroyed" , f)
326
+ }
297
327
}
298
328
}
299
329
@@ -429,8 +459,8 @@ impl<T: 'static> LocalKey<T> {
429
459
// not to enter this else block in the recursive call.
430
460
self . try_with ( f)
431
461
}
432
- LocalKeyState :: Initializing |
433
- LocalKeyState :: Destroyed => Err ( AccessError { _private : ( ) } ) ,
462
+ LocalKeyState :: Initializing => Err ( AccessError { init : true } ) ,
463
+ LocalKeyState :: Destroyed => Err ( AccessError { init : false } ) ,
434
464
LocalKeyState :: Valid => unreachable ! ( ) ,
435
465
}
436
466
}
0 commit comments