Skip to main content

ringolo_macros/
lib.rs

1#![allow(clippy::needless_doctest_main)]
2#![warn(
3    missing_debug_implementations,
4    missing_docs,
5    rust_2018_idioms,
6    unreachable_pub
7)]
8#![doc(test(
9    no_crate_inject,
10    attr(deny(warnings, rust_2018_idioms), allow(dead_code, unused_variables))
11))]
12
13//! Macros for user with Ringolo (copied from tokio-macros)
14
15mod entry;
16mod parse;
17
18/// Marks async function to be executed by the selected runtime. This macro
19/// helps set up a `Runtime` without requiring the user to use Runtime or Builder.
20///
21/// Note: This macro is designed to be simplistic and targets applications that
22/// do not require a complex setup. If the provided functionality is not
23/// sufficient, you may be interested in using `runtime::Builder` which provides
24/// a more powerful interface.
25///
26/// # Non-worker async function
27///
28/// Note that the async function marked with this macro does not run as a
29/// worker. The expectation is that other tasks are spawned by the function here.
30/// Awaiting on other futures from the function provided here will not
31/// perform as fast as those spawned as workers.
32///
33/// # Runtime flavors
34///
35/// The macro can be configured with a `flavor` parameter to select
36/// different runtime configurations.
37///
38/// ## Task Stealing Runtime (multi-thread)
39///
40/// To use the task stealing runtime, the macro can be configured using
41///
42/// ```no_run
43/// #[ringolo::main(flavor = "stealing", worker_threads = 10)]
44/// # async fn main() {}
45/// ```
46///
47/// The `worker_threads` option configures the number of worker threads, and
48/// defaults to the number of cpus on the system. This is the default flavor.
49///
50/// ## Local (current-thread)
51///
52/// To use the local runtime the macro can be configured using
53///
54/// ```rust
55/// #[ringolo::main(flavor = "local")]
56/// # async fn main() {}
57/// ```
58///
59/// # Function arguments
60///
61/// Function arguments are NOT allowed.
62///
63/// # Usage
64///
65/// ## Using the stealing runtime (default runtime)
66///
67/// ```no_run
68/// #[ringolo::main]
69/// async fn main() {
70///     println!("Hello world");
71/// }
72/// ```
73///
74/// Equivalent code not using `#[ringolo::main]`
75///
76/// ```no_run
77/// fn main() {
78///     ringolo::runtime::Builder::new_stealing()
79///         .try_build()
80///         .unwrap()
81///         .block_on(async {
82///             println!("Hello world");
83///         })
84/// }
85/// ```
86///
87/// ## Using the local runtime
88///
89/// ```rust
90/// #[ringolo::main(flavor = "local")]
91/// async fn main() {
92///     println!("Hello world");
93/// }
94/// ```
95///
96/// Equivalent code not using `#[ringolo::main]`
97///
98/// ```rust
99/// fn main() {
100///     ringolo::runtime::Builder::new_local()
101///         .try_build()
102///         .unwrap()
103///         .block_on(async {
104///             println!("Hello world");
105///         })
106/// }
107/// ```
108///
109/// ## Set number of worker threads
110///
111/// ```no_run
112/// #[ringolo::main(worker_threads = 2)]
113/// async fn main() {
114///     println!("Hello world");
115/// }
116/// ```
117///
118/// Equivalent code not using `#[ringolo::main]`
119///
120/// ```no_run
121/// fn main() {
122///     ringolo::runtime::Builder::new_stealing()
123///         .worker_threads(2)
124///         .try_build()
125///         .unwrap()
126///         .block_on(async {
127///             println!("Hello world");
128///         })
129/// }
130/// ```
131#[proc_macro_attribute]
132pub fn main(
133    args: proc_macro::TokenStream,
134    item: proc_macro::TokenStream,
135) -> proc_macro::TokenStream {
136    entry::main(args.into(), item.into()).into()
137}
138
139/// Marks async function to be executed by runtime, suitable to test environment.
140/// This macro helps set up a `Runtime` without requiring the user to use `Runtime`
141/// or `Builder` directly.
142///
143/// Note: This macro is designed to be simplistic and targets applications that
144/// do not require a complex setup. If the provided functionality is not
145/// sufficient, you may be interested in using `Builder` which provides a more
146/// powerful interface.
147///
148/// # Stealing runtime
149///
150/// To use the multi-threaded runtime, the macro can be configured using
151///
152/// ```no_run
153/// #[ringolo::test(flavor = "stealing", worker_threads = 1)]
154/// async fn my_test() {
155///     assert!(true);
156/// }
157/// ```
158///
159/// The `worker_threads` option configures the number of worker threads, and
160/// defaults to the number of cpus on the system.
161///
162/// # Local runtime
163///
164/// The default test runtime is single-threaded. Each test gets a
165/// separate local runtime.
166///
167/// ```no_run
168/// #[ringolo::test]
169/// async fn my_test() {
170///     assert!(true);
171/// }
172/// ```
173///
174/// ## Usage
175///
176/// ### Using the stealing runtime
177///
178/// ```no_run
179/// #[ringolo::test(flavor = "stealing")]
180/// async fn my_test() {
181///     assert!(true);
182/// }
183/// ```
184///
185/// Equivalent code not using `#[ringolo::test]`
186///
187/// ```no_run
188/// #[test]
189/// fn my_test() {
190///     ringolo::runtime::Builder::new_stealing()
191///         .try_build()
192///         .unwrap()
193///         .block_on(async {
194///             assert!(true);
195///         })
196/// }
197/// ```
198///
199/// ### Using local runtime
200///
201/// ```no_run
202/// #[ringolo::test]
203/// async fn my_test() {
204///     assert!(true);
205/// }
206/// ```
207///
208/// Equivalent code not using `#[ringolo::test]`
209///
210/// ```no_run
211/// #[test]
212/// fn my_test() {
213///     ringolo::runtime::Builder::new_local()
214///         .try_build()
215///         .unwrap()
216///         .block_on(async {
217///             assert!(true);
218///         })
219/// }
220/// ```
221///
222/// ### Set number of worker threads
223///
224/// ```no_run
225/// #[ringolo::test(flavor = "stealing", worker_threads = 2)]
226/// async fn my_test() {
227///     assert!(true);
228/// }
229/// ```
230///
231/// Equivalent code not using `#[ringolo::test]`
232///
233/// ```no_run
234/// #[test]
235/// fn my_test() {
236///     ringolo::runtime::Builder::new_stealing()
237///         .worker_threads(2)
238///         .try_build()
239///         .unwrap()
240///         .block_on(async {
241///             assert!(true);
242///         })
243/// }
244/// ```
245#[proc_macro_attribute]
246pub fn test(
247    args: proc_macro::TokenStream,
248    item: proc_macro::TokenStream,
249) -> proc_macro::TokenStream {
250    entry::test(args.into(), item.into()).into()
251}