Allow any expression blocks in thread_local!

This commit is contained in:
Nikolai Vazquez 2023-10-03 14:44:41 -04:00 committed by David Tolnay
parent 867d39cdf6
commit fc75a4e146
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82

View File

@ -166,7 +166,10 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
/// ``` /// ```
/// use std::cell::Cell; /// use std::cell::Cell;
/// thread_local! { /// thread_local! {
/// pub static FOO: Cell<u32> = const { Cell::new(1) }; /// pub static FOO: Cell<u32> = const {
/// let value = 1;
/// Cell::new(value)
/// };
/// } /// }
/// ///
/// assert_eq!(FOO.get(), 1); /// assert_eq!(FOO.get(), 1);
@ -186,12 +189,12 @@ macro_rules! thread_local {
// empty (base case for the recursion) // empty (base case for the recursion)
() => {}; () => {};
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }; $($rest:tt)*) => ( ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const $init:block; $($rest:tt)*) => (
$crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init); $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
$crate::thread_local!($($rest)*); $crate::thread_local!($($rest)*);
); );
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const { $init:expr }) => ( ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = const $init:block) => (
$crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init); $crate::thread::local_impl::thread_local_inner!($(#[$attr])* $vis $name, $t, const $init);
); );