2022-02-12 00:11:15 +01:00
|
|
|
use darling::FromMeta;
|
|
|
|
use proc_macro2::TokenStream;
|
|
|
|
use quote::{format_ident, quote};
|
|
|
|
|
|
|
|
use crate::util::ctxt::Ctxt;
|
|
|
|
use crate::util::path::ModulePrefix;
|
|
|
|
|
|
|
|
#[derive(Debug, FromMeta)]
|
|
|
|
struct Args {
|
|
|
|
#[darling(default)]
|
|
|
|
pool_size: Option<usize>,
|
|
|
|
#[darling(default)]
|
|
|
|
embassy_prefix: ModulePrefix,
|
|
|
|
}
|
|
|
|
|
2022-04-25 22:18:52 +02:00
|
|
|
pub fn run(args: syn::AttributeArgs, f: syn::ItemFn) -> Result<TokenStream, TokenStream> {
|
2022-02-12 00:11:15 +01:00
|
|
|
let args = Args::from_list(&args).map_err(|e| e.write_errors())?;
|
|
|
|
|
|
|
|
let embassy_prefix = args.embassy_prefix.append("embassy");
|
|
|
|
let embassy_path = embassy_prefix.path();
|
|
|
|
|
|
|
|
let pool_size: usize = args.pool_size.unwrap_or(1);
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
if pool_size < 1 {
|
|
|
|
ctxt.error_spanned_by(&f.sig, "pool_size must be 1 or greater");
|
|
|
|
}
|
|
|
|
|
2022-04-25 22:18:52 +02:00
|
|
|
let mut arg_types = Vec::new();
|
|
|
|
let mut arg_names = Vec::new();
|
|
|
|
let mut arg_indexes = Vec::new();
|
2022-02-12 00:11:15 +01:00
|
|
|
let mut fargs = f.sig.inputs.clone();
|
|
|
|
|
2022-04-25 22:18:52 +02:00
|
|
|
for (i, arg) in fargs.iter_mut().enumerate() {
|
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());
|
|
|
|
arg_types.push(t.ty.clone());
|
|
|
|
arg_indexes.push(syn::Index::from(i));
|
|
|
|
id.mutability = None;
|
2022-02-12 00:11:15 +01:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
ctxt.error_spanned_by(
|
|
|
|
arg,
|
2022-04-25 22:18:52 +02:00
|
|
|
"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-04-25 22:18:52 +02:00
|
|
|
let mod_ident = format_ident!("__{}_mod", task_ident);
|
|
|
|
let args_ident = format_ident!("__{}_args", 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
|
|
|
|
2022-02-12 00:11:15 +01:00
|
|
|
let result = quote! {
|
2022-04-25 22:18:52 +02:00
|
|
|
#task_inner
|
2022-04-24 03:06:29 +02:00
|
|
|
|
|
|
|
#[allow(non_camel_case_types)]
|
2022-04-25 22:18:52 +02:00
|
|
|
type #args_ident = (#(#arg_types,)*);
|
|
|
|
|
|
|
|
mod #mod_ident {
|
|
|
|
use #embassy_path::executor::SpawnToken;
|
|
|
|
use #embassy_path::executor::raw::TaskStorage;
|
2022-04-24 03:06:29 +02:00
|
|
|
|
2022-04-25 22:18:52 +02:00
|
|
|
type Fut = impl ::core::future::Future + 'static;
|
2022-04-24 03:06:29 +02:00
|
|
|
|
2022-02-12 00:11:15 +01:00
|
|
|
#[allow(clippy::declare_interior_mutable_const)]
|
2022-04-25 22:18:52 +02:00
|
|
|
const NEW_TS: TaskStorage<Fut> = TaskStorage::new();
|
2022-04-24 03:06:29 +02:00
|
|
|
|
2022-04-25 22:18:52 +02:00
|
|
|
static POOL: [TaskStorage<Fut>; #pool_size] = [NEW_TS; #pool_size];
|
|
|
|
|
|
|
|
pub(super) fn task(args: super::#args_ident) -> SpawnToken<Fut> {
|
|
|
|
unsafe { TaskStorage::spawn_pool(&POOL, move || super::#task_inner_ident(#(args.#arg_indexes),*)) }
|
|
|
|
}
|
|
|
|
}
|
2022-04-24 03:06:29 +02:00
|
|
|
|
2022-04-25 22:18:52 +02:00
|
|
|
#visibility fn #task_ident(#fargs) -> #embassy_path::executor::SpawnToken<impl ::core::future::Future + 'static> {
|
|
|
|
#mod_ident::task((#(#arg_names,)*))
|
2022-02-12 00:11:15 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|