@@ -238,7 +238,6 @@ macro_rules! tls(
238238)
239239
240240pub struct Key <T : 'static > { /* ... */ }
241- pub struct Ref <T : 'static > { /* ... */ }
242241
243242impl <T : 'static > Key <T > {
244243 /// Access this TLS variable, lazily initializing it if necessary.
@@ -249,10 +248,8 @@ impl<T: 'static> Key<T> {
249248 ///
250249 /// This function can return `None` for the same reasons of static TLS
251250 /// returning `None` (destructors are running or may have run).
252- pub fn get (& 'static self ) -> Option <Ref < T >> { /* ... */ }
251+ pub fn with < R > (& 'static self , f : | Option <& T > | -> R ) -> R { /* ... */ }
253252}
254-
255- impl <T : 'static > Deref <T > for Ref <T > { /* ... */ }
256253```
257254
258255### Destructors
@@ -279,9 +276,14 @@ some of the minor downsides.
279276
280277### Variations
281278
282- Like the scoped TLS varations, the primary way this API could be altered would
283- be to return ` Ref<T> ` instead of an ` Option ` from ` get ` , while then providing a
284- function to test whether a value is being destroyed.
279+ Like the scoped TLS variation, this key has a ` with ` function instead of the
280+ normally expected ` get ` function (returning a reference). One possible
281+ alternative would be to yield ` &T ` instead of ` Option<&T> ` and ` panic! ` if the
282+ variable has been destroyed. Another possible alternative is to have a ` get `
283+ function returning a ` Ref<T> ` . Currently this is unsafe, however, as there is no
284+ way to ensure that ` Ref<T> ` does not satisfy ` 'static ` . If the returned
285+ reference satisfies ` 'static ` , then it's possible for TLS values to reference
286+ each other after one has been destroyed, causing a use-after-free.
285287
286288# Drawbacks
287289
0 commit comments