1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-25 06:36:02 +02:00

move state to request object

This commit is contained in:
Nikolay Kim
2017-11-26 21:18:38 -08:00
parent 8e0a7f44d4
commit 5a3b6638a7
12 changed files with 81 additions and 71 deletions

View File

@@ -1,4 +1,3 @@
use std::rc::Rc;
use std::marker::PhantomData;
use std::collections::HashMap;
@@ -64,7 +63,7 @@ impl<S> Resource<S> where S: 'static {
/// Register handler for specified method.
pub fn handler<F, R>(&mut self, method: Method, handler: F)
where F: Fn(HttpRequest, &S) -> Result<R> + 'static,
where F: Fn(HttpRequest<S>) -> Result<R> + 'static,
R: Into<HttpResponse> + 'static,
{
self.routes.insert(method, Box::new(FnHandler::new(handler)));
@@ -72,7 +71,7 @@ impl<S> Resource<S> where S: 'static {
/// Register async handler for specified method.
pub fn async<F, R>(&mut self, method: Method, handler: F)
where F: Fn(HttpRequest, &S) -> R + 'static,
where F: Fn(HttpRequest<S>) -> R + 'static,
R: Stream<Item=Frame, Error=Error> + 'static,
{
self.routes.insert(method, Box::new(StreamHandler::new(handler)));
@@ -125,11 +124,11 @@ impl<S> Resource<S> where S: 'static {
impl<S: 'static> RouteHandler<S> for Resource<S> {
fn handle(&self, req: HttpRequest, state: Rc<S>) -> Task {
fn handle(&self, req: HttpRequest<S>) -> Task {
if let Some(handler) = self.routes.get(req.method()) {
handler.handle(req, state)
handler.handle(req)
} else {
self.default.handle(req, state)
self.default.handle(req)
}
}
}