tokio_async_attributes/lib.rs
1#![doc(html_root_url = "https://docs.rs/tokio-macros/0.2.5")]
2#![allow(clippy::needless_doctest_main)]
3#![warn(
4 missing_debug_implementations,
5 missing_docs,
6 rust_2018_idioms,
7 unreachable_pub
8)]
9#![deny(intra_doc_link_resolution_failure)]
10#![doc(test(
11 no_crate_inject,
12 attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
13))]
14
15//! Macros for use with Tokio
16
17// This `extern` is required for older `rustc` versions but newer `rustc`
18// versions warn about the unused `extern crate`.
19#[allow(unused_extern_crates)]
20extern crate proc_macro;
21
22mod entry;
23mod select;
24
25use proc_macro::TokenStream;
26
27/// Marks async function to be executed by selected runtime.
28///
29/// ## Options:
30///
31///
32/// - `core_threads=n` - Sets core threads to `n` (requires `rt-threaded` feature).
33/// - `max_threads=n` - Sets max threads to `n` (requires `rt-core` or `rt-threaded` feature).
34///
35/// ## Function arguments:
36///
37/// Arguments are allowed for any functions aside from `main` which is special
38///
39/// ## Usage
40///
41/// ### Using default
42///
43/// ```rust
44/// #[tokio::main]
45/// async fn main() {
46/// println!("Hello world");
47/// }
48/// ```
49///
50/// ### Set number of core threads
51///
52/// ```rust
53/// #[tokio::main(core_threads = 1)]
54/// async fn main() {
55/// println!("Hello world");
56/// }
57/// ```
58#[proc_macro_attribute]
59#[cfg(not(test))] // Work around for rust-lang/rust#62127
60pub fn main_threaded(args: TokenStream, item: TokenStream) -> TokenStream {
61 entry::main(args, item, true)
62}
63
64/// Marks async function to be executed by selected runtime.
65///
66/// ## Options:
67///
68/// - `basic_scheduler` - All tasks are executed on the current thread.
69/// - `threaded_scheduler` - Uses the multi-threaded scheduler. Used by default (requires `rt-threaded` feature).
70///
71/// ## Function arguments:
72///
73/// Arguments are allowed for any functions aside from `main` which is special
74///
75/// ## Usage
76///
77/// ### Using default
78///
79/// ```rust
80/// #[tokio::main]
81/// async fn main() {
82/// println!("Hello world");
83/// }
84/// ```
85///
86/// ### Select runtime
87///
88/// ```rust
89/// #[tokio::main(basic_scheduler)]
90/// async fn main() {
91/// println!("Hello world");
92/// }
93/// ```
94#[proc_macro_attribute]
95#[cfg(not(test))] // Work around for rust-lang/rust#62127
96pub fn main(args: TokenStream, item: TokenStream) -> TokenStream {
97 entry::old::main(args, item)
98}
99
100/// Marks async function to be executed by selected runtime.
101///
102/// ## Options:
103///
104/// - `max_threads=n` - Sets max threads to `n`.
105///
106/// ## Function arguments:
107///
108/// Arguments are allowed for any functions aside from `main` which is special
109///
110/// ## Usage
111///
112/// ### Using default
113///
114/// ```rust
115/// #[tokio::main]
116/// async fn main() {
117/// println!("Hello world");
118/// }
119/// ```
120#[proc_macro_attribute]
121#[cfg(not(test))] // Work around for rust-lang/rust#62127
122pub fn main_basic(args: TokenStream, item: TokenStream) -> TokenStream {
123 entry::main(args, item, false)
124}
125
126/// Marks async function to be executed by runtime, suitable to test environment
127///
128/// ## Options:
129///
130/// - `core_threads=n` - Sets core threads to `n` (requires `rt-threaded` feature).
131/// - `max_threads=n` - Sets max threads to `n` (requires `rt-core` or `rt-threaded` feature).
132///
133/// ## Usage
134///
135/// ### Select runtime
136///
137/// ```no_run
138/// #[tokio::test(core_threads = 1)]
139/// async fn my_test() {
140/// assert!(true);
141/// }
142/// ```
143///
144/// ### Using default
145///
146/// ```no_run
147/// #[tokio::test]
148/// async fn my_test() {
149/// assert!(true);
150/// }
151/// ```
152#[proc_macro_attribute]
153pub fn test_threaded(args: TokenStream, item: TokenStream) -> TokenStream {
154 entry::test(args, item, true)
155}
156
157/// Marks async function to be executed by runtime, suitable to test environment
158///
159/// ## Options:
160///
161/// - `basic_scheduler` - All tasks are executed on the current thread. Used by default.
162/// - `threaded_scheduler` - Use multi-threaded scheduler (requires `rt-threaded` feature).
163///
164/// ## Usage
165///
166/// ### Select runtime
167///
168/// ```no_run
169/// #[tokio::test(threaded_scheduler)]
170/// async fn my_test() {
171/// assert!(true);
172/// }
173/// ```
174///
175/// ### Using default
176///
177/// ```no_run
178/// #[tokio::test]
179/// async fn my_test() {
180/// assert!(true);
181/// }
182/// ```
183#[proc_macro_attribute]
184pub fn test(args: TokenStream, item: TokenStream) -> TokenStream {
185 entry::old::test(args, item)
186}
187
188/// Marks async function to be executed by runtime, suitable to test environment
189///
190/// ## Options:
191///
192/// - `max_threads=n` - Sets max threads to `n`.
193///
194/// ## Usage
195///
196/// ```no_run
197/// #[tokio::test]
198/// async fn my_test() {
199/// assert!(true);
200/// }
201/// ```
202#[proc_macro_attribute]
203pub fn test_basic(args: TokenStream, item: TokenStream) -> TokenStream {
204 entry::test(args, item, false)
205}
206
207/// Implementation detail of the `select!` macro. This macro is **not** intended
208/// to be used as part of the public API and is permitted to change.
209#[proc_macro]
210#[doc(hidden)]
211pub fn select_priv_declare_output_enum(input: TokenStream) -> TokenStream {
212 select::declare_output_enum(input)
213}