From 13a3f7368e3355ac614250c2a0db442b50288312 Mon Sep 17 00:00:00 2001 From: Yong Wen Chua Date: Wed, 21 Nov 2018 12:17:40 +0800 Subject: [PATCH] Upgrade to Rust 2018 to fix Macro use and import issues (#52) * Upgrade to Rust 2018 to fix Macro use and import issues Fixes #51 * Cargo fmt --- Cargo.toml | 1 + examples/fairing.rs | 4 +-- examples/guard.rs | 12 ++++----- examples/json.rs | 6 ++--- examples/manual.rs | 8 +++--- examples/mix.rs | 6 ++--- src/fairing.rs | 14 +++++----- src/headers.rs | 4 +-- src/lib.rs | 63 ++++++++++++++------------------------------- tests/fairing.rs | 3 +-- tests/guard.rs | 18 ++++++------- tests/headers.rs | 3 +-- tests/manual.rs | 9 +++---- tests/mix.rs | 6 ++--- 14 files changed, 66 insertions(+), 91 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 647c208..56517be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ repository = "https://github.com/lawliet89/rocket_cors" documentation = "https://docs.rs/rocket_cors/" keywords = ["rocket", "cors"] categories = ["web-programming"] +edition = "2018" [badges] travis-ci = { repository = "lawliet89/rocket_cors" } diff --git a/examples/fairing.rs b/examples/fairing.rs index 88a4b7e..c640078 100644 --- a/examples/fairing.rs +++ b/examples/fairing.rs @@ -1,6 +1,6 @@ #![feature(proc_macro_hygiene, decl_macro)] -extern crate rocket; -extern crate rocket_cors; +use rocket; +use rocket_cors; use rocket::http::Method; use rocket::{get, routes}; diff --git a/examples/guard.rs b/examples/guard.rs index ab087c8..3971e64 100644 --- a/examples/guard.rs +++ b/examples/guard.rs @@ -1,6 +1,6 @@ #![feature(proc_macro_hygiene, decl_macro)] -extern crate rocket; -extern crate rocket_cors; +use rocket; +use rocket_cors; use std::io::Cursor; @@ -11,13 +11,13 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins, Guard, Responder}; /// Using a `Responder` -- the usual way you would use this #[get("/")] -fn responder(cors: Guard) -> Responder<&str> { +fn responder(cors: Guard<'_>) -> Responder<'_, &str> { cors.responder("Hello CORS!") } /// Using a `Response` instead of a `Responder`. You generally won't have to do this. #[get("/response")] -fn response(cors: Guard) -> Response { +fn response(cors: Guard<'_>) -> Response<'_> { let mut response = Response::new(); response.set_sized_body(Cursor::new("Hello CORS!")); cors.response(response) @@ -25,13 +25,13 @@ fn response(cors: Guard) -> Response { /// Manually mount an OPTIONS route for your own handling #[options("/manual")] -fn manual_options(cors: Guard) -> Responder<&str> { +fn manual_options(cors: Guard<'_>) -> Responder<'_, &str> { cors.responder("Manual OPTIONS preflight handling") } /// Manually mount an OPTIONS route for your own handling #[get("/manual")] -fn manual(cors: Guard) -> Responder<&str> { +fn manual(cors: Guard<'_>) -> Responder<'_, &str> { cors.responder("Manual OPTIONS preflight handling") } diff --git a/examples/json.rs b/examples/json.rs index c1bfd94..08131ca 100644 --- a/examples/json.rs +++ b/examples/json.rs @@ -2,9 +2,9 @@ //! //! Note: This requires the `serialization` feature which is enabled by default. #![feature(proc_macro_hygiene, decl_macro)] -extern crate rocket; -extern crate rocket_cors as cors; -extern crate serde_json; + +use rocket_cors as cors; +use serde_json; use crate::cors::{AllowedHeaders, AllowedOrigins, Cors}; use rocket::http::Method; diff --git a/examples/manual.rs b/examples/manual.rs index bc0b4de..5b4bc05 100644 --- a/examples/manual.rs +++ b/examples/manual.rs @@ -1,6 +1,6 @@ #![feature(proc_macro_hygiene, decl_macro)] -extern crate rocket; -extern crate rocket_cors; +use rocket; +use rocket_cors; use std::io::Cursor; @@ -13,7 +13,7 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors}; /// Note that the `'r` lifetime annotation is not requred here because `State` borrows with lifetime /// `'r` and so does `Responder`! #[get("/")] -fn borrowed(options: State) -> impl Responder { +fn borrowed(options: State<'_, Cors>) -> impl Responder<'_> { options .inner() .respond_borrowed(|guard| guard.responder("Hello CORS")) @@ -23,7 +23,7 @@ fn borrowed(options: State) -> impl Responder { /// Note that the `'r` lifetime annotation is not requred here because `State` borrows with lifetime /// `'r` and so does `Responder`! #[get("/response")] -fn response(options: State) -> impl Responder { +fn response(options: State<'_, Cors>) -> impl Responder<'_> { let mut response = Response::new(); response.set_sized_body(Cursor::new("Hello CORS!")); diff --git a/examples/mix.rs b/examples/mix.rs index f2fd0dd..0ba1815 100644 --- a/examples/mix.rs +++ b/examples/mix.rs @@ -4,8 +4,8 @@ //! `ping` route that you want to allow all Origins to access. #![feature(proc_macro_hygiene, decl_macro)] -extern crate rocket; -extern crate rocket_cors; +use rocket; +use rocket_cors; use rocket::http::Method; use rocket::response::Responder; @@ -14,7 +14,7 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors, Guard}; /// The "usual" app route #[get("/")] -fn app(cors: Guard) -> rocket_cors::Responder<&str> { +fn app(cors: Guard<'_>) -> rocket_cors::Responder<'_, &str> { cors.responder("Hello CORS!") } diff --git a/src/fairing.rs b/src/fairing.rs index 6277e26..18a6834 100644 --- a/src/fairing.rs +++ b/src/fairing.rs @@ -1,6 +1,6 @@ //! Fairing implementation -use log::{error, info, log}; +use ::log::{error, info, log}; use rocket::http::{self, uri::Origin, Status}; use rocket::{self, error_, info_, log_, Outcome, Request}; @@ -16,7 +16,7 @@ enum CorsValidation { /// Route for Fairing error handling pub(crate) fn fairing_error_route<'r>( - request: &'r Request, + request: &'r Request<'_>, _: rocket::Data, ) -> rocket::handler::Outcome<'r> { let status = request @@ -36,7 +36,7 @@ fn fairing_route(rank: isize) -> rocket::Route { } /// Modifies a `Request` to route to Fairing error handler -fn route_to_fairing_error_handler(options: &Cors, status: u16, request: &mut Request) { +fn route_to_fairing_error_handler(options: &Cors, status: u16, request: &mut Request<'_>) { let origin = Origin::parse_owned(format!("{}/{}", options.fairing_route_base, status)).unwrap(); request.set_method(http::Method::Get); @@ -45,8 +45,8 @@ fn route_to_fairing_error_handler(options: &Cors, status: u16, request: &mut Req fn on_response_wrapper( options: &Cors, - request: &Request, - response: &mut rocket::Response, + request: &Request<'_>, + response: &mut rocket::Response<'_>, ) -> Result<(), Error> { let origin = match origin(request)? { None => { @@ -112,7 +112,7 @@ impl rocket::fairing::Fairing for Cors { } } - fn on_request(&self, request: &mut Request, _: &rocket::Data) { + fn on_request(&self, request: &mut Request<'_>, _: &rocket::Data) { let result = match validate(self, request) { Ok(_) => CorsValidation::Success, Err(err) => { @@ -126,7 +126,7 @@ impl rocket::fairing::Fairing for Cors { let _ = request.local_cache(|| result); } - fn on_response(&self, request: &Request, response: &mut rocket::Response) { + fn on_response(&self, request: &Request<'_>, response: &mut rocket::Response<'_>) { if let Err(err) = on_response_wrapper(self, request, response) { error_!("Fairings on_response error: {}\nMost likely a bug", err); response.set_status(Status::InternalServerError); diff --git a/src/headers.rs b/src/headers.rs index 300c73f..7685dfd 100644 --- a/src/headers.rs +++ b/src/headers.rs @@ -34,7 +34,7 @@ impl Deref for HeaderFieldName { } impl fmt::Display for HeaderFieldName { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } @@ -68,7 +68,7 @@ pub type HeaderFieldNamesSet = HashSet; pub struct Url(#[cfg_attr(feature = "serialization", serde(with = "url_serde"))] url::Url); impl fmt::Display for Url { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { self.0.fmt(f) } } diff --git a/src/lib.rs b/src/lib.rs index 57c8529..7bc206f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -550,29 +550,6 @@ )] #![doc(test(attr(allow(unused_variables), deny(warnings))))] -extern crate log; -extern crate rocket; -extern crate unicase; -extern crate url; - -#[cfg(feature = "serialization")] -extern crate serde; -#[cfg(feature = "serialization")] -extern crate serde_derive; -#[cfg(feature = "serialization")] -extern crate unicase_serde; -#[cfg(feature = "serialization")] -extern crate url_serde; - -#[cfg(test)] -extern crate hyper; -#[cfg(feature = "serialization")] -#[cfg(test)] -extern crate serde_json; -#[cfg(feature = "serialization")] -#[cfg(test)] -extern crate serde_test; - #[cfg(test)] #[macro_use] mod test_macros; @@ -588,7 +565,7 @@ use std::marker::PhantomData; use std::ops::Deref; use std::str::FromStr; -use log::{error, info, log}; +use ::log::{error, info, log}; use rocket::http::{self, Status}; use rocket::request::{FromRequest, Request}; use rocket::response; @@ -687,7 +664,7 @@ impl error::Error for Error { } } - fn cause(&self) -> Option<&error::Error> { + fn cause(&self) -> Option<&dyn error::Error> { match *self { Error::BadOrigin(ref e) => Some(e), _ => Some(self), @@ -696,7 +673,7 @@ impl error::Error for Error { } impl fmt::Display for Error { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { Error::BadOrigin(ref e) => fmt::Display::fmt(e, f), _ => write!(f, "{}", error::Error::description(self)), @@ -705,7 +682,7 @@ impl fmt::Display for Error { } impl<'r> response::Responder<'r> for Error { - fn respond_to(self, _: &Request) -> Result, Status> { + fn respond_to(self, _: &Request<'_>) -> Result, Status> { error_!("CORS Error: {}", self); Err(self.status()) } @@ -786,7 +763,7 @@ impl From for Method { } impl fmt::Display for Method { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Display::fmt(&self.0, f) } } @@ -820,7 +797,7 @@ mod method_serde { impl<'de> Visitor<'de> for MethodVisitor { type Value = Method; - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { formatter.write_str("a string containing a HTTP Verb") } @@ -1303,7 +1280,7 @@ impl Response { /// Merge CORS headers with an existing `rocket::Response`. /// /// This will overwrite any existing CORS headers - fn merge(&self, response: &mut response::Response) { + fn merge(&self, response: &mut response::Response<'_>) { // TODO: We should be able to remove this let origin = match self.allow_origin { None => { @@ -1426,7 +1403,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Guard<'r> { type Error = Error; fn from_request(request: &'a Request<'r>) -> rocket::request::Outcome { - let options = match request.guard::>() { + let options = match request.guard::>() { Outcome::Success(options) => options, _ => { let error = Error::MissingCorsInRocketState; @@ -1461,7 +1438,7 @@ impl<'a, 'r> FromRequest<'a, 'r> for Guard<'r> { pub struct Responder<'r, R> { responder: R, cors_response: Response, - marker: PhantomData>, + marker: PhantomData>, } impl<'r, R: response::Responder<'r>> Responder<'r, R> { @@ -1474,7 +1451,7 @@ impl<'r, R: response::Responder<'r>> Responder<'r, R> { } /// Respond to a request - fn respond(self, request: &Request) -> response::Result<'r> { + fn respond(self, request: &Request<'_>) -> response::Result<'r> { let mut response = self.responder.respond_to(request)?; // handle status errors? self.cors_response.merge(&mut response); Ok(response) @@ -1482,7 +1459,7 @@ impl<'r, R: response::Responder<'r>> Responder<'r, R> { } impl<'r, R: response::Responder<'r>> response::Responder<'r> for Responder<'r, R> { - fn respond_to(self, request: &Request) -> response::Result<'r> { + fn respond_to(self, request: &Request<'_>) -> response::Result<'r> { self.respond(request) } } @@ -1514,7 +1491,7 @@ where } } - fn build_guard(&self, request: &Request) -> Result, Error> { + fn build_guard(&self, request: &Request<'_>) -> Result, Error> { let response = Response::validate_and_build(&self.options, request)?; Ok(Guard::new(response)) } @@ -1525,7 +1502,7 @@ where F: FnOnce(Guard<'r>) -> R + 'r, R: response::Responder<'r>, { - fn respond_to(self, request: &Request) -> response::Result<'r> { + fn respond_to(self, request: &Request<'_>) -> response::Result<'r> { let guard = match self.build_guard(request) { Ok(guard) => guard, Err(err) => { @@ -1554,7 +1531,7 @@ enum ValidationResult { } /// Validates a request for CORS and returns a CORS Response -fn validate_and_build(options: &Cors, request: &Request) -> Result { +fn validate_and_build(options: &Cors, request: &Request<'_>) -> Result { let result = validate(options, request)?; Ok(match result { @@ -1567,7 +1544,7 @@ fn validate_and_build(options: &Cors, request: &Request) -> Result Result { +fn validate(options: &Cors, request: &Request<'_>) -> Result { // 1. If the Origin header is not present terminate this set of steps. // The request is outside the scope of this specification. let origin = origin(request)?; @@ -1644,7 +1621,7 @@ fn validate_allowed_headers( } /// Gets the `Origin` request header from the request -fn origin(request: &Request) -> Result, Error> { +fn origin(request: &Request<'_>) -> Result, Error> { match Origin::from_request(request) { Outcome::Forward(()) => Ok(None), Outcome::Success(origin) => Ok(Some(origin)), @@ -1653,7 +1630,7 @@ fn origin(request: &Request) -> Result, Error> { } /// Gets the `Access-Control-Request-Method` request header from the request -fn request_method(request: &Request) -> Result, Error> { +fn request_method(request: &Request<'_>) -> Result, Error> { match AccessControlRequestMethod::from_request(request) { Outcome::Forward(()) => Ok(None), Outcome::Success(method) => Ok(Some(method)), @@ -1662,7 +1639,7 @@ fn request_method(request: &Request) -> Result Result, Error> { +fn request_headers(request: &Request<'_>) -> Result, Error> { match AccessControlRequestHeaders::from_request(request) { Outcome::Forward(()) => Ok(None), Outcome::Success(geaders) => Ok(Some(geaders)), @@ -1887,10 +1864,10 @@ pub fn catch_all_options_routes() -> Vec { /// Handler for the "catch all options route" fn catch_all_options_route_handler<'r>( - request: &'r Request, + request: &'r Request<'_>, _: rocket::Data, ) -> rocket::handler::Outcome<'r> { - let guard: Guard = match request.guard() { + let guard: Guard<'_> = match request.guard() { Outcome::Success(guard) => guard, Outcome::Failure((status, _)) => return rocket::handler::Outcome::failure(status), Outcome::Forward(()) => unreachable!("Should not be reachable"), diff --git a/tests/fairing.rs b/tests/fairing.rs index e41008e..7d45232 100644 --- a/tests/fairing.rs +++ b/tests/fairing.rs @@ -1,9 +1,8 @@ //! This crate tests using `rocket_cors` using Fairings #![feature(proc_macro_hygiene, decl_macro)] -extern crate hyper; +use hyper; #[macro_use] extern crate rocket; -extern crate rocket_cors; use std::str::FromStr; diff --git a/tests/guard.rs b/tests/guard.rs index e354fed..13e7a42 100644 --- a/tests/guard.rs +++ b/tests/guard.rs @@ -1,9 +1,9 @@ //! This crate tests using `rocket_cors` using the per-route handling with request guard #![feature(proc_macro_hygiene, decl_macro)] -extern crate hyper; +use hyper; #[macro_use] extern crate rocket; -extern crate rocket_cors as cors; +use rocket_cors as cors; use std::str::FromStr; @@ -13,42 +13,42 @@ use rocket::local::Client; use rocket::{Response, State}; #[get("/")] -fn cors(cors: cors::Guard) -> cors::Responder<&str> { +fn cors(cors: cors::Guard<'_>) -> cors::Responder<'_, &str> { cors.responder("Hello CORS") } #[get("/panic")] -fn panicking_route(_cors: cors::Guard) { +fn panicking_route(_cors: cors::Guard<'_>) { panic!("This route will panic"); } /// Manually specify our own OPTIONS route #[options("/manual")] -fn cors_manual_options(cors: cors::Guard) -> cors::Responder<&str> { +fn cors_manual_options(cors: cors::Guard<'_>) -> cors::Responder<'_, &str> { cors.responder("Manual CORS Preflight") } /// Manually specify our own OPTIONS route #[get("/manual")] -fn cors_manual(cors: cors::Guard) -> cors::Responder<&str> { +fn cors_manual(cors: cors::Guard<'_>) -> cors::Responder<'_, &str> { cors.responder("Hello CORS") } /// Using a `Response` instead of a `Responder` #[get("/response")] -fn response(cors: cors::Guard) -> Response { +fn response(cors: cors::Guard<'_>) -> Response<'_> { cors.response(Response::new()) } /// `Responder` with String #[get("/responder/string")] -fn responder_string(cors: cors::Guard) -> cors::Responder { +fn responder_string(cors: cors::Guard<'_>) -> cors::Responder<'_, String> { cors.responder("Hello CORS".to_string()) } /// `Responder` with 'static () #[get("/responder/unit")] -fn responder_unit(cors: cors::Guard) -> cors::Responder<()> { +fn responder_unit(cors: cors::Guard<'_>) -> cors::Responder<'_, ()> { cors.responder(()) } diff --git a/tests/headers.rs b/tests/headers.rs index 2511591..825c3f5 100644 --- a/tests/headers.rs +++ b/tests/headers.rs @@ -1,9 +1,8 @@ //! This crate tests that all the request headers are parsed correctly in the round trip #![feature(proc_macro_hygiene, decl_macro)] -extern crate hyper; +use hyper; #[macro_use] extern crate rocket; -extern crate rocket_cors; use std::ops::Deref; use std::str::FromStr; diff --git a/tests/manual.rs b/tests/manual.rs index 725319f..9adcf36 100644 --- a/tests/manual.rs +++ b/tests/manual.rs @@ -1,9 +1,8 @@ //! This crate tests using `rocket_cors` using manual mode #![feature(proc_macro_hygiene, decl_macro)] -extern crate hyper; +use hyper; #[macro_use] extern crate rocket; -extern crate rocket_cors; use std::str::FromStr; @@ -16,14 +15,14 @@ use rocket_cors::*; /// Using a borrowed `Cors` #[get("/")] -fn cors(options: State) -> impl Responder { +fn cors(options: State<'_, Cors>) -> impl Responder<'_> { options .inner() .respond_borrowed(|guard| guard.responder("Hello CORS")) } #[get("/panic")] -fn panicking_route(options: State) -> impl Responder { +fn panicking_route(options: State<'_, Cors>) -> impl Responder<'_> { options.inner().respond_borrowed(|_| -> () { panic!("This route will panic"); }) @@ -49,7 +48,7 @@ fn owned<'r>() -> impl Responder<'r> { /// `Responder` with String #[get("/")] -fn responder_string(options: State) -> impl Responder { +fn responder_string(options: State<'_, Cors>) -> impl Responder<'_> { options .inner() .respond_borrowed(|guard| guard.responder("Hello CORS".to_string())) diff --git a/tests/mix.rs b/tests/mix.rs index 9731323..cbe600a 100644 --- a/tests/mix.rs +++ b/tests/mix.rs @@ -3,10 +3,10 @@ //! In this example, you typically have an application wide `Cors` struct except for one specific //! `ping` route that you want to allow all Origins to access. #![feature(proc_macro_hygiene, decl_macro)] -extern crate hyper; +use hyper; #[macro_use] extern crate rocket; -extern crate rocket_cors; +use rocket_cors; use std::str::FromStr; @@ -18,7 +18,7 @@ use rocket_cors::{AllowedHeaders, AllowedOrigins, Cors, Guard}; /// The "usual" app route #[get("/")] -fn app(cors: Guard) -> rocket_cors::Responder<&str> { +fn app(cors: Guard<'_>) -> rocket_cors::Responder<'_, &str> { cors.responder("Hello CORS!") }