async-rt-macros 0.1.0

Attribute macros for async-rt
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
//! Attribute macros for `async-rt`.

use proc_macro::TokenStream;
use proc_macro_crate::{FoundCrate, crate_name};
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::{format_ident, quote};
use syn::parse::Parser;
use syn::punctuated::Punctuated;
use syn::{Error, Expr, ExprLit, ItemFn, Lit, Meta, Token, parse2, spanned::Spanned};

#[derive(Clone, Copy)]
enum AttributeKind {
    Main,
    Test,
}

impl AttributeKind {
    fn is_test(self) -> bool {
        matches!(self, Self::Test)
    }
}

#[derive(Clone, Copy, Default)]
enum Executor {
    #[default]
    Global,
    Tokio,
    Smol,
    Compio,
    ThreadPool,
    Lite,
}

impl Executor {
    fn parse(value: &str, span: Span) -> syn::Result<Self> {
        match value {
            "global" => Ok(Self::Global),
            "tokio" => Ok(Self::Tokio),
            "smol" => Ok(Self::Smol),
            "compio" => Ok(Self::Compio),
            "threadpool" | "thread_pool" => Ok(Self::ThreadPool),
            "lite" => Ok(Self::Lite),
            _ => Err(Error::new(
                span,
                "unknown executor. expected `global`, `tokio`, `smol`, `compio`, `threadpool`, or `lite`",
            )),
        }
    }

