Skip to content

Commit

Permalink
refactor(lifetimes): change names of lifetimes to be more meaningful
Browse files Browse the repository at this point in the history
Attempt to rename 'a and 'k to something more meaningful. Not sure
that I got this part right.
  • Loading branch information
nikomatsakis committed Aug 14, 2015
1 parent eeb1992 commit 82b7f76
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 32 deletions.
4 changes: 2 additions & 2 deletions src/favicon_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct FaviconHandler {
}

impl Middleware for FaviconHandler {
fn invoke<'a, 'b>(&'a self, req: &mut Request<'a, 'a, 'b>, res: Response<'a, net::Fresh>)
fn invoke<'a, 'server>(&'a self, req: &mut Request<'a, 'server>, res: Response<'a, net::Fresh>)
-> MiddlewareResult<'a> {
if FaviconHandler::is_favicon_request(req) {
self.handle_request(req, res)
Expand Down Expand Up @@ -78,7 +78,7 @@ impl FaviconHandler {
}
}

pub fn send_favicon<'a, 'b>(&self, req: &Request, mut res: Response<'a>) -> MiddlewareResult<'a> {
pub fn send_favicon<'a, 'server>(&self, req: &Request, mut res: Response<'a>) -> MiddlewareResult<'a> {
debug!("{:?} {:?}", req.origin.method, self.icon_path.display());
res.set(MediaType::Ico);
res.send(&*self.icon)
Expand Down
4 changes: 2 additions & 2 deletions src/json_body_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::io::{Read, ErrorKind};
// Plugin boilerplate
struct JsonBodyParser;
impl Key for JsonBodyParser { type Value = String; }
impl<'a, 'b, 'k> Plugin<Request<'a, 'b, 'k>> for JsonBodyParser {
impl<'mw, 'conn> Plugin<Request<'mw, 'conn>> for JsonBodyParser {
type Error = io::Error;

fn eval(req: &mut Request) -> Result<String, io::Error> {
Expand All @@ -22,7 +22,7 @@ pub trait JsonBody {
fn json_as<T: Decodable>(&mut self) -> Result<T, io::Error>;
}

impl<'a, 'b, 'k> JsonBody for Request<'a, 'b, 'k> {
impl<'mw, 'conn> JsonBody for Request<'mw, 'conn> {
fn json_as<T: Decodable>(&mut self) -> Result<T, io::Error> {
self.get_ref::<JsonBodyParser>().and_then(|parsed|
json::decode::<T>(&*parsed).map_err(|err|
Expand Down
10 changes: 5 additions & 5 deletions src/macros/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ macro_rules! _middleware_inner {
use $crate::{MiddlewareResult,Responder, Response, Request};

#[inline(always)]
fn restrict<'a, R: Responder>(r: R, res: Response<'a>)
-> MiddlewareResult<'a> {
fn restrict<'mw, R: Responder>(r: R, res: Response<'mw>)
-> MiddlewareResult<'mw> {
res.send(r)
}

// Inference fails due to thinking it's a (&Request, Response) with
// different mutability requirements
#[inline(always)]
fn restrict_closure<F>(f: F) -> F
where F: for<'r, 'b, 'a>
Fn(&'r mut Request<'a, 'a, 'b>, Response<'a>)
-> MiddlewareResult<'a> + Send + Sync { f }
where F: for<'r, 'mw, 'conn>
Fn(&'r mut Request<'mw, 'conn>, Response<'mw>)
-> MiddlewareResult<'mw> + Send + Sync { f }

restrict_closure(move |as_pat!($req), $res_binding| {
restrict(as_block!({$($b)+}), $res)
Expand Down
14 changes: 7 additions & 7 deletions src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use hyper::net;

pub use self::Action::{Continue, Halt};

pub type MiddlewareResult<'a> = Result<Action<Response<'a, net::Fresh>,
Response<'a, net::Streaming>>,
NickelError<'a>>;
pub type MiddlewareResult<'mw> = Result<Action<Response<'mw, net::Fresh>,
Response<'mw, net::Streaming>>,
NickelError<'mw>>;

pub enum Action<T=(), U=()> {
Continue(T),
Expand All @@ -17,13 +17,13 @@ pub enum Action<T=(), U=()> {
// the usage of + Send is weird here because what we really want is + Static
// but that's not possible as of today. We have to use + Send for now.
pub trait Middleware: Send + 'static + Sync {
fn invoke<'a, 'b>(&'a self, _req: &mut Request<'a, 'a, 'b>, res: Response<'a, net::Fresh>) -> MiddlewareResult<'a> {
fn invoke<'mw, 'conn>(&'mw self, _req: &mut Request<'mw, 'conn>, res: Response<'mw, net::Fresh>) -> MiddlewareResult<'mw> {
Ok(Continue(res))
}
}

impl<T> Middleware for T where T: for<'r, 'b, 'a> Fn(&'r mut Request<'a, 'a, 'b>, Response<'a>) -> MiddlewareResult<'a> + Send + Sync + 'static {
fn invoke<'a, 'b>(&'a self, req: &mut Request<'a, 'a, 'b>, res: Response<'a>) -> MiddlewareResult<'a> {
impl<T> Middleware for T where T: for<'r, 'mw, 'conn> Fn(&'r mut Request<'mw, 'conn>, Response<'mw>) -> MiddlewareResult<'mw> + Send + Sync + 'static {
fn invoke<'mw, 'conn>(&'mw self, req: &mut Request<'mw, 'conn>, res: Response<'mw>) -> MiddlewareResult<'mw> {
(*self)(req, res)
}
}
Expand Down Expand Up @@ -52,7 +52,7 @@ impl MiddlewareStack {
self.error_handlers.push(Box::new(handler));
}

pub fn invoke<'a, 'b>(&'a self, mut req: Request<'a, 'a, 'b>, mut res: Response<'a>) {
pub fn invoke<'mw, 'conn>(&'mw self, mut req: Request<'mw, 'conn>, mut res: Response<'mw>) {
for handler in self.handlers.iter() {
match handler.invoke(&mut req, res) {
Ok(Halt(res)) => {
Expand Down
4 changes: 2 additions & 2 deletions src/query_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl Query {
struct QueryStringParser;
impl Key for QueryStringParser { type Value = Query; }

impl<'a, 'b, 'k> Plugin<Request<'a, 'b, 'k>> for QueryStringParser {
impl<'mw, 'conn> Plugin<Request<'mw, 'conn>> for QueryStringParser {
type Error = ();

fn eval(req: &mut Request) -> Result<Query, ()> {
Expand All @@ -46,7 +46,7 @@ pub trait QueryString {
fn query(&mut self) -> &Query;
}

impl<'a, 'b, 'k> QueryString for Request<'a, 'b, 'k> {
impl<'mw, 'conn> QueryString for Request<'mw, 'conn> {
fn query(&mut self) -> &Query {
self.get_ref::<QueryStringParser>()
.ok()
Expand Down
22 changes: 14 additions & 8 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ use typemap::TypeMap;
use hyper::server::Request as HyperRequest;
use hyper::uri::RequestUri::AbsolutePath;

///A container for all the request data
pub struct Request<'a, 'b, 'k: 'a> {
/// A container for all the request data.
///
/// The lifetime `'mw` represents the lifetime of various bits of
/// middleware state within nickel. It can vary and get shorter.
///
/// The lifetime `'server` represents the lifetime of data internal to
/// the server. It is fixed and longer than `'mw`.
pub struct Request<'mw, 'server: 'mw> {
///the original `hyper::server::Request`
pub origin: HyperRequest<'a, 'k>,
pub origin: HyperRequest<'mw, 'server>,
///a `HashMap<String, String>` holding all params with names and values
pub route_result: Option<RouteResult<'b>>,
pub route_result: Option<RouteResult<'mw>>,

map: TypeMap
}

impl<'a, 'b, 'k> Request<'a, 'b, 'k> {
pub fn from_internal(req: HyperRequest<'a, 'k>) -> Request<'a, 'b, 'k> {
impl<'mw, 'server> Request<'mw, 'server> {
pub fn from_internal(req: HyperRequest<'mw, 'server>) -> Request<'mw, 'server> {
Request {
origin: req,
route_result: None,
Expand All @@ -35,7 +41,7 @@ impl<'a, 'b, 'k> Request<'a, 'b, 'k> {
}
}

impl<'a, 'b, 'k> Extensible for Request<'a, 'b, 'k> {
impl<'mw, 'server> Extensible for Request<'mw, 'server> {
fn extensions(&self) -> &TypeMap {
&self.map
}
Expand All @@ -45,4 +51,4 @@ impl<'a, 'b, 'k> Extensible for Request<'a, 'b, 'k> {
}
}

impl<'a, 'b, 'k> Pluggable for Request<'a, 'b, 'k> {}
impl<'mw, 'server> Pluggable for Request<'mw, 'server> {}
12 changes: 6 additions & 6 deletions src/router/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ pub struct Route {
/// It contains the matched `route` and also a `params` property holding
/// a HashMap with the keys being the variable names and the value being the
/// evaluated string
pub struct RouteResult<'a> {
pub route: &'a Route,
pub struct RouteResult<'mw> {
pub route: &'mw Route,
params: Vec<(String, String)>
}

impl<'a> RouteResult<'a> {
impl<'mw> RouteResult<'mw> {
pub fn param(&self, key: &str) -> Option<&str> {
for &(ref k, ref v) in &self.params {
if k == &key {
Expand Down Expand Up @@ -56,7 +56,7 @@ impl Router {
}
}

pub fn match_route<'a>(&'a self, method: &Method, path: &str) -> Option<RouteResult<'a>> {
pub fn match_route<'mw>(&'mw self, method: &Method, path: &str) -> Option<RouteResult<'mw>> {
self.routes
.iter()
.find(|item| item.method == *method && item.matcher.is_match(path))
Expand Down Expand Up @@ -95,8 +95,8 @@ impl HttpRouter for Router {
}

impl Middleware for Router {
fn invoke<'a, 'b>(&'a self, req: &mut Request<'a, 'a, 'b>, mut res: Response<'a>)
-> MiddlewareResult<'a> {
fn invoke<'mw, 'conn>(&'mw self, req: &mut Request<'mw, 'conn>, mut res: Response<'mw>)
-> MiddlewareResult<'mw> {
debug!("Router::invoke for '{:?}'", req.origin.uri);

// Strip off the querystring when matching a route
Expand Down

0 comments on commit 82b7f76

Please sign in to comment.