]> git.lizzy.rs Git - rust.git/commit
std: Add a variant of thread locals with const init
authorAlex Crichton <alex@alexcrichton.com>
Tue, 23 Mar 2021 18:04:29 +0000 (11:04 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Fri, 16 Apr 2021 16:21:38 +0000 (09:21 -0700)
commitc6eea222a9cafeeeedfa1767232328710b350447
tree445d5ae2cf15dca79fe4ac1611e83aedccb08ac6
parentf1ca558db189690a9774713306c94550681ac590
std: Add a variant of thread locals with const init

This commit adds a variant of the `thread_local!` macro as a new
`thread_local_const_init!` macro which requires that the initialization
expression is constant (e.g. could be stuck into a `const` if so
desired). This form of thread local allows for a more efficient
implementation of `LocalKey::with` both if the value has a destructor
and if it doesn't. If the value doesn't have a destructor then `with`
should desugar to exactly as-if you use `#[thread_local]` given
sufficient inlining.

The purpose of this new form of thread locals is to precisely be
equivalent to `#[thread_local]` on platforms where possible for values
which fit the bill (those without destructors). This should help close
the gap in performance between `thread_local!`, which is safe, relative
to `#[thread_local]`, which is not easy to use in a portable fashion.
library/std/src/lib.rs
library/std/src/thread/local.rs
library/std/src/thread/local/tests.rs
library/std/src/thread/mod.rs
src/test/ui/feature-gates/thread-local-const-init.rs [new file with mode: 0644]
src/test/ui/feature-gates/thread-local-const-init.stderr [new file with mode: 0644]