Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rust 2018 #1013

Closed
wants to merge 15 commits into from
1 change: 1 addition & 0 deletions contrib/codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ readme = "../../README.md"
keywords = ["rocket", "contrib", "code", "generation", "proc-macro"]
license = "MIT/Apache-2.0"
build = "build.rs"
edition = "2018"

[features]
database_attribute = []
Expand Down
8 changes: 4 additions & 4 deletions contrib/codegen/src/database.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use proc_macro::TokenStream;
use devise::{Spanned, Result};
use syn::{DataStruct, Fields, Data, Type, LitStr, DeriveInput, Ident, Visibility};
use crate::syn::{DataStruct, Fields, Data, Type, LitStr, DeriveInput, Ident, Visibility};

#[derive(Debug)]
struct DatabaseInvocation {
Expand All @@ -24,12 +24,12 @@ const NO_GENERIC_STRUCTS: &str = "`database` attribute cannot be applied to stru
with generics";

fn parse_invocation(attr: TokenStream, input: TokenStream) -> Result<DatabaseInvocation> {
let attr_stream2 = ::proc_macro2::TokenStream::from(attr);
let attr_stream2 = crate::proc_macro2::TokenStream::from(attr);
let attr_span = attr_stream2.span();
let string_lit = ::syn::parse2::<LitStr>(attr_stream2)
let string_lit = crate::syn::parse2::<LitStr>(attr_stream2)
.map_err(|_| attr_span.error("expected string literal"))?;

let input = ::syn::parse::<DeriveInput>(input).unwrap();
let input = crate::syn::parse::<DeriveInput>(input).unwrap();
if !input.generics.params.is_empty() {
return Err(input.generics.span().error(NO_GENERIC_STRUCTS));
}
Expand Down
5 changes: 3 additions & 2 deletions contrib/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#![feature(crate_visibility_modifier)]
#![recursion_limit="256"]

#![warn(rust_2018_idioms)]

//! # Rocket Contrib - Code Generation
//! This crate implements the code generation portion of the Rocket Contrib
//! crate. This is for officially sanctioned contributor libraries that require
Expand All @@ -24,7 +26,6 @@
//! DATABASE_NAME := (string literal)
//! </pre>

extern crate devise;
extern crate proc_macro;

#[allow(unused_imports)]
Expand All @@ -43,7 +44,7 @@ use proc_macro::TokenStream;
#[cfg(feature = "database_attribute")]
#[proc_macro_attribute]
pub fn database(attr: TokenStream, input: TokenStream) -> TokenStream {
::database::database_attr(attr, input).unwrap_or_else(|diag| {
crate::database::database_attr(attr, input).unwrap_or_else(|diag| {
diag.emit();
TokenStream::new()
})
Expand Down
1 change: 1 addition & 0 deletions contrib/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/SergioBenitez/Rocket"
readme = "../../README.md"
keywords = ["rocket", "web", "framework", "contrib", "contributed"]
license = "MIT/Apache-2.0"
edition = "2018"

[features]
# Internal use only.
Expand Down
4 changes: 2 additions & 2 deletions contrib/lib/src/compression/fairing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ impl Fairing for Compression {
Ok(rocket.manage(ctxt))
}

fn on_response(&self, request: &Request, response: &mut Response) {
fn on_response(&self, request: &Request<'_>, response: &mut Response<'_>) {
let context = request
.guard::<::rocket::State<Context>>()
.guard::<rocket::State<'_, Context>>()
.expect("Compression Context registered in on_attach");

super::CompressionUtils::compress_response(request, response, &context.exclusions);
Expand Down
12 changes: 5 additions & 7 deletions contrib/lib/src/compression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
//! application vulnerable to attacks including BREACH. These risks should be
//! evaluated in the context of your application before enabling compression.
//!
#[cfg(feature="brotli_compression")] extern crate brotli;
#[cfg(feature="gzip_compression")] extern crate flate2;

mod fairing;
mod responder;
Expand All @@ -38,15 +36,15 @@ use rocket::http::hyper::header::{ContentEncoding, Encoding};
use rocket::{Request, Response};

#[cfg(feature = "brotli_compression")]
use self::brotli::enc::backward_references::BrotliEncoderMode;
use brotli::enc::backward_references::BrotliEncoderMode;

#[cfg(feature = "gzip_compression")]
use self::flate2::read::GzEncoder;
use flate2::read::GzEncoder;

struct CompressionUtils;

impl CompressionUtils {
fn accepts_encoding(request: &Request, encoding: &str) -> bool {
fn accepts_encoding(request: &Request<'_>, encoding: &str) -> bool {
request
.headers()
.get("Accept-Encoding")
Expand All @@ -55,7 +53,7 @@ impl CompressionUtils {
.any(|accept| accept == encoding)
}

fn already_encoded(response: &Response) -> bool {
fn already_encoded(response: &Response<'_>) -> bool {
response.headers().get("Content-Encoding").next().is_some()
}

Expand Down Expand Up @@ -84,7 +82,7 @@ impl CompressionUtils {
}
}

fn compress_response(request: &Request, response: &mut Response, exclusions: &[MediaType]) {
fn compress_response(request: &Request<'_>, response: &mut Response<'_>, exclusions: &[MediaType]) {
if CompressionUtils::already_encoded(response) {
return;
}
Expand Down
2 changes: 1 addition & 1 deletion contrib/lib/src/compression/responder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub struct Compress<R>(pub R);

impl<'r, R: Responder<'r>> Responder<'r> for Compress<R> {
#[inline(always)]
fn respond_to(self, request: &Request) -> response::Result<'r> {
fn respond_to(self, request: &Request<'_>) -> response::Result<'r> {
let mut response = Response::build()
.merge(self.0.respond_to(request)?)
.finalize();
Expand Down
35 changes: 18 additions & 17 deletions contrib/lib/src/databases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
//! # struct LogsDbConn(diesel::SqliteConnection);
//! #
//! # type Logs = ();
//! # type Result<T> = ::std::result::Result<T, ()>;
//! # type Result<T> = std::result::Result<T, ()>;
//! #
//! #[get("/logs/<id>")]
//! fn get_logs(conn: LogsDbConn, id: usize) -> Result<Logs> {
Expand Down Expand Up @@ -229,7 +229,7 @@
//! allowing the type to be used as a request guard. This implementation
//! retrieves a connection from the database pool or fails with a
//! `Status::ServiceUnavailable` if no connections are available. The macro also
//! generates an implementation of the [`Deref`](::std::ops::Deref) trait with
//! generates an implementation of the [`Deref`](std::ops::Deref) trait with
//! the internal `Poolable` type as the target.
//!
//! The macro will also generate two inherent methods on the decorated type:
Expand Down Expand Up @@ -395,6 +395,7 @@
//! [request guards]: rocket::request::FromRequest
//! [`Poolable`]: databases::Poolable

#![allow(unused_extern_crates)]
pub extern crate r2d2;

#[cfg(any(feature = "diesel_sqlite_pool",
Expand Down Expand Up @@ -491,7 +492,7 @@ pub enum ConfigError {
/// configuration.
MissingKey,
/// The configuration associated with the key isn't a
/// [`Table`](::rocket::config::Table).
/// [`Table`](rocket::config::Table).
MalformedConfiguration,
/// The required `url` key is missing.
MissingUrl,
Expand Down Expand Up @@ -594,7 +595,7 @@ pub fn database_config<'a>(
}

impl<'a> Display for ConfigError {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
ConfigError::MissingTable => {
write!(f, "A table named `databases` was not found for this configuration")
Expand Down Expand Up @@ -659,15 +660,15 @@ impl<'a> Display for ConfigError {
/// # use std::fmt;
/// # use rocket_contrib::databases::r2d2;
/// # #[derive(Debug)] pub struct Error;
/// # impl ::std::error::Error for Error { }
/// # impl std::error::Error for Error { }
/// # impl fmt::Display for Error {
/// # fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { Ok(()) }
/// # }
/// #
/// # pub struct Connection;
/// # pub struct ConnectionManager;
/// #
/// # type Result<T> = ::std::result::Result<T, Error>;
/// # type Result<T> = std::result::Result<T, Error>;
/// #
/// # impl ConnectionManager {
/// # pub fn new(url: &str) -> Result<Self> { Err(Error) }
Expand Down Expand Up @@ -717,15 +718,15 @@ pub trait Poolable: Send + Sized + 'static {

/// Creates an `r2d2` connection pool for `Manager::Connection`, returning
/// the pool on success.
fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error>;
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error>;
}

#[cfg(feature = "diesel_sqlite_pool")]
impl Poolable for diesel::SqliteConnection {
type Manager = diesel::r2d2::ConnectionManager<diesel::SqliteConnection>;
type Error = r2d2::Error;

fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
let manager = diesel::r2d2::ConnectionManager::new(config.url);
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
}
Expand All @@ -736,7 +737,7 @@ impl Poolable for diesel::PgConnection {
type Manager = diesel::r2d2::ConnectionManager<diesel::PgConnection>;
type Error = r2d2::Error;

fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
let manager = diesel::r2d2::ConnectionManager::new(config.url);
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
}
Expand All @@ -747,7 +748,7 @@ impl Poolable for diesel::MysqlConnection {
type Manager = diesel::r2d2::ConnectionManager<diesel::MysqlConnection>;
type Error = r2d2::Error;

fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
let manager = diesel::r2d2::ConnectionManager::new(config.url);
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
}
Expand All @@ -759,7 +760,7 @@ impl Poolable for postgres::Connection {
type Manager = r2d2_postgres::PostgresConnectionManager;
type Error = DbError<postgres::Error>;

fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
let manager = r2d2_postgres::PostgresConnectionManager::new(config.url, r2d2_postgres::TlsMode::None)
.map_err(DbError::Custom)?;

Expand All @@ -773,7 +774,7 @@ impl Poolable for mysql::Conn {
type Manager = r2d2_mysql::MysqlConnectionManager;
type Error = r2d2::Error;

fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
let opts = mysql::OptsBuilder::from_opts(config.url);
let manager = r2d2_mysql::MysqlConnectionManager::new(opts);
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
Expand All @@ -785,7 +786,7 @@ impl Poolable for rusqlite::Connection {
type Manager = r2d2_sqlite::SqliteConnectionManager;
type Error = r2d2::Error;

fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
let manager = r2d2_sqlite::SqliteConnectionManager::file(config.url);

r2d2::Pool::builder().max_size(config.pool_size).build(manager)
Expand All @@ -797,7 +798,7 @@ impl Poolable for rusted_cypher::GraphClient {
type Manager = r2d2_cypher::CypherConnectionManager;
type Error = r2d2::Error;

fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
let manager = r2d2_cypher::CypherConnectionManager { url: config.url.to_string() };
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
}
Expand All @@ -808,7 +809,7 @@ impl Poolable for redis::Connection {
type Manager = r2d2_redis::RedisConnectionManager;
type Error = DbError<redis::RedisError>;

fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
let manager = r2d2_redis::RedisConnectionManager::new(config.url).map_err(DbError::Custom)?;
r2d2::Pool::builder().max_size(config.pool_size).build(manager)
.map_err(DbError::PoolError)
Expand All @@ -820,7 +821,7 @@ impl Poolable for mongodb::db::Database {
type Manager = r2d2_mongodb::MongodbConnectionManager;
type Error = DbError<mongodb::Error>;

fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
let manager = r2d2_mongodb::MongodbConnectionManager::new_with_uri(config.url).map_err(DbError::Custom)?;
r2d2::Pool::builder().max_size(config.pool_size).build(manager).map_err(DbError::PoolError)
}
Expand All @@ -831,7 +832,7 @@ impl Poolable for memcache::Client {
type Manager = r2d2_memcache::MemcacheConnectionManager;
type Error = DbError<memcache::MemcacheError>;

fn pool(config: DatabaseConfig) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
fn pool(config: DatabaseConfig<'_>) -> Result<r2d2::Pool<Self::Manager>, Self::Error> {
let manager = r2d2_memcache::MemcacheConnectionManager::new(config.url);
r2d2::Pool::builder().max_size(config.pool_size).build(manager).map_err(DbError::PoolError)
}
Expand Down
6 changes: 3 additions & 3 deletions contrib/lib/src/helmet/helmet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rocket::http::uncased::UncasedStr;
use rocket::fairing::{Fairing, Info, Kind};
use rocket::{Request, Response, Rocket};

use helmet::*;
use crate::helmet::*;

/// A [`Fairing`](../../rocket/fairing/trait.Fairing.html) that adds HTTP
/// headers to outgoing responses that control security features on the browser.
Expand Down Expand Up @@ -167,7 +167,7 @@ impl SpaceHelmet {

/// Sets all of the headers in `self.policies` in `response` as long as the
/// header is not already in the response.
fn apply(&self, response: &mut Response) {
fn apply(&self, response: &mut Response<'_>) {
for policy in self.policies.values() {
let name = policy.name();
if response.headers().contains(name.as_str()) {
Expand Down Expand Up @@ -196,7 +196,7 @@ impl Fairing for SpaceHelmet {
}
}

fn on_response(&self, _request: &Request, response: &mut Response) {
fn on_response(&self, _request: &Request<'_>, response: &mut Response<'_>) {
self.apply(response);
}

Expand Down
2 changes: 0 additions & 2 deletions contrib/lib/src/helmet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,6 @@
//!
//! [OWASP]: https://www.owasp.org/index.php/OWASP_Secure_Headers_Project#tab=Headers

extern crate time;

mod helmet;
mod policy;

Expand Down
Loading