| 1 |
use quote::{format_ident, ToTokens}; |
| 2 |
use syn::{parse_quote, FnArg, Generics, Ident, ItemFn, Lifetime, Signature, Type, TypeReference}; |
| 3 |
|
| 4 |
use crate::{ |
| 5 |
parse::{arguments::ArgumentsInfo, future::MaybeFutureImplType}, |
| 6 |
refident::{MaybeIdent, MaybePat, MaybePatIdent, RemoveMutability}, |
| 7 |
}; |
| 8 |
|
| 9 |
pub(crate) trait ApplyArguments { |
| 10 |
type Output: Sized; |
| 11 |
type Context; |
| 12 |
|
| 13 |
fn apply_arguments( |
| 14 |
&mut self, |
| 15 |
arguments: &mut ArgumentsInfo, |
| 16 |
ctx: &mut Self::Context, |
| 17 |
) -> Self::Output; |
| 18 |
} |
| 19 |
|
| 20 |
impl ApplyArguments for FnArg { |
| 21 |
type Output = Option<Lifetime>; |
| 22 |
type Context = usize; |
| 23 |
|
| 24 |
fn apply_arguments( |
| 25 |
&mut self, |
| 26 |
arguments: &mut ArgumentsInfo, |
| 27 |
anoymous_id: &mut usize, |
| 28 |
) -> Self::Output { |
| 29 |
if self |
| 30 |
.maybe_pat() |
| 31 |
.map(|id| arguments.is_future(id)) |
| 32 |
.unwrap_or_default() |
| 33 |
{ |
| 34 |
self.impl_future_arg(anoymous_id) |
| 35 |
} else { |
| 36 |
None |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
fn move_generic_list(data: &mut Generics, other: Generics) { |
| 42 |
data.lt_token = data.lt_token.or(other.lt_token); |
| 43 |
data.params = other.params; |
| 44 |
data.gt_token = data.gt_token.or(other.gt_token); |
| 45 |
} |
| 46 |
|
| 47 |
fn extend_generics_with_lifetimes<'a, 'b>( |
| 48 |
generics: impl Iterator<Item = &'a syn::GenericParam>, |
| 49 |
lifetimes: impl Iterator<Item = &'b syn::Lifetime>, |
| 50 |
) -> Generics { |
| 51 |
let all = lifetimes |
| 52 |
.map(|lt| lt as &dyn ToTokens) |
| 53 |
.chain(generics.map(|gp| gp as &dyn ToTokens)); |
| 54 |
parse_quote! { |
| 55 |
<#(#all),*> |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
impl ApplyArguments for Signature { |
| 60 |
type Output = (); |
| 61 |
type Context = (); |
| 62 |
|
| 63 |
fn apply_arguments(&mut self, arguments: &mut ArgumentsInfo, _: &mut ()) { |
| 64 |
let mut anonymous_lt = 0_usize; |
| 65 |
let new_lifetimes = self |
| 66 |
.inputs |
| 67 |
.iter_mut() |
| 68 |
.filter_map(|arg| arg.apply_arguments(arguments, &mut anonymous_lt)) |
| 69 |
.collect::<Vec<_>>(); |
| 70 |
if !new_lifetimes.is_empty() || !self.generics.params.is_empty() { |
| 71 |
let new_generics = |
| 72 |
extend_generics_with_lifetimes(self.generics.params.iter(), new_lifetimes.iter()); |
| 73 |
move_generic_list(&mut self.generics, new_generics); |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
impl ApplyArguments for ItemFn { |
| 79 |
type Output = (); |
| 80 |
type Context = (); |
| 81 |
|
| 82 |
fn apply_arguments(&mut self, arguments: &mut ArgumentsInfo, _: &mut ()) { |
| 83 |
let args = self.sig.inputs.iter().cloned().collect::<Vec<_>>(); |
| 84 |
self.sig.apply_arguments(arguments, &mut ()); |
| 85 |
let rebound_awaited_args = args |
| 86 |
.iter() |
| 87 |
.filter_map(MaybePat::maybe_pat) |
| 88 |
.filter(|p| arguments.is_future_await(p)) |
| 89 |
.filter_map(MaybePatIdent::maybe_patident) |
| 90 |
.map(|p| { |
| 91 |
let a = &p.ident; |
| 92 |
quote::quote! { let #p = #a.await; } |
| 93 |
}); |
| 94 |
let orig_block_impl = self.block.clone(); |
| 95 |
self.block = parse_quote! { |
| 96 |
{ |
| 97 |
#(#rebound_awaited_args)* |
| 98 |
#orig_block_impl |
| 99 |
} |
| 100 |
}; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
pub(crate) trait ImplFutureArg { |
| 105 |
fn impl_future_arg(&mut self, anonymous_lt: &mut usize) -> Option<Lifetime>; |
| 106 |
} |
| 107 |
|
| 108 |
impl ImplFutureArg for FnArg { |
| 109 |
fn impl_future_arg(&mut self, anonymous_lt: &mut usize) -> Option<Lifetime> { |
| 110 |
let lifetime_id = self |
| 111 |
.maybe_ident() |
| 112 |
.map(|id| format_ident!("_{}", id)) |
| 113 |
.unwrap_or_else(|| { |
| 114 |
*anonymous_lt += 1; |
| 115 |
format_ident!("_anonymous_lt_{}", anonymous_lt) |
| 116 |
}); |
| 117 |
match self.as_mut_future_impl_type() { |
| 118 |
Some(ty) => { |
| 119 |
let lifetime = update_type_with_lifetime(ty, lifetime_id); |
| 120 |
*ty = parse_quote! { |
| 121 |
impl ::core::future::Future<Output = #ty> |
| 122 |
}; |
| 123 |
self.remove_mutability(); |
| 124 |
lifetime |
| 125 |
} |
| 126 |
None => None, |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
fn update_type_with_lifetime(ty: &mut Type, ident: Ident) -> Option<Lifetime> { |
| 132 |
if let Type::Reference(ty_ref @ TypeReference { lifetime: None, .. }) = ty { |
| 133 |
let lifetime = Some(syn::Lifetime { |
| 134 |
apostrophe: ident.span(), |
| 135 |
ident, |
| 136 |
}); |
| 137 |
ty_ref.lifetime.clone_from(&lifetime); |
| 138 |
lifetime |
| 139 |
} else { |
| 140 |
None |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
#[cfg(test)] |
| 145 |
mod should { |
| 146 |
use super::*; |
| 147 |
use crate::test::{assert_eq, *}; |
| 148 |
use syn::ItemFn; |
| 149 |
|
| 150 |
#[rstest] |
| 151 |
#[case("fn simple(a: u32) {}")] |
| 152 |
#[case("fn more(a: u32, b: &str) {}")] |
| 153 |
#[case("fn gen<S: AsRef<str>>(a: u32, b: S) {}")] |
| 154 |
#[case("fn attr(#[case] a: u32, #[values(1,2)] b: i32) {}")] |
| 155 |
fn no_change(#[case] item_fn: &str) { |
| 156 |
let mut item_fn: ItemFn = item_fn.ast(); |
| 157 |
let orig = item_fn.clone(); |
| 158 |
let mut args = ArgumentsInfo::default(); |
| 159 |
|
| 160 |
item_fn.sig.apply_arguments(&mut args, &mut ()); |
| 161 |
|
| 162 |
assert_eq!(orig, item_fn) |
| 163 |
} |
| 164 |
|
| 165 |
#[rstest] |
| 166 |
#[case::simple( |
| 167 |
"fn f(a: u32) {}", |
| 168 |
&["a"], |
| 169 |
"fn f(a: impl ::core::future::Future<Output = u32>) {}" |
| 170 |
)] |
| 171 |
#[case::more_than_one( |
| 172 |
"fn f(a: u32, b: String, c: std::collection::HashMap<usize, String>) {}", |
| 173 |
&["a", "b", "c"], |
| 174 |
r#"fn f(a: impl ::core::future::Future<Output = u32>, |
| 175 |
b: impl ::core::future::Future<Output = String>, |
| 176 |
c: impl ::core::future::Future<Output = std::collection::HashMap<usize, String>>) {}"#, |
| 177 |
)] |
| 178 |
#[case::just_one( |
| 179 |
"fn f(a: u32, b: String) {}", |
| 180 |
&["b"], |
| 181 |
r#"fn f(a: u32, |
| 182 |
b: impl ::core::future::Future<Output = String>) {}"# |
| 183 |
)] |
| 184 |
#[case::generics( |
| 185 |
"fn f<S: AsRef<str>>(a: S) {}", |
| 186 |
&["a"], |
| 187 |
"fn f<S: AsRef<str>>(a: impl ::core::future::Future<Output = S>) {}" |
| 188 |
)] |
| 189 |
#[case::remove_mut( |
| 190 |
"fn f(mut a: u32) {}", |
| 191 |
&["a"], |
| 192 |
r#"fn f(a: impl ::core::future::Future<Output = u32>) {}"# |
| 193 |
)] |
| 194 |
fn replace_future_basic_type( |
| 195 |
#[case] item_fn: &str, |
| 196 |
#[case] futures: &[&str], |
| 197 |
#[case] expected: &str, |
| 198 |
) { |
| 199 |
let mut item_fn: ItemFn = item_fn.ast(); |
| 200 |
let expected: ItemFn = expected.ast(); |
| 201 |
|
| 202 |
let mut arguments = ArgumentsInfo::default(); |
| 203 |
futures |
| 204 |
.into_iter() |
| 205 |
.for_each(|&f| arguments.add_future(pat(f))); |
| 206 |
|
| 207 |
item_fn.sig.apply_arguments(&mut arguments, &mut ()); |
| 208 |
|
| 209 |
assert_eq!(expected, item_fn) |
| 210 |
} |
| 211 |
|
| 212 |
#[rstest] |
| 213 |
#[case::base( |
| 214 |
"fn f(ident_name: &u32) {}", |
| 215 |
&["ident_name"], |
| 216 |
"fn f<'_ident_name>(ident_name: impl ::core::future::Future<Output = &'_ident_name u32>) {}" |
| 217 |
)] |
| 218 |
#[case::lifetime_already_exists( |
| 219 |
"fn f<'b>(a: &'b u32) {}", |
| 220 |
&["a"], |
| 221 |
"fn f<'b>(a: impl ::core::future::Future<Output = &'b u32>) {}" |
| 222 |
)] |
| 223 |
#[case::some_other_generics( |
| 224 |
"fn f<'b, IT: Iterator<Item=String + 'b>>(a: &u32, it: IT) {}", |
| 225 |
&["a"], |
| 226 |
"fn f<'_a, 'b, IT: Iterator<Item=String + 'b>>(a: impl ::core::future::Future<Output = &'_a u32>, it: IT) {}" |
| 227 |
)] |
| 228 |
fn replace_reference_type( |
| 229 |
#[case] item_fn: &str, |
| 230 |
#[case] futures: &[&str], |
| 231 |
#[case] expected: &str, |
| 232 |
) { |
| 233 |
let mut item_fn: ItemFn = item_fn.ast(); |
| 234 |
let expected: ItemFn = expected.ast(); |
| 235 |
|
| 236 |
let mut arguments = ArgumentsInfo::default(); |
| 237 |
futures |
| 238 |
.into_iter() |
| 239 |
.for_each(|&f| arguments.add_future(pat(f))); |
| 240 |
|
| 241 |
item_fn.sig.apply_arguments(&mut arguments, &mut ()); |
| 242 |
|
| 243 |
assert_eq!(expected, item_fn) |
| 244 |
} |
| 245 |
|
| 246 |
mod await_future_args { |
| 247 |
use rstest_test::{assert_in, assert_not_in}; |
| 248 |
|
| 249 |
use crate::parse::arguments::FutureArg; |
| 250 |
|
| 251 |
use super::*; |
| 252 |
|
| 253 |
#[test] |
| 254 |
fn with_global_await() { |
| 255 |
let mut item_fn: ItemFn = r#"fn test(a: i32, b:i32, c:i32) {} "#.ast(); |
| 256 |
let mut arguments: ArgumentsInfo = Default::default(); |
| 257 |
arguments.set_global_await(true); |
| 258 |
arguments.add_future(pat("a")); |
| 259 |
arguments.add_future(pat("b")); |
| 260 |
|
| 261 |
item_fn.apply_arguments(&mut arguments, &mut ()); |
| 262 |
|
| 263 |
let code = item_fn.block.display_code(); |
| 264 |
|
| 265 |
assert_in!(code, await_argument_code_string("a")); |
| 266 |
assert_in!(code, await_argument_code_string("b")); |
| 267 |
assert_not_in!(code, await_argument_code_string("c")); |
| 268 |
} |
| 269 |
|
| 270 |
#[test] |
| 271 |
fn with_selective_await() { |
| 272 |
let mut item_fn: ItemFn = r#"fn test(a: i32, b:i32, c:i32) {} "#.ast(); |
| 273 |
let mut arguments: ArgumentsInfo = Default::default(); |
| 274 |
arguments.set_future(pat("a"), FutureArg::Define); |
| 275 |
arguments.set_future(pat("b"), FutureArg::Await); |
| 276 |
|
| 277 |
item_fn.apply_arguments(&mut arguments, &mut ()); |
| 278 |
|
| 279 |
let code = item_fn.block.display_code(); |
| 280 |
|
| 281 |
assert_not_in!(code, await_argument_code_string("a")); |
| 282 |
assert_in!(code, await_argument_code_string("b")); |
| 283 |
assert_not_in!(code, await_argument_code_string("c")); |
| 284 |
} |
| 285 |
|
| 286 |
#[test] |
| 287 |
fn with_mut_await() { |
| 288 |
let mut item_fn: ItemFn = r#"fn test(mut a: i32) {} "#.ast(); |
| 289 |
let mut arguments: ArgumentsInfo = Default::default(); |
| 290 |
|
| 291 |
arguments.set_future(pat("a").with_mut(), FutureArg::Await); |
| 292 |
|
| 293 |
item_fn.apply_arguments(&mut arguments, &mut ()); |
| 294 |
|
| 295 |
let code = item_fn.block.display_code(); |
| 296 |
assert_in!(code, mut_await_argument_code_string("a")); |
| 297 |
} |
| 298 |
} |
| 299 |
} |