From a36993f6151a6871db083e49b3d3a235e5ee3e39 Mon Sep 17 00:00:00 2001 From: Dominic Date: Thu, 30 Apr 2020 00:37:24 +0200 Subject: [PATCH] there is no need to force people to take &State arg this highly improves async compatibility --- gotham_restful_derive/src/method.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/gotham_restful_derive/src/method.rs b/gotham_restful_derive/src/method.rs index 514bb4a..95e9221 100644 --- a/gotham_restful_derive/src/method.rs +++ b/gotham_restful_derive/src/method.rs @@ -175,12 +175,20 @@ impl Spanned for MethodArgument } } -fn interpret_arg_ty(index : usize, attrs : &[Attribute], name : &str, ty : Type) -> Result +fn interpret_arg_ty(attrs : &[Attribute], name : &str, ty : Type) -> Result { let attr = attrs.iter() .find(|arg| arg.path.segments.iter().any(|path| &path.ident.to_string() == "rest_arg")) .map(|arg| arg.tokens.to_string()); + if attr.as_deref() == Some("state") || (attr.is_none() && name == "state") + { + return match ty { + Type::Reference(ty) => Ok(if ty.mutability.is_none() { MethodArgumentType::StateRef } else { MethodArgumentType::StateMutRef }), + _ => Err(Error::new(ty.span(), "The state parameter has to be a (mutable) reference to gotham_restful::State")) + }; + } + if cfg!(feature = "auth") && (attr.as_deref() == Some("auth") || (attr.is_none() && name == "auth")) { return Ok(match ty { @@ -197,14 +205,6 @@ fn interpret_arg_ty(index : usize, attrs : &[Attribute], name : &str, ty : Type) })); } - if index == 0 - { - return match ty { - Type::Reference(ty) => Ok(if ty.mutability.is_none() { MethodArgumentType::StateRef } else { MethodArgumentType::StateMutRef }), - _ => Err(Error::new(ty.span(), "The first argument, unless some feature is used, has to be a (mutable) reference to gotham::state::State")) - }; - } - Ok(MethodArgumentType::MethodArg(ty)) } @@ -213,7 +213,7 @@ fn interpret_arg(index : usize, arg : &PatType) -> Result let pat = &arg.pat; let ident = format_ident!("arg{}", index); let orig_name = quote!(#pat); - let ty = interpret_arg_ty(index, &arg.attrs, &orig_name.to_string(), *arg.ty.clone())?; + let ty = interpret_arg_ty(&arg.attrs, &orig_name.to_string(), *arg.ty.clone())?; Ok(MethodArgument { ident, ident_span: arg.pat.span(), ty }) }