diff --git a/guide/src/qs_4.md b/guide/src/qs_4.md index addb1541..53b58c77 100644 --- a/guide/src/qs_4.md +++ b/guide/src/qs_4.md @@ -4,14 +4,14 @@ A request handler can by any object that implements [`Handler` trait](../actix_web/dev/trait.Handler.html#implementors). Request handling happen in two stages. First handler object get called. Handle can return any object that implements -[`FromRequest` trait](../actix_web/trait.FromRequest.html#foreign-impls). -Then `from_request()` get called on returned object. And finally -result of the `from_request()` call get converted to `Reply` object. +[*Responder trait*](../actix_web/trait.Responder.html#foreign-impls). +Then `respond_to()` get called on returned object. And finally +result of the `respond_to()` call get converted to `Reply` object. -By default actix provides several `FromRequest` implementations for some standard types, +By default actix provides `Responder` implementations for some standard types, like `&'static str`, `String`, etc. For complete list of implementations check -[FromRequest documentation](../actix_web/trait.FromRequest.html#foreign-impls). +[Responder documentation](../actix_web/trait.Responder.html#foreign-impls). Examples of valid handlers: @@ -59,11 +59,11 @@ struct MyObj { } /// we have to convert Error into HttpResponse as well -impl FromRequest for MyObj { +impl Responder for MyObj { type Item = HttpResponse; type Error = Error; - fn from_request(self, req: HttpRequest) -> Result { + fn respond_to(self, req: HttpRequest) -> Result { let body = serde_json::to_string(&self)?; // Create response and set content type diff --git a/guide/src/qs_4_5.md b/guide/src/qs_4_5.md index 6c2863e4..cbc69654 100644 --- a/guide/src/qs_4_5.md +++ b/guide/src/qs_4_5.md @@ -5,11 +5,11 @@ and [`ResponseError` trait](../actix_web/error/trait.ResponseError.html) for handling handler's errors. Any error that implements `ResponseError` trait can be returned as error value. *Handler* can return *Result* object, actix by default provides -`FromRequest` implemenation for compatible result object. Here is implementation +`Responder` implemenation for compatible result object. Here is implementation definition: ```rust,ignore -impl> FromRequest for Result +impl> Responder for Result ``` And any error that implements `ResponseError` can be converted into `Error` object. diff --git a/src/fs.rs b/src/fs.rs index 736d9910..78f3b4e7 100644 --- a/src/fs.rs +++ b/src/fs.rs @@ -10,7 +10,7 @@ use std::ops::{Deref, DerefMut}; use mime_guess::get_mime_type; use param::FromParam; -use handler::{Handler, FromRequest}; +use handler::{Handler, Responder}; use httprequest::HttpRequest; use httpresponse::HttpResponse; use httpcodes::HTTPOk; @@ -77,11 +77,11 @@ impl DerefMut for NamedFile { } } -impl FromRequest for NamedFile { +impl Responder for NamedFile { type Item = HttpResponse; type Error = io::Error; - fn from_request(mut self, _: HttpRequest) -> Result { + fn respond_to(mut self, _: HttpRequest) -> Result { let mut resp = HTTPOk.build(); if let Some(ext) = self.path().extension() { let mime = get_mime_type(&ext.to_string_lossy()); @@ -124,11 +124,11 @@ impl Directory { } } -impl FromRequest for Directory { +impl Responder for Directory { type Item = HttpResponse; type Error = io::Error; - fn from_request(self, req: HttpRequest) -> Result { + fn respond_to(self, req: HttpRequest) -> Result { let index_of = format!("Index of {}", req.path()); let mut body = String::new(); let base = Path::new(req.path()); @@ -176,14 +176,14 @@ pub enum FilesystemElement { Directory(Directory), } -impl FromRequest for FilesystemElement { +impl Responder for FilesystemElement { type Item = HttpResponse; type Error = io::Error; - fn from_request(self, req: HttpRequest) -> Result { + fn respond_to(self, req: HttpRequest) -> Result { match self { - FilesystemElement::File(file) => file.from_request(req), - FilesystemElement::Directory(dir) => dir.from_request(req), + FilesystemElement::File(file) => file.respond_to(req), + FilesystemElement::Directory(dir) => dir.respond_to(req), } } } @@ -294,7 +294,7 @@ mod tests { let _f: &File = &file; } { let _f: &mut File = &mut file; } - let resp = file.from_request(HttpRequest::default()).unwrap(); + let resp = file.respond_to(HttpRequest::default()).unwrap(); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/x-toml") } @@ -312,7 +312,7 @@ mod tests { req.match_info_mut().add("tail", ""); st.show_index = true; - let resp = st.handle(req).from_request(HttpRequest::default()).unwrap(); + let resp = st.handle(req).respond_to(HttpRequest::default()).unwrap(); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "text/html; charset=utf-8"); assert!(resp.body().is_binary()); assert!(format!("{:?}", resp.body()).contains("README.md")); diff --git a/src/handler.rs b/src/handler.rs index f059e81c..9d60f1f6 100644 --- a/src/handler.rs +++ b/src/handler.rs @@ -18,26 +18,30 @@ use httpresponse::HttpResponse; pub trait Handler: 'static { /// The type of value that handler will return. - type Result: FromRequest; + type Result: Responder; /// Handle request fn handle(&self, req: HttpRequest) -> Self::Result; } -pub trait FromRequest { +/// Trait implemented by types that generate responses for clients. +/// +/// Types that implement this trait can be used as the return type of a handler. +pub trait Responder { /// The associated item which can be returned. type Item: Into; /// The associated error which can be returned. type Error: Into; - fn from_request(self, req: HttpRequest) -> Result; + /// Convert itself to `Reply` or `Error`. + fn respond_to(self, req: HttpRequest) -> Result; } /// Handler for Fn() impl Handler for F where F: Fn(HttpRequest) -> R + 'static, - R: FromRequest + 'static + R: Responder + 'static { type Result = R; @@ -93,20 +97,20 @@ impl Reply { } } -impl FromRequest for Reply { +impl Responder for Reply { type Item = Reply; type Error = Error; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { Ok(self) } } -impl FromRequest for HttpResponse { +impl Responder for HttpResponse { type Item = Reply; type Error = Error; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { Ok(Reply(ReplyItem::Message(Box::new(self)))) } } @@ -118,14 +122,14 @@ impl From for Reply { } } -impl> FromRequest for Result +impl> Responder for Result { - type Item = ::Item; + type Item = ::Item; type Error = Error; - fn from_request(self, req: HttpRequest) -> Result { + fn respond_to(self, req: HttpRequest) -> Result { match self { - Ok(val) => match val.from_request(req) { + Ok(val) => match val.respond_to(req) { Ok(val) => Ok(val), Err(err) => Err(err.into()), }, @@ -143,12 +147,12 @@ impl> From> for Reply { } } -impl>, S: 'static> FromRequest for HttpContext +impl>, S: 'static> Responder for HttpContext { type Item = Reply; type Error = Error; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { Ok(Reply(ReplyItem::Actor(Box::new(self)))) } } @@ -160,12 +164,12 @@ impl>, S: 'static> From> fo } } -impl FromRequest for Box> +impl Responder for Box> { type Item = Reply; type Error = Error; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { Ok(Reply(ReplyItem::Future(self))) } } @@ -179,7 +183,7 @@ pub(crate) trait RouteHandler: 'static { pub(crate) struct WrapHandler where H: Handler, - R: FromRequest, + R: Responder, S: 'static, { h: H, @@ -188,7 +192,7 @@ struct WrapHandler impl WrapHandler where H: Handler, - R: FromRequest, + R: Responder, S: 'static, { pub fn new(h: H) -> Self { @@ -198,12 +202,12 @@ impl WrapHandler impl RouteHandler for WrapHandler where H: Handler, - R: FromRequest + 'static, + R: Responder + 'static, S: 'static, { fn handle(&self, req: HttpRequest) -> Reply { let req2 = req.clone_without_state(); - match self.h.handle(req).from_request(req2) { + match self.h.handle(req).respond_to(req2) { Ok(reply) => reply.into(), Err(err) => Reply::response(err.into()), } @@ -265,11 +269,11 @@ impl RouteHandler for AsyncHandler /// ``` pub struct Json (pub T); -impl FromRequest for Json { +impl Responder for Json { type Item = HttpResponse; type Error = Error; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { let body = serde_json::to_string(&self.0)?; Ok(HttpResponse::Ok() @@ -406,7 +410,7 @@ mod tests { #[test] fn test_json() { let json = Json(MyObj{name: "test"}); - let resp = json.from_request(HttpRequest::default()).unwrap(); + let resp = json.respond_to(HttpRequest::default()).unwrap(); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), "application/json"); } diff --git a/src/httpcodes.rs b/src/httpcodes.rs index f00a2a5f..274167c9 100644 --- a/src/httpcodes.rs +++ b/src/httpcodes.rs @@ -3,7 +3,7 @@ use http::{StatusCode, Error as HttpError}; use body::Body; -use handler::{Reply, Handler, RouteHandler, FromRequest}; +use handler::{Reply, Handler, RouteHandler, Responder}; use httprequest::HttpRequest; use httpresponse::{HttpResponse, HttpResponseBuilder}; @@ -81,11 +81,11 @@ impl RouteHandler for StaticResponse { } } -impl FromRequest for StaticResponse { +impl Responder for StaticResponse { type Item = HttpResponse; type Error = HttpError; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { self.build().body(Body::Empty) } } diff --git a/src/httpresponse.rs b/src/httpresponse.rs index a60411d2..a1b26caf 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -12,7 +12,7 @@ use cookie::Cookie; use body::Body; use error::Error; -use handler::FromRequest; +use handler::Responder; use encoding::ContentEncoding; use httprequest::HttpRequest; @@ -431,11 +431,11 @@ impl From for HttpResponse { } } -impl FromRequest for HttpResponseBuilder { +impl Responder for HttpResponseBuilder { type Item = HttpResponse; type Error = HttpError; - fn from_request(mut self, _: HttpRequest) -> Result { + fn respond_to(mut self, _: HttpRequest) -> Result { self.finish() } } @@ -449,11 +449,11 @@ impl From<&'static str> for HttpResponse { } } -impl FromRequest for &'static str { +impl Responder for &'static str { type Item = HttpResponse; type Error = HttpError; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { HttpResponse::build(StatusCode::OK) .content_type("text/plain; charset=utf-8") .body(self) @@ -469,11 +469,11 @@ impl From<&'static [u8]> for HttpResponse { } } -impl FromRequest for &'static [u8] { +impl Responder for &'static [u8] { type Item = HttpResponse; type Error = HttpError; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { HttpResponse::build(StatusCode::OK) .content_type("application/octet-stream") .body(self) @@ -489,11 +489,11 @@ impl From for HttpResponse { } } -impl FromRequest for String { +impl Responder for String { type Item = HttpResponse; type Error = HttpError; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { HttpResponse::build(StatusCode::OK) .content_type("text/plain; charset=utf-8") .body(self) @@ -509,11 +509,11 @@ impl<'a> From<&'a String> for HttpResponse { } } -impl<'a> FromRequest for &'a String { +impl<'a> Responder for &'a String { type Item = HttpResponse; type Error = HttpError; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { HttpResponse::build(StatusCode::OK) .content_type("text/plain; charset=utf-8") .body(self) @@ -529,11 +529,11 @@ impl From for HttpResponse { } } -impl FromRequest for Bytes { +impl Responder for Bytes { type Item = HttpResponse; type Error = HttpError; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { HttpResponse::build(StatusCode::OK) .content_type("application/octet-stream") .body(self) @@ -549,11 +549,11 @@ impl From for HttpResponse { } } -impl FromRequest for BytesMut { +impl Responder for BytesMut { type Item = HttpResponse; type Error = HttpError; - fn from_request(self, _: HttpRequest) -> Result { + fn respond_to(self, _: HttpRequest) -> Result { HttpResponse::build(StatusCode::OK) .content_type("application/octet-stream") .body(self) @@ -689,7 +689,7 @@ mod tests { assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.body().binary().unwrap(), &Binary::from("test")); - let resp: HttpResponse = "test".from_request(req.clone()).ok().unwrap(); + let resp: HttpResponse = "test".respond_to(req.clone()).ok().unwrap(); assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), header::HeaderValue::from_static("text/plain; charset=utf-8")); @@ -703,7 +703,7 @@ mod tests { assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.body().binary().unwrap(), &Binary::from(b"test".as_ref())); - let resp: HttpResponse = b"test".as_ref().from_request(req.clone()).ok().unwrap(); + let resp: HttpResponse = b"test".as_ref().respond_to(req.clone()).ok().unwrap(); assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), header::HeaderValue::from_static("application/octet-stream")); @@ -717,7 +717,7 @@ mod tests { assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.body().binary().unwrap(), &Binary::from("test".to_owned())); - let resp: HttpResponse = "test".to_owned().from_request(req.clone()).ok().unwrap(); + let resp: HttpResponse = "test".to_owned().respond_to(req.clone()).ok().unwrap(); assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), header::HeaderValue::from_static("text/plain; charset=utf-8")); @@ -731,7 +731,7 @@ mod tests { assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.body().binary().unwrap(), &Binary::from((&"test".to_owned()))); - let resp: HttpResponse = (&"test".to_owned()).from_request(req.clone()).ok().unwrap(); + let resp: HttpResponse = (&"test".to_owned()).respond_to(req.clone()).ok().unwrap(); assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), header::HeaderValue::from_static("text/plain; charset=utf-8")); @@ -747,7 +747,7 @@ mod tests { assert_eq!(resp.body().binary().unwrap(), &Binary::from(Bytes::from_static(b"test"))); let b = Bytes::from_static(b"test"); - let resp: HttpResponse = b.from_request(req.clone()).ok().unwrap(); + let resp: HttpResponse = b.respond_to(req.clone()).ok().unwrap(); assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), header::HeaderValue::from_static("application/octet-stream")); @@ -763,7 +763,7 @@ mod tests { assert_eq!(resp.body().binary().unwrap(), &Binary::from(BytesMut::from("test"))); let b = BytesMut::from("test"); - let resp: HttpResponse = b.from_request(req.clone()).ok().unwrap(); + let resp: HttpResponse = b.respond_to(req.clone()).ok().unwrap(); assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.headers().get(header::CONTENT_TYPE).unwrap(), header::HeaderValue::from_static("application/octet-stream")); diff --git a/src/lib.rs b/src/lib.rs index 062e6220..d9563e1f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -122,7 +122,7 @@ pub use application::Application; pub use httprequest::HttpRequest; pub use httpresponse::HttpResponse; pub use payload::{Payload, PayloadItem}; -pub use handler::{Reply, FromRequest, Json, NormalizePath}; +pub use handler::{Reply, Responder, Json, NormalizePath}; pub use route::Route; pub use resource::Resource; pub use server::HttpServer; diff --git a/src/resource.rs b/src/resource.rs index 523a3096..af615147 100644 --- a/src/resource.rs +++ b/src/resource.rs @@ -5,7 +5,7 @@ use http::{Method, StatusCode}; use pred; use body::Body; use route::Route; -use handler::{Reply, Handler, FromRequest}; +use handler::{Reply, Handler, Responder}; use httprequest::HttpRequest; use httpresponse::HttpResponse; @@ -120,7 +120,7 @@ impl Resource { /// ``` pub fn f(&mut self, handler: F) where F: Fn(HttpRequest) -> R + 'static, - R: FromRequest + 'static, + R: Responder + 'static, { self.routes.push(Route::default()); self.routes.last_mut().unwrap().f(handler) diff --git a/src/route.rs b/src/route.rs index daea3cb3..fa2c7813 100644 --- a/src/route.rs +++ b/src/route.rs @@ -2,7 +2,7 @@ use futures::Future; use error::Error; use pred::Predicate; -use handler::{Reply, Handler, FromRequest, RouteHandler, AsyncHandler, WrapHandler}; +use handler::{Reply, Handler, Responder, RouteHandler, AsyncHandler, WrapHandler}; use httpcodes::HTTPNotFound; use httprequest::HttpRequest; use httpresponse::HttpResponse; @@ -58,7 +58,7 @@ impl Route { /// during route configuration, because it does not return reference to self. pub fn f(&mut self, handler: F) where F: Fn(HttpRequest) -> R + 'static, - R: FromRequest + 'static, + R: Responder + 'static, { self.handler = Box::new(WrapHandler::new(handler)); }