Skip to content

Commit 4fc3b6d

Browse files
committed
Tweak the API of owning TLS
1 parent f6ad78d commit 4fc3b6d

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

text/0000-tls-overhaul.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ macro_rules! tls(
238238
)
239239

240240
pub struct Key<T: 'static> { /* ... */ }
241-
pub struct Ref<T: 'static> { /* ... */ }
242241

243242
impl<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

Comments
 (0)