    fn drive(
        self,
        kind: AttributeKind,
        runtime_crate: &TokenStream2,
        body: &syn::Block,
    ) -> TokenStream2 {
        let builtin_executor = match self {
            Self::Tokio => quote!(#runtime_crate::global::BuiltinExecutor::Tokio),
            Self::Smol => quote!(#runtime_crate::global::BuiltinExecutor::Smol),
            Self::Compio => quote!(#runtime_crate::global::BuiltinExecutor::Compio),
            Self::ThreadPool => quote!(#runtime_crate::global::BuiltinExecutor::ThreadPool),
            Self::Lite => quote!(#runtime_crate::global::BuiltinExecutor::Lite),
            Self::Global => unreachable!("the global executor must be resolved before expansion"),
        };
        let create_executor = match self {
            Self::Tokio if kind.is_test() => quote! {
                #runtime_crate::rt::tokio::TokioRuntimeExecutor::with_single_thread()
                    .expect("async-rt failed to create the Tokio test runtime")
            },
            Self::Tokio => quote! {
                #runtime_crate::rt::tokio::TokioRuntimeExecutor::with_multi_thread()
                    .expect("async-rt failed to create the Tokio runtime")
            },
            Self::Smol => quote! {
                #runtime_crate::rt::smol::SmolRuntimeExecutor::new()
            },
            Self::Compio => quote! {
                #runtime_crate::rt::compio::CompioRuntimeExecutor::new()
                    .expect("async-rt failed to create the Compio runtime")
            },
            Self::ThreadPool => quote! {
                #runtime_crate::rt::threadpool::ThreadPoolExecutor
            },
            Self::Lite => quote! {
                #runtime_crate::rt::lite::LiteExecutor
            },
            Self::Global => unreachable!("the global executor must be resolved before expansion"),
        };

        quote! {
            let __async_rt_body = async move #body;
            #[allow(
                clippy::diverging_sub_expression,
                clippy::expect_used,
                clippy::needless_return,
                clippy::unwrap_in_result
            )]
            {
                let __async_rt_executor = #runtime_crate::global::ConfiguredExecutor::with_task_executor(
                    #create_executor,
                    #builtin_executor,
                );
                return #runtime_crate::ExecutorBlockOn::block_on(
                    &__async_rt_executor,
                    __async_rt_body,
                );
            }
        }
    }
}

#[derive(Default)]
struct Arguments {
    executor: Executor,
    executor_set: bool,
    driver: Option<Expr>,
}

impl Arguments {
    fn parse(tokens: TokenStream2) -> syn::Result<Self> {
        let parser = Punctuated::<Meta, Token![,]>::parse_terminated;
        let arguments = parser.parse2(tokens)?;
        let mut result = Self::default();

        for argument in arguments {
            let Meta::NameValue(argument) = argument else {
                return Err(Error::new(
                    argument.span(),
                    "expected `executor = \"...\"` or `driver = ...`",
                ));
            };

            if argument.path.is_ident("driver") {
                if result.driver.is_some() {
                    return Err(Error::new(
                        argument.path.span(),
                        "duplicate `driver` option",
                    ));
                }
                if result.executor_set {
                    return Err(Error::new(
                        argument.path.span(),
                        "`driver` and `executor` cannot be used together",
                    ));
                }

                result.driver = Some(argument.value);
                continue;
            }

            if !argument.path.is_ident("executor") {
                return Err(Error::new(
                    argument.path.span(),
                    "unknown option. expected `executor` or `driver`",
                ));
            }
            if result.executor_set {
                return Err(Error::new(
                    argument.path.span(),
                    "duplicate `executor` option",
                ));
            }
            if result.driver.is_some() {
                return Err(Error::new(
                    argument.path.span(),
                    "`driver` and `executor` cannot be used together",
                ));
            }

            let (value, span) = match argument.value {
                Expr::Lit(ExprLit {
                    lit: Lit::Str(value),
                    ..
                }) => (value.value(), value.span()),
                Expr::Path(value) if value.path.get_ident().is_some() => {
                    let ident = value.path.get_ident().expect("checked above");
                    (ident.to_string(), ident.span())
                }
                value => {
                    return Err(Error::new(
                        value.span(),
                        "executor must be a string or identifier",
                    ));
                }
            };

            result.executor = Executor::parse(&value, span)?;
            result.executor_set = true;
        }

        Ok(result)
    }
}

macro_rules! entry_points {
    ($main:ident, $test:ident, $executor:ident) => {
        /// Runs an async function as a synchronous entry point.
        ///
        /// The `async-rt` crate selects the default executor when it re-exports
        /// this macro. A built-in executor can be selected explicitly with, for
        /// example, `#[async_rt::main(executor = "compio")]`.
        ///
        /// An explicit selection controls the runtime driving this function and the
        /// executor used by `async_rt::task`.
        ///
        /// A custom driver can be supplied with `driver = expression`. It drives the
        /// annotated future without changing the executor used by `async_rt::task`.
        #[proc_macro_attribute]
        pub fn $main(arguments: TokenStream, item: TokenStream) -> TokenStream {
            expand(
                arguments,
                item,
                AttributeKind::Main,
                Some(Executor::$executor),
            )
        }

        /// Runs an async function as a synchronous test.
        ///
        /// The `async-rt` crate selects the default executor when it re-exports
        /// this macro. A built-in executor can be selected explicitly with, for
        /// example, `#[async_rt::test(executor = "tokio")]`.
        ///
        /// An explicit selection controls the runtime driving this function and the
        /// executor used by `async_rt::task`.
        /// Tests using different executors in the same binary run one at a time.
        ///
        /// A custom driver can be supplied with `driver = expression`. It drives the
        /// annotated future without changing the executor used by `async_rt::task`.
        #[proc_macro_attribute]
        pub fn $test(arguments: TokenStream, item: TokenStream) -> TokenStream {
            expand(
                arguments,
                item,
                AttributeKind::Test,
                Some(Executor::$executor),
            )
        }
    };
}

entry_points!(main_tokio, test_tokio, Tokio);
entry_points!(main_smol, test_smol, Smol);
entry_points!(main_compio, test_compio, Compio);
entry_points!(main_threadpool, test_threadpool, ThreadPool);
entry_points!(main_lite, test_lite, Lite);

/// Runs an async function when no default runtime is enabled.
#[proc_macro_attribute]
pub fn main_fail(arguments: TokenStream, item: TokenStream) -> TokenStream {
    expand(arguments, item, AttributeKind::Main, None)
}

/// Runs an async test when no default runtime is enabled.
#[proc_macro_attribute]
pub fn test_fail(arguments: TokenStream, item: TokenStream) -> TokenStream {
    expand(arguments, item, AttributeKind::Test, None)
}

macro_rules! failure_entry_points {
    ($main:ident, $test:ident, $message:literal) => {
        /// Emits an error because no supported runtime is available.
        #[proc_macro_attribute]
        pub fn $main(_arguments: TokenStream, _item: TokenStream) -> TokenStream {
            Error::new(Span::call_site(), $message)
                .into_compile_error()
                .into()
        }

        /// Emits an error because no supported test runtime is available.
        #[proc_macro_attribute]
        pub fn $test(_arguments: TokenStream, _item: TokenStream) -> TokenStream {
            Error::new(Span::call_site(), $message)
                .into_compile_error()
                .into()
        }
    };
}

failure_entry_points!(
    main_wasm_fail,
    test_wasm_fail,
    "async-rt's `main` and `test` macros do not yet support wasm32"
);

fn expand(
    arguments: TokenStream,
    item: TokenStream,
    kind: AttributeKind,
    default_executor: Option<Executor>,
) -> TokenStream {
    expand_inner(arguments.into(), item.into(), kind, default_executor)
        .unwrap_or_else(Error::into_compile_error)
        .into()
}

fn expand_inner(
    arguments: TokenStream2,
    item: TokenStream2,
    kind: AttributeKind,
    default_executor: Option<Executor>,
) -> syn::Result<TokenStream2> {
    let arguments = Arguments::parse(arguments)?;
    let mut function: ItemFn = parse2(item)?;

    if function.sig.asyncness.take().is_none() {
        return Err(Error::new(
            function.sig.fn_token.span,
            "the `async` keyword is required",
        ));
    }

    let runtime_crate = runtime_crate()?;
    let test_attribute = match kind {
        AttributeKind::Main => TokenStream2::new(),
        AttributeKind::Test => quote!(#[::core::prelude::v1::test]),
    };
    let attributes = function.attrs;
    let visibility = function.vis;
    let signature = function.sig;
    let body = function.block;
    let drive = match arguments.driver {
        Some(driver) => drive_custom(&runtime_crate, &body, driver),
        None => {
            let executor = match arguments.executor {
                Executor::Global => default_executor.ok_or_else(|| {
                    Error::new(
                        Span::call_site(),
                        "async-rt's `main` and `test` macros require a default runtime feature, an explicit executor, or a custom `driver`",
                    )
                })?,
                executor => executor,
            };
            executor.drive(kind, &runtime_crate, &body)
        }
    };

    Ok(quote! {
        #(#attributes)*
        #test_attribute
        #visibility #signature {
            #drive
        }
    })
}

fn drive_custom(runtime_crate: &TokenStream2, body: &syn::Block, driver: Expr) -> TokenStream2 {
    quote! {
        let __async_rt_body = async move #body;
        let __async_rt_executor = #runtime_crate::global::ConfiguredExecutor::new(#driver);
        return #runtime_crate::ExecutorBlockOn::block_on(
            &__async_rt_executor,
            __async_rt_body,
        );
    }
}

fn runtime_crate() -> syn::Result<TokenStream2> {
    match crate_name("async-rt") {
        Ok(FoundCrate::Itself) => Ok(quote!(::async_rt)),
        Ok(FoundCrate::Name(name)) => {
            let name = format_ident!("{}", name.replace('-', "_"));
            Ok(quote!(::#name))
        }
        Err(error) => Err(Error::new(
            Span::call_site(),
            format!("could not find the `async-rt` crate: {error}"),
        )),
    }
}

#[cfg(test)]
mod tests {
    use super::{Arguments, Executor};
    use quote::quote;
    use syn::Expr;

    #[test]
    fn parses_string_and_identifier_executors() {
        assert!(matches!(
            Arguments::parse(quote!(executor = "compio"))
                .unwrap()
                .executor,
            Executor::Compio
        ));
        assert!(matches!(
            Arguments::parse(quote!(executor = tokio)).unwrap().executor,
            Executor::Tokio
        ));
        assert!(matches!(
            Arguments::parse(quote!(executor = "lite"))
                .unwrap()
                .executor,
            Executor::Lite
        ));
    }

    #[test]
    fn rejects_unknown_options_and_executors() {
        assert!(Arguments::parse(quote!(flavor = "current_thread")).is_err());
        assert!(Arguments::parse(quote!(executor = "unknown")).is_err());
    }

    #[test]
    fn parses_custom_driver_expression() {
        let arguments = Arguments::parse(quote!(driver = custom::executor())).unwrap();
        assert!(matches!(arguments.driver, Some(Expr::Call(_))));
    }

    #[test]
    fn rejects_duplicate_or_conflicting_driver_options() {
        assert!(Arguments::parse(quote!(driver = first(), driver = second())).is_err());
        assert!(Arguments::parse(quote!(executor = "tokio", driver = custom())).is_err());
        assert!(Arguments::parse(quote!(driver = custom(), executor = "tokio")).is_err());
    }
}