|
| 1 | +extern crate proc_macro; |
| 2 | + |
| 3 | +use quote::quote; |
| 4 | +use syn::{ |
| 5 | + braced, |
| 6 | + parenthesized, |
| 7 | + parse::{Parse, ParseStream}, |
| 8 | + parse_macro_input, |
| 9 | + parse_quote, |
| 10 | + parse_quote_spanned, |
| 11 | + spanned::Spanned, |
| 12 | + Block, |
| 13 | + Error, |
| 14 | + Generics, |
| 15 | + Ident, |
| 16 | + Lifetime, |
| 17 | + Token, |
| 18 | + Type, |
| 19 | +}; |
| 20 | + |
| 21 | +/// Generates: |
| 22 | +/// * an `IntoFuture` executing the given method body |
| 23 | +/// * an opaque wrapper type for the future in case we want to do something more fancy than |
| 24 | +/// BoxFuture. |
| 25 | +/// * a `run` method for sync execution, optionally with a wrapper function |
| 26 | +#[proc_macro] |
| 27 | +pub fn action_impl(input: proc_macro::TokenStream) -> proc_macro::TokenStream { |
| 28 | + let ActionImpl { |
| 29 | + generics, |
| 30 | + lifetime, |
| 31 | + action, |
| 32 | + future_name, |
| 33 | + exec_self_mut, |
| 34 | + exec_output, |
| 35 | + exec_body, |
| 36 | + sync_wrap, |
| 37 | + } = parse_macro_input!(input as ActionImpl); |
| 38 | + |
| 39 | + let mut unbounded_generics = generics.clone(); |
| 40 | + for lt in unbounded_generics.lifetimes_mut() { |
| 41 | + lt.bounds.clear(); |
| 42 | + } |
| 43 | + for ty in unbounded_generics.type_params_mut() { |
| 44 | + ty.bounds.clear(); |
| 45 | + } |
| 46 | + |
| 47 | + let SyncWrap { |
| 48 | + sync_arg_mut, |
| 49 | + sync_arg, |
| 50 | + sync_output, |
| 51 | + sync_body, |
| 52 | + } = sync_wrap.unwrap_or_else(|| { |
| 53 | + parse_quote! { fn sync_wrap(out) -> #exec_output { out } } |
| 54 | + }); |
| 55 | + |
| 56 | + quote! { |
| 57 | + impl #generics crate::action::private::Sealed for #action { } |
| 58 | + |
| 59 | + impl #generics crate::action::Action for #action { } |
| 60 | + |
| 61 | + impl #generics std::future::IntoFuture for #action { |
| 62 | + type Output = #exec_output; |
| 63 | + type IntoFuture = #future_name #unbounded_generics; |
| 64 | + |
| 65 | + fn into_future(#exec_self_mut self) -> Self::IntoFuture { |
| 66 | + #future_name (Box::pin(async move { |
| 67 | + #exec_body |
| 68 | + })) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + pub struct #future_name #generics (crate::BoxFuture<#lifetime, #exec_output>); |
| 73 | + |
| 74 | + impl #generics std::future::Future for #future_name #unbounded_generics { |
| 75 | + type Output = #exec_output; |
| 76 | + |
| 77 | + fn poll(mut self: std::pin::Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> std::task::Poll<Self::Output> { |
| 78 | + self.0.as_mut().poll(cx) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + #[cfg(feature = "sync")] |
| 83 | + impl #generics #action { |
| 84 | + /// Synchronously execute this action. |
| 85 | + pub fn run(self) -> #sync_output { |
| 86 | + let #sync_arg_mut #sync_arg = crate::sync::TOKIO_RUNTIME.block_on(std::future::IntoFuture::into_future(self)); |
| 87 | + #sync_body |
| 88 | + } |
| 89 | + } |
| 90 | + }.into() |
| 91 | +} |
| 92 | + |
| 93 | +// impl<generics> Action for ActionType { |
| 94 | +// type Future = FutureName; |
| 95 | +// async fn execute([mut] self) -> OutType { <exec body> } |
| 96 | +// [SyncWrap] |
| 97 | +// } |
| 98 | +struct ActionImpl { |
| 99 | + generics: Generics, |
| 100 | + lifetime: Lifetime, |
| 101 | + action: Type, |
| 102 | + future_name: Ident, |
| 103 | + exec_self_mut: Option<Token![mut]>, |
| 104 | + exec_output: Type, |
| 105 | + exec_body: Block, |
| 106 | + sync_wrap: Option<SyncWrap>, |
| 107 | +} |
| 108 | + |
| 109 | +impl Parse for ActionImpl { |
| 110 | + fn parse(input: ParseStream) -> syn::Result<Self> { |
| 111 | + // impl<generics> Action for ActionType |
| 112 | + input.parse::<Token![impl]>()?; |
| 113 | + let generics: Generics = input.parse()?; |
| 114 | + let mut lifetime = None; |
| 115 | + for lt in generics.lifetimes() { |
| 116 | + if lifetime.is_some() { |
| 117 | + return Err(input.error("only one lifetime argument permitted")); |
| 118 | + } |
| 119 | + lifetime = Some(lt); |
| 120 | + } |
| 121 | + let lifetime = match lifetime { |
| 122 | + Some(lt) => lt.lifetime.clone(), |
| 123 | + None => parse_quote_spanned! { generics.span() => 'static }, |
| 124 | + }; |
| 125 | + parse_name(input, "Action")?; |
| 126 | + input.parse::<Token![for]>()?; |
| 127 | + let action = input.parse()?; |
| 128 | + |
| 129 | + let impl_body; |
| 130 | + braced!(impl_body in input); |
| 131 | + |
| 132 | + // type Future = FutureName; |
| 133 | + impl_body.parse::<Token![type]>()?; |
| 134 | + parse_name(&impl_body, "Future")?; |
| 135 | + impl_body.parse::<Token![=]>()?; |
| 136 | + let future_name = impl_body.parse()?; |
| 137 | + impl_body.parse::<Token![;]>()?; |
| 138 | + |
| 139 | + // async fn execute([mut] self) -> OutType { <exec body> } |
| 140 | + impl_body.parse::<Token![async]>()?; |
| 141 | + impl_body.parse::<Token![fn]>()?; |
| 142 | + parse_name(&impl_body, "execute")?; |
| 143 | + let exec_args; |
| 144 | + parenthesized!(exec_args in impl_body); |
| 145 | + let exec_self_mut = exec_args.parse()?; |
| 146 | + exec_args.parse::<Token![self]>()?; |
| 147 | + if !exec_args.is_empty() { |
| 148 | + return Err(exec_args.error("unexpected token")); |
| 149 | + } |
| 150 | + impl_body.parse::<Token![->]>()?; |
| 151 | + let exec_output = impl_body.parse()?; |
| 152 | + let exec_body = impl_body.parse()?; |
| 153 | + |
| 154 | + // Optional SyncWrap. |
| 155 | + let sync_wrap = if impl_body.peek(Token![fn]) { |
| 156 | + Some(impl_body.parse()?) |
| 157 | + } else { |
| 158 | + None |
| 159 | + }; |
| 160 | + |
| 161 | + if !impl_body.is_empty() { |
| 162 | + return Err(exec_args.error("unexpected token")); |
| 163 | + } |
| 164 | + |
| 165 | + Ok(ActionImpl { |
| 166 | + generics, |
| 167 | + lifetime, |
| 168 | + action, |
| 169 | + future_name, |
| 170 | + exec_self_mut, |
| 171 | + exec_output, |
| 172 | + exec_body, |
| 173 | + sync_wrap, |
| 174 | + }) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +// fn sync_wrap([mut] out) -> OutType { <out body> } |
| 179 | +struct SyncWrap { |
| 180 | + sync_arg_mut: Option<Token![mut]>, |
| 181 | + sync_arg: Ident, |
| 182 | + sync_output: Type, |
| 183 | + sync_body: Block, |
| 184 | +} |
| 185 | + |
| 186 | +impl Parse for SyncWrap { |
| 187 | + fn parse(input: ParseStream) -> syn::Result<Self> { |
| 188 | + input.parse::<Token![fn]>()?; |
| 189 | + parse_name(input, "sync_wrap")?; |
| 190 | + let args_input; |
| 191 | + parenthesized!(args_input in input); |
| 192 | + let sync_arg_mut = args_input.parse()?; |
| 193 | + let sync_arg = args_input.parse()?; |
| 194 | + if !args_input.is_empty() { |
| 195 | + return Err(args_input.error("unexpected token")); |
| 196 | + } |
| 197 | + input.parse::<Token![->]>()?; |
| 198 | + let sync_output = input.parse()?; |
| 199 | + let sync_body = input.parse()?; |
| 200 | + |
| 201 | + Ok(SyncWrap { |
| 202 | + sync_arg_mut, |
| 203 | + sync_arg, |
| 204 | + sync_output, |
| 205 | + sync_body, |
| 206 | + }) |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +/// Parse an identifier with a specific expected value. |
| 211 | +fn parse_name(input: ParseStream, name: &str) -> syn::Result<()> { |
| 212 | + let ident = input.parse::<Ident>()?; |
| 213 | + if ident.to_string() != name { |
| 214 | + return Err(Error::new( |
| 215 | + ident.span(), |
| 216 | + format!("expected '{}', got '{}'", name, ident), |
| 217 | + )); |
| 218 | + } |
| 219 | + Ok(()) |
| 220 | +} |
0 commit comments