2023-06-25 22:24:48 +02:00
|
|
|
use darling::export::NestedMeta;
|
2022-02-12 00:11:15 +01:00
|
|
|
use darling::FromMeta;
|
2023-06-25 22:32:24 +02:00
|
|
|
use proc_macro2::{Span, TokenStream};
|
2022-02-12 00:11:15 +01:00
|
|
|
use quote::{format_ident, quote};
|
2023-06-25 22:32:24 +02:00
|
|
|
use syn::{parse_quote, Expr, ExprLit, ItemFn, Lit, LitInt, ReturnType, Type};
|
2022-02-12 00:11:15 +01:00
|
|
|
|
|
|
|
use crate::util::ctxt::Ctxt;
|
|
|
|
|
|
|
|
#[derive(Debug, FromMeta)]
|
|
|
|
struct Args {
|
|
|
|
#[darling(default)]
|
2023-06-25 22:32:24 +02:00
|
|
|
pool_size: Option<syn::Expr>,
|
2022-02-12 00:11:15 +01:00
|
|
|
}
|
|
|
|
|
2023-06-25 22:24:48 +02:00
|
|
|
pub fn run(args: &[NestedMeta], f: syn::ItemFn) -> Result<TokenStream, TokenStream> {
|
|
|
|
let args = Args::from_list(args).map_err(|e| e.write_errors())?;
|
2022-02-12 00:11:15 +01:00
|
|
|
|
2023-06-25 22:32:24 +02:00
|
|
|
let pool_size = args.pool_size.unwrap_or(Expr::Lit(ExprLit {
|
|
|
|
attrs: vec![],
|
|
|
|
lit: Lit::Int(LitInt::new("1", Span::call_site())),
|
|
|
|
}));
|
2022-02-12 00:11:15 +01:00
|
|
|
|
|
|
|
let ctxt = Ctxt::new();
|
|
|
|
|
|
|
|
if f.sig.asyncness.is_none() {
|
|
|
|
ctxt.error_spanned_by(&f.sig, "task functions must be async");
|
|
|
|
}
|
|
|
|
if !f.sig.generics.params.is_empty() {
|
|
|
|
ctxt.error_spanned_by(&f.sig, "task functions must not be generic");
|
|
|
|
}
|
2023-03-08 01:59:06 +01:00
|
|
|
if !f.sig.generics.where_clause.is_none() {
|
|
|
|
ctxt.error_spanned_by(&f.sig, "task functions must not have `where` clauses");
|
|
|
|
}
|
|
|
|
if !f.sig.abi.is_none() {
|
|
|
|
ctxt.error_spanned_by(&f.sig, "task functions must not have an ABI qualifier");
|
|
|
|
}
|
|
|
|
if !f.sig.variadic.is_none() {
|
|
|
|
ctxt.error_spanned_by(&f.sig, "task functions must not be variadic");
|
|
|
|
}
|
|
|
|
match &f.sig.output {
|
|
|
|
ReturnType::Default => {}
|
|
|
|
ReturnType::Type(_, ty) => match &**ty {
|
|
|
|
Type::Tuple(tuple) if tuple.elems.is_empty() => {}
|
|
|
|
Type::Never(_) => {}
|
|
|
|
_ => ctxt.error_spanned_by(
|
|
|
|
&f.sig,
|
|
|
|
"task functions must either not return a value, return `()` or return `!`",
|
|
|
|
),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-04-25 22:18:52 +02:00
|
|
|
let mut arg_names = Vec::new();
|
2022-02-12 00:11:15 +01:00
|
|
|
let mut fargs = f.sig.inputs.clone();
|
|
|
|
|
2022-04-26 18:36:38 +02:00
|
|
|
for arg in fargs.iter_mut() {
|
2022-02-12 00:11:15 +01:00
|
|
|
match arg {
|
|
|
|
syn::FnArg::Receiver(_) => {
|
|
|
|
ctxt.error_spanned_by(arg, "task functions must not have receiver arguments");
|
|
|
|
}
|
|
|
|
syn::FnArg::Typed(t) => match t.pat.as_mut() {
|
2022-04-25 22:18:52 +02:00
|
|
|
syn::Pat::Ident(id) => {
|
|
|
|
arg_names.push(id.ident.clone());
|
|
|
|
id.mutability = None;
|
2022-02-12 00:11:15 +01:00
|
|
|
}
|
|
|
|
_ => {
|
2022-06-12 22:15:44 +02:00
|
|
|
ctxt.error_spanned_by(arg, "pattern matching in task arguments is not yet supported");
|
2022-02-12 00:11:15 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctxt.check()?;
|
|
|
|
|
2022-04-24 03:06:29 +02:00
|
|
|
let task_ident = f.sig.ident.clone();
|
|
|
|
let task_inner_ident = format_ident!("__{}_task", task_ident);
|
2022-02-12 00:11:15 +01:00
|
|
|
|
2022-04-25 22:18:52 +02:00
|
|
|
let mut task_inner = f;
|
|
|
|
let visibility = task_inner.vis.clone();
|
|
|
|
task_inner.vis = syn::Visibility::Inherited;
|
|
|
|
task_inner.sig.ident = task_inner_ident.clone();
|
2022-04-24 03:06:29 +02:00
|
|
|
|
2023-11-24 21:37:27 +01:00
|
|
|
#[cfg(feature = "nightly")]
|
2022-12-25 04:21:07 +01:00
|
|
|
let mut task_outer: ItemFn = parse_quote! {
|
|
|
|
#visibility fn #task_ident(#fargs) -> ::embassy_executor::SpawnToken<impl Sized> {
|
|
|
|
type Fut = impl ::core::future::Future + 'static;
|
2023-06-25 22:32:24 +02:00
|
|
|
const POOL_SIZE: usize = #pool_size;
|
|
|
|
static POOL: ::embassy_executor::raw::TaskPool<Fut, POOL_SIZE> = ::embassy_executor::raw::TaskPool::new();
|
2022-12-25 04:21:07 +01:00
|
|
|
unsafe { POOL._spawn_async_fn(move || #task_inner_ident(#(#arg_names,)*)) }
|
|
|
|
}
|
|
|
|
};
|
2023-11-24 21:37:27 +01:00
|
|
|
#[cfg(not(feature = "nightly"))]
|
|
|
|
let mut task_outer: ItemFn = parse_quote! {
|
|
|
|
#visibility fn #task_ident(#fargs) -> ::embassy_executor::SpawnToken<impl Sized> {
|
|
|
|
const POOL_SIZE: usize = #pool_size;
|
|
|
|
static POOL: ::embassy_executor::_export::TaskPoolRef = ::embassy_executor::_export::TaskPoolRef::new();
|
|
|
|
unsafe { POOL.get::<_, POOL_SIZE>()._spawn_async_fn(move || #task_inner_ident(#(#arg_names,)*)) }
|
|
|
|
}
|
|
|
|
};
|
2022-12-25 04:21:07 +01:00
|
|
|
|
|
|
|
task_outer.attrs.append(&mut task_inner.attrs.clone());
|
|
|
|
|
2022-02-12 00:11:15 +01:00
|
|
|
let result = quote! {
|
2022-04-26 18:36:38 +02:00
|
|
|
// This is the user's task function, renamed.
|
|
|
|
// We put it outside the #task_ident fn below, because otherwise
|
|
|
|
// the items defined there (such as POOL) would be in scope
|
|
|
|
// in the user's code.
|
2022-12-25 04:21:07 +01:00
|
|
|
#[doc(hidden)]
|
2022-04-25 22:18:52 +02:00
|
|
|
#task_inner
|
2022-04-24 03:06:29 +02:00
|
|
|
|
2022-12-25 04:21:07 +01:00
|
|
|
#task_outer
|
2022-02-12 00:11:15 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|