diff --git a/example/src/main.rs b/example/src/main.rs index 507b27c..6c706a2 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -49,38 +49,33 @@ fn read(_state : &mut State, id : u64) -> Success } #[rest_create(Users)] -fn create(_state : &mut State, body : User) -> NoContent +fn create(_state : &mut State, body : User) { info!("Created User: {}", body.username); - ().into() } #[rest_update_all(Users)] -fn update_all(_state : &mut State, body : Vec) -> NoContent +fn update_all(_state : &mut State, body : Vec) { info!("Changing all Users to {:?}", body.into_iter().map(|u| u.username).collect::>()); - ().into() } #[rest_update(Users)] -fn update(_state : &mut State, id : u64, body : User) -> NoContent +fn update(_state : &mut State, id : u64, body : User) { info!("Change User {} to {}", id, body.username); - ().into() } #[rest_delete_all(Users)] -fn delete_all(_state : &mut State) -> NoContent +fn delete_all(_state : &mut State) { info!("Delete all Users"); - ().into() } #[rest_delete(Users)] -fn delete(_state : &mut State, id : u64) -> NoContent +fn delete(_state : &mut State, id : u64) { info!("Delete User {}", id); - ().into() } const ADDR : &str = "127.0.0.1:18080"; diff --git a/gotham_restful_derive/src/method.rs b/gotham_restful_derive/src/method.rs index 53fb66d..010246f 100644 --- a/gotham_restful_derive/src/method.rs +++ b/gotham_restful_derive/src/method.rs @@ -59,9 +59,9 @@ pub fn expand_method(method : Method, attrs : TokenStream, item : TokenStream) - let ident = parse_macro_input!(attrs as Ident); let fun = parse_macro_input!(item as ItemFn); - let ret = match fun.sig.output { - ReturnType::Default => quote!(()), - ReturnType::Type(_, ty) => quote!(#ty) + let (ret, is_no_content) = match fun.sig.output { + ReturnType::Default => (quote!(::gotham_restful::NoContent), true), + ReturnType::Type(_, ty) => (quote!(#ty), false) }; let args : Vec<(TokenStream2, TokenStream2)> = fun.sig.inputs.iter().map(|arg| match arg { FnArg::Typed(arg) => { @@ -79,7 +79,11 @@ pub fn expand_method(method : Method, attrs : TokenStream, item : TokenStream) - } generics.push(quote!(#ret)); let args : Vec = args.into_iter().map(|(pat, ty)| quote!(#pat : #ty)).collect(); - let block = fun.block; + let block = fun.block.stmts; + let ret_stmt = match is_no_content { + true => Some(quote!(().into())), + false => None + }; let trait_ident = method.trait_ident(); let fn_ident = method.fn_ident(); @@ -89,7 +93,10 @@ pub fn expand_method(method : Method, attrs : TokenStream, item : TokenStream) - where #ident : ::gotham_restful::Resource { fn #fn_ident(#(#args),*) -> #ret - #block + { + #(#block)* + #ret_stmt + } } }; output.into()