Skip to content

Commit b0a03eb

Browse files
author
Stjepan Glavina
committed
Fix task_local macro
1 parent b12d298 commit b0a03eb

File tree

4 files changed

+59
-56
lines changed

4 files changed

+59
-56
lines changed

src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ mod utils;
197197
#[doc(inline)]
198198
pub use async_attributes::{main, test};
199199

200+
#[cfg(feature = "std")]
201+
mod macros;
202+
200203
cfg_std! {
201204
pub mod future;
202205
pub mod io;
@@ -205,7 +208,6 @@ cfg_std! {
205208
pub mod stream;
206209
pub mod sync;
207210
pub mod task;
208-
mod macros;
209211
}
210212

211213
cfg_default! {

src/macros.rs

+52
Original file line numberDiff line numberDiff line change
@@ -165,3 +165,55 @@ macro_rules! eprintln {
165165
}
166166
);
167167
}
168+
169+
/// Declares task-local values.
170+
///
171+
/// The macro wraps any number of static declarations and makes them task-local. Attributes and
172+
/// visibility modifiers are allowed.
173+
///
174+
/// Each declared value is of the accessor type [`LocalKey`].
175+
///
176+
/// [`LocalKey`]: task/struct.LocalKey.html
177+
///
178+
/// # Examples
179+
///
180+
/// ```
181+
/// #
182+
/// use std::cell::Cell;
183+
///
184+
/// use async_std::task;
185+
/// use async_std::prelude::*;
186+
///
187+
/// task_local! {
188+
/// static VAL: Cell<u32> = Cell::new(5);
189+
/// }
190+
///
191+
/// task::block_on(async {
192+
/// let v = VAL.with(|c| c.get());
193+
/// assert_eq!(v, 5);
194+
/// });
195+
/// ```
196+
#[cfg(feature = "default")]
197+
#[macro_export]
198+
macro_rules! task_local {
199+
() => ();
200+
201+
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => (
202+
$(#[$attr])* $vis static $name: $crate::task::LocalKey<$t> = {
203+
#[inline]
204+
fn __init() -> $t {
205+
$init
206+
}
207+
208+
$crate::task::LocalKey {
209+
__init,
210+
__key: ::std::sync::atomic::AtomicU32::new(0),
211+
}
212+
};
213+
);
214+
215+
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
216+
$crate::task_local!($(#[$attr])* $vis static $name: $t = $init);
217+
$crate::task_local!($($rest)*);
218+
);
219+
}

src/prelude.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ cfg_std! {
4040
pub use crate::io::prelude::WriteExt as _;
4141
}
4242

43-
// cfg_default! {
44-
// #[doc(no_inline)]
45-
// pub use crate::task_local;
46-
// }
43+
cfg_default! {
44+
#[doc(no_inline)]
45+
pub use crate::task_local;
46+
}
4747

4848
cfg_unstable! {
4949
#[doc(no_inline)]

src/task/task_local.rs

-51
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,6 @@ use std::sync::atomic::{AtomicU32, Ordering};
55

66
use crate::task::Task;
77

8-
/// Declares task-local values.
9-
///
10-
/// The macro wraps any number of static declarations and makes them task-local. Attributes and
11-
/// visibility modifiers are allowed.
12-
///
13-
/// Each declared value is of the accessor type [`LocalKey`].
14-
///
15-
/// [`LocalKey`]: task/struct.LocalKey.html
16-
///
17-
/// # Examples
18-
///
19-
/// ```
20-
/// #
21-
/// use std::cell::Cell;
22-
///
23-
/// use async_std::task;
24-
/// use async_std::prelude::*;
25-
///
26-
/// task_local! {
27-
/// static VAL: Cell<u32> = Cell::new(5);
28-
/// }
29-
///
30-
/// task::block_on(async {
31-
/// let v = VAL.with(|c| c.get());
32-
/// assert_eq!(v, 5);
33-
/// });
34-
/// ```
35-
#[macro_export]
36-
macro_rules! task_local {
37-
() => ();
38-
39-
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => (
40-
$(#[$attr])* $vis static $name: $crate::task::LocalKey<$t> = {
41-
#[inline]
42-
fn __init() -> $t {
43-
$init
44-
}
45-
46-
$crate::task::LocalKey {
47-
__init,
48-
__key: ::std::sync::atomic::AtomicU32::new(0),
49-
}
50-
};
51-
);
52-
53-
($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => (
54-
$crate::task_local!($(#[$attr])* $vis static $name: $t = $init);
55-
$crate::task_local!($($rest)*);
56-
);
57-
}
58-
598
/// The key for accessing a task-local value.
609
///
6110
/// Every task-local value is lazily initialized on first access and destroyed when the task

0 commit comments

Comments
 (0)