@@ -232,6 +232,32 @@ pub enum LocalKeyState {
232232 Destroyed ,
233233}
234234
235+ /// An error returned by [`LocalKey::try_with`](struct.LocalKey.html#method.try_with).
236+ #[ unstable( feature = "thread_local_state" ,
237+ reason = "state querying was recently added" ,
238+ issue = "27716" ) ]
239+ pub struct AccessError {
240+ _private : ( ) ,
241+ }
242+
243+ #[ unstable( feature = "thread_local_state" ,
244+ reason = "state querying was recently added" ,
245+ issue = "27716" ) ]
246+ impl fmt:: Debug for AccessError {
247+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
248+ f. debug_struct ( "AccessError" ) . finish ( )
249+ }
250+ }
251+
252+ #[ unstable( feature = "thread_local_state" ,
253+ reason = "state querying was recently added" ,
254+ issue = "27716" ) ]
255+ impl fmt:: Display for AccessError {
256+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
257+ fmt:: Display :: fmt ( "already destroyed" , f)
258+ }
259+ }
260+
235261impl < T : ' static > LocalKey < T > {
236262 #[ doc( hidden) ]
237263 #[ unstable( feature = "thread_local_internals" ,
@@ -331,6 +357,32 @@ impl<T: 'static> LocalKey<T> {
331357 }
332358 }
333359 }
360+
361+ /// Acquires a reference to the value in this TLS key.
362+ ///
363+ /// This will lazily initialize the value if this thread has not referenced
364+ /// this key yet. If the key has been destroyed (which may happen if this is called
365+ /// in a destructor), this function will return a ThreadLocalError.
366+ ///
367+ /// # Panics
368+ ///
369+ /// This function will still `panic!()` if the key is uninitialized and the
370+ /// key's initializer panics.
371+ #[ unstable( feature = "thread_local_state" ,
372+ reason = "state querying was recently added" ,
373+ issue = "27716" ) ]
374+ pub fn try_with < F , R > ( & ' static self , f : F ) -> Result < R , AccessError >
375+ where F : FnOnce ( & T ) -> R {
376+ unsafe {
377+ let slot = ( self . inner ) ( ) . ok_or ( AccessError {
378+ _private : ( ) ,
379+ } ) ?;
380+ Ok ( f ( match * slot. get ( ) {
381+ Some ( ref inner) => inner,
382+ None => self . init ( slot) ,
383+ } ) )
384+ }
385+ }
334386}
335387
336388#[ doc( hidden) ]
0 commit comments