File size: 6,227 Bytes
1e92f2d |
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 |
use proc_macro::TokenStream;
use quote::quote;
use syn::{DeriveInput, parse_macro_input, spanned::Spanned};
use turbo_tasks_macros_shared::{generate_exhaustive_destructuring, match_expansion};
pub fn derive_task_input(input: TokenStream) -> TokenStream {
let derive_input = parse_macro_input!(input as DeriveInput);
let ident = &derive_input.ident;
let generics = &derive_input.generics;
if let Some(where_clause) = &generics.where_clause {
// NOTE(alexkirsz) We could support where clauses and generic parameters bounds
// in the future, but for simplicity's sake, we don't support them yet.
where_clause
.span()
.unwrap()
.error("the TaskInput derive macro does not support where clauses yet")
.emit();
}
for param in &generics.params {
match param {
syn::GenericParam::Type(param) => {
if !param.bounds.is_empty() {
// NOTE(alexkirsz) See where clause above.
param
.span()
.unwrap()
.error(
"the TaskInput derive macro does not support generic parameters \
bounds yet",
)
.emit();
}
}
syn::GenericParam::Lifetime(param) => {
param
.span()
.unwrap()
.error("the TaskInput derive macro does not support generic lifetimes")
.emit();
}
syn::GenericParam::Const(param) => {
// NOTE(alexkirsz) Ditto: not supported yet for simplicity's sake.
param
.span()
.unwrap()
.error("the TaskInput derive macro does not support const generics yet")
.emit();
}
}
}
let is_resolved_impl = match_expansion(
&derive_input,
&|_ident, fields| {
let (capture, fields) = generate_exhaustive_destructuring(fields.named.iter());
(
capture,
quote! {
{#(
turbo_tasks::TaskInput::is_resolved(#fields) &&
)* true}
},
)
},
&|_ident, fields| {
let (capture, fields) = generate_exhaustive_destructuring(fields.unnamed.iter());
(
capture,
quote! {
{#(
turbo_tasks::TaskInput::is_resolved(#fields) &&
)* true}
},
)
},
&|_ident| quote! {true},
);
let is_transient_impl = match_expansion(
&derive_input,
&|_ident, fields| {
let (capture, fields) = generate_exhaustive_destructuring(fields.named.iter());
(
capture,
quote! {
{#(
turbo_tasks::TaskInput::is_transient(#fields) ||
)* false}
},
)
},
&|_ident, fields| {
let (capture, fields) = generate_exhaustive_destructuring(fields.unnamed.iter());
(
capture,
quote! {
{#(
turbo_tasks::TaskInput::is_transient(#fields) ||
)* false}
},
)
},
&|_ident| quote! {false},
);
let resolve_impl = match_expansion(
&derive_input,
&|ident, fields| {
let (capture, fields) = generate_exhaustive_destructuring(fields.named.iter());
(
capture,
quote! {
{
#(
let #fields = turbo_tasks::TaskInput::resolve_input(#fields).await?;
)*
Ok(#ident { #(#fields),* })
}
},
)
},
&|ident, fields| {
let (capture, fields) = generate_exhaustive_destructuring(fields.unnamed.iter());
(
capture,
quote! {
{
#(
let #fields = turbo_tasks::TaskInput::resolve_input(#fields).await?;
)*
Ok(#ident(#(#fields),*))
}
},
)
},
&|ident| quote! {Ok(#ident)},
);
let generic_params: Vec<_> = generics
.params
.iter()
.filter_map(|param| match param {
syn::GenericParam::Type(param) => Some(param),
_ => {
// We already report an error for this above.
None
}
})
.collect();
quote! {
#[turbo_tasks::macro_helpers::async_trait]
impl #generics turbo_tasks::TaskInput for #ident #generics
where
#(#generic_params: turbo_tasks::TaskInput,)*
{
#[allow(non_snake_case)]
#[allow(unreachable_code)] // This can occur for enums with no variants.
fn is_resolved(&self) -> bool {
#is_resolved_impl
}
#[allow(non_snake_case)]
#[allow(unreachable_code)] // This can occur for enums with no variants.
fn is_transient(&self) -> bool {
#is_transient_impl
}
#[allow(non_snake_case)]
#[allow(unreachable_code)] // This can occur for enums with no variants.
#[allow(clippy::manual_async_fn)] // some impls need the manual return type to work :(
fn resolve_input(
&self,
) -> impl
::std::future::Future<Output = turbo_tasks::Result<Self>> +
::std::marker::Send +
'_
{
async move {
#resolve_impl
}
}
}
}
.into()
}
|