Strip lifetimes for generated param types.

This commit is contained in:
Sergio Benitez 2016-09-06 23:24:20 -07:00
parent 327b28a98e
commit a6967cb48f
4 changed files with 32 additions and 6 deletions

View File

@ -9,6 +9,11 @@ fn hello(name: &str, age: i8) -> String {
format!("Hello, {} year old named {}!", age, name)
}
fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![hello]);
#[get("/hello/<name>")]
fn hi<'r>(name: &'r str) -> &'r str {
name
}
fn main() {
Rocket::new("localhost", 8000).mount_and_launch("/", routes![hello, hi]);
}

View File

@ -28,7 +28,7 @@ pub struct Request<'a> {
}
impl<'a> Request<'a> {
pub fn get_param<T: FromParam<'a>>(&'a self, n: usize) -> Result<T, Error> {
pub fn get_param<T: FromParam<'a>>(&self, n: usize) -> Result<T, Error> {
let params = self.params.borrow();
if params.is_none() || n >= params.as_ref().unwrap().len() {
Err(Error::NoKey)

View File

@ -2,7 +2,8 @@ use std::collections::HashSet;
use std::fmt::Display;
use ::{ROUTE_STRUCT_PREFIX, ROUTE_FN_PREFIX, PARAM_PREFIX};
use utils::{emit_item, span, sep_by_tok, SpanExt, IdentExt, ArgExt, option_as_expr};
use utils::{emit_item, span, sep_by_tok, option_as_expr, strip_ty_lifetimes};
use utils::{SpanExt, IdentExt, ArgExt};
use parser::RouteParams;
use syntax::codemap::{Span, Spanned};
@ -77,7 +78,8 @@ impl RouteGenerateExt for RouteParams {
}
let arg = arg.unwrap();
let (name, ty) = (arg.ident().unwrap().prepend(PARAM_PREFIX), &arg.ty);
let name = arg.ident().unwrap().prepend(PARAM_PREFIX);
let ty = strip_ty_lifetimes(arg.ty.clone());
Some(quote_stmt!(ecx,
let $name: $ty =
match ::rocket::form::FromForm::from_form_string($form_string) {
@ -135,7 +137,8 @@ impl RouteGenerateExt for RouteParams {
// Generate code for each user declared parameter.
for (i, arg) in all.iter().filter(declared).enumerate() {
let (ident, ty) = (arg.ident().unwrap().prepend(PARAM_PREFIX), &arg.ty);
let ident = arg.ident().unwrap().prepend(PARAM_PREFIX);
let ty = strip_ty_lifetimes(arg.ty.clone());
fn_param_statements.push(quote_stmt!(ecx,
let $ident: $ty = match _req.get_param($i) {
Ok(v) => v,

View File

@ -18,6 +18,8 @@ use syntax::codemap::{spanned, Span, Spanned, DUMMY_SP};
use syntax::ext::quote::rt::ToTokens;
use syntax::print::pprust::item_to_string;
use syntax::ptr::P;
use syntax::fold::Folder;
use syntax::ast::{Lifetime, LifetimeDef, Ty};
#[inline]
pub fn span<T>(t: T, span: Span) -> Spanned<T> {
@ -72,3 +74,19 @@ macro_rules! quote_enum {
})
}
pub struct TyLifetimeRemover;
// FIXME: Doesn't work for T + whatever.
impl Folder for TyLifetimeRemover {
fn fold_opt_lifetime(&mut self, _: Option<Lifetime>) -> Option<Lifetime> {
None
}
fn fold_lifetime_defs(&mut self, _: Vec<LifetimeDef>) -> Vec<LifetimeDef> {
vec![]
}
}
pub fn strip_ty_lifetimes(ty: P<Ty>) -> P<Ty> {
TyLifetimeRemover.fold_ty(ty)
}