File tree 4 files changed +59
-56
lines changed
4 files changed +59
-56
lines changed Original file line number Diff line number Diff line change @@ -197,6 +197,9 @@ mod utils;
197
197
#[ doc( inline) ]
198
198
pub use async_attributes:: { main, test} ;
199
199
200
+ #[ cfg( feature = "std" ) ]
201
+ mod macros;
202
+
200
203
cfg_std ! {
201
204
pub mod future;
202
205
pub mod io;
@@ -205,7 +208,6 @@ cfg_std! {
205
208
pub mod stream;
206
209
pub mod sync;
207
210
pub mod task;
208
- mod macros;
209
211
}
210
212
211
213
cfg_default ! {
Original file line number Diff line number Diff line change @@ -165,3 +165,55 @@ macro_rules! eprintln {
165
165
}
166
166
) ;
167
167
}
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
+ }
Original file line number Diff line number Diff line change @@ -40,10 +40,10 @@ cfg_std! {
40
40
pub use crate :: io:: prelude:: WriteExt as _;
41
41
}
42
42
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
+ }
47
47
48
48
cfg_unstable ! {
49
49
#[ doc( no_inline) ]
Original file line number Diff line number Diff line change @@ -5,57 +5,6 @@ use std::sync::atomic::{AtomicU32, Ordering};
5
5
6
6
use crate :: task:: Task ;
7
7
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
-
59
8
/// The key for accessing a task-local value.
60
9
///
61
10
/// Every task-local value is lazily initialized on first access and destroyed when the task
You can’t perform that action at this time.
0 commit comments