Rocket

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 4010a967a6ff265a21c3b1978ffeecccc78f368f
parent 29c9cffdbe484cc5ade61906c58e519426331619
Author: Sergio Benitez <sb@sergio.bz>
Date:   Mon, 13 Aug 2018 00:43:29 -0700

Allow any 'T: Into<Vec<Route>>' to be mounted.

Diffstat:
Mcore/lib/src/handler.rs | 25++++++++++---------------
Mcore/lib/src/rocket.rs | 4++--
2 files changed, 12 insertions(+), 17 deletions(-)

diff --git a/core/lib/src/handler.rs b/core/lib/src/handler.rs @@ -38,24 +38,13 @@ pub type Outcome<'r> = outcome::Outcome<Response<'r>, Status, Data>; /// Such a handler might be written and used as follows: /// /// ```rust -/// # #[derive(Copy, Clone)] -/// # enum Kind { -/// # Simple, -/// # Intermediate, -/// # Complex, -/// # } +/// # #[derive(Copy, Clone)] enum Kind { Simple, Intermediate, Complex, } /// use rocket::{Request, Data, Route, http::Method}; /// use rocket::handler::{self, Handler, Outcome}; /// /// #[derive(Clone)] /// struct CustomHandler(Kind); /// -/// impl CustomHandler { -/// pub fn new(kind: Kind) -> Vec<Route> { -/// vec![Route::new(Method::Get, "/", CustomHandler(kind))] -/// } -/// } -/// /// impl Handler for CustomHandler { /// fn handle<'r>(&self, req: &'r Request, data: Data) -> Outcome<'r> { /// match self.0 { @@ -66,10 +55,16 @@ pub type Outcome<'r> = outcome::Outcome<Response<'r>, Status, Data>; /// } /// } /// +/// impl Into<Vec<Route>> for CustomHandler { +/// fn into(self) -> Vec<Route> { +/// vec![Route::new(Method::Get, "/", self)] +/// } +/// } +/// /// fn main() { /// # if false { /// rocket::ignite() -/// .mount("/", CustomHandler::new(Kind::Simple)) +/// .mount("/", CustomHandler(Kind::Simple)) /// .launch(); /// # } /// } @@ -81,8 +76,8 @@ pub type Outcome<'r> = outcome::Outcome<Response<'r>, Status, Data>; /// `CustomHandler` implements `Cloneable` automatically. The `Cloneable` /// trait serves no other purpose but to ensure that every `Handler` can be /// cloned, allowing `Route`s to be cloned. -/// 2. The `CustomHandler::new()` method returns a vector of routes so that -/// the user can trivially mount the handler. +/// 2. `CustomHandler` implements `Into<Vec<Route>>`, allowing an instance to +/// be used directly as the second parameter to `rocket.mount()`. /// 3. Unlike static-function-based handlers, this custom handler can make use /// of any internal state. /// diff --git a/core/lib/src/rocket.rs b/core/lib/src/rocket.rs @@ -492,7 +492,7 @@ impl Rocket { /// # } /// ``` #[inline] - pub fn mount(mut self, base: &str, routes: Vec<Route>) -> Self { + pub fn mount<R: Into<Vec<Route>>>(mut self, base: &str, routes: R) -> Self { info!("{}{} '{}':", Paint::masked("🛰 "), Paint::purple("Mounting"), @@ -520,7 +520,7 @@ impl Rocket { panic!("Invalid mount point."); } - for mut route in routes { + for mut route in routes.into() { let complete_uri = format!("{}/{}", base_uri, route.uri); let uri = Origin::parse_route(&complete_uri) .unwrap_or_else(|e| {