1
0
mirror of https://github.com/fafhrd91/actix-web synced 2025-08-30 16:40:21 +02:00

better handler function ergonimics

This commit is contained in:
Nikolay Kim
2017-10-29 21:39:59 -07:00
parent 8aa20c6261
commit 4e216701c0
6 changed files with 25 additions and 15 deletions

View File

@@ -15,6 +15,9 @@ use httprequest::HttpRequest;
use httpresponse::HttpResponse;
use httpcodes::{HTTPNotFound, HTTPMethodNotAllowed};
/// Result of a resource handler function
pub type HandlerResult<T> = Result<T, HttpResponse>;
/// Http resource
///
/// `Resource` is an entry in route table which corresponds to requested URL.
@@ -48,7 +51,6 @@ impl<S> Default for Resource<S> {
}
}
impl<S> Resource<S> where S: 'static {
pub(crate) fn default_not_found() -> Self {
@@ -66,7 +68,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(&mut HttpRequest, Payload, &S) -> R + 'static,
where F: Fn(&mut HttpRequest, Payload, &S) -> HandlerResult<R> + 'static,
R: Into<HttpResponse> + 'static,
{
self.routes.insert(method, Box::new(FnHandler::new(handler)));