From 4010a967a6ff265a21c3b1978ffeecccc78f368f Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Mon, 13 Aug 2018 00:43:29 -0700 Subject: [PATCH] Allow any 'T: Into>' to be mounted. --- core/lib/src/handler.rs | 25 ++++++++++--------------- core/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 index ae7f8715..ab76b1bd 100644 --- a/core/lib/src/handler.rs +++ b/core/lib/src/handler.rs @@ -38,24 +38,13 @@ pub type Outcome<'r> = outcome::Outcome, 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 { -/// 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, Status, Data>; /// } /// } /// +/// impl Into> for CustomHandler { +/// fn into(self) -> Vec { +/// 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, 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>`, 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 index 4f02739d..4309031d 100644 --- 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) -> Self { + pub fn mount>>(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| {