Skip to content

Commit

Permalink
Migrate http to Rust 2018.
Browse files Browse the repository at this point in the history
  • Loading branch information
jebrosen authored and SergioBenitez committed Jun 25, 2019
1 parent 42f8af4 commit 90e37be
Show file tree
Hide file tree
Showing 33 changed files with 298 additions and 304 deletions.
1 change: 1 addition & 0 deletions core/http/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ readme = "../../README.md"
keywords = ["rocket", "web", "framework", "http"]
license = "MIT/Apache-2.0"
categories = ["web-programming"]
edition = "2018"

[features]
default = []
Expand Down
14 changes: 7 additions & 7 deletions core/http/src/accept.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use std::fmt;

use smallvec::SmallVec;

use {Header, MediaType};
use ext::IntoCollection;
use parse::parse_accept;
use crate::{Header, MediaType};
use crate::ext::IntoCollection;
use crate::parse::parse_accept;

/// A `MediaType` with an associated quality value.
#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -88,7 +88,7 @@ pub enum AcceptParams {
Dynamic(SmallVec<[QMediaType; 1]>)
}

impl ::pear::parsers::Collection for AcceptParams {
impl pear::parsers::Collection for AcceptParams {
type Item = QMediaType;

fn new() -> Self {
Expand Down Expand Up @@ -130,7 +130,7 @@ impl PartialEq for AcceptParams {
/// [`Request::accept()`] method. The [`preferred()`] method can be used to
/// retrieve the client's preferred media type.
///
/// [`Request::accept`]: ::rocket::Request::accept()
/// [`Request::accept`]: rocket::Request::accept()
/// [`preferred()`]: Accept::preferred()
///
/// An `Accept` type with a single, common media type can be easily constructed
Expand Down Expand Up @@ -353,7 +353,7 @@ impl Accept {
}

impl fmt::Display for Accept {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, media_type) in self.iter().enumerate() {
if i >= 1 {
write!(f, ", {}", media_type.0)?;
Expand Down Expand Up @@ -387,7 +387,7 @@ impl Into<Header<'static>> for Accept {

#[cfg(test)]
mod test {
use {Accept, MediaType};
use crate::{Accept, MediaType};

macro_rules! assert_preference {
($string:expr, $expect:expr) => (
Expand Down
10 changes: 5 additions & 5 deletions core/http/src/content_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::ops::Deref;
use std::str::FromStr;
use std::fmt;

use header::Header;
use media_type::{MediaType, Source};
use ext::IntoCollection;
use hyper::mime::Mime;
use crate::header::Header;
use crate::media_type::{MediaType, Source};
use crate::ext::IntoCollection;
use crate::hyper::mime::Mime;

/// Representation of HTTP Content-Types.
///
Expand Down Expand Up @@ -350,7 +350,7 @@ impl fmt::Display for ContentType {
/// assert_eq!(ct, "application/json");
/// ```
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
Expand Down
26 changes: 13 additions & 13 deletions core/http/src/cookies.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;
use std::cell::RefMut;

use Header;
use crate::Header;
use cookie::Delta;

#[doc(hidden)] pub use self::key::*;
Expand Down Expand Up @@ -40,7 +40,7 @@ mod key {
/// can be added or removed via the [`add()`], [`add_private()`], [`remove()`],
/// and [`remove_private()`] methods.
///
/// [`Request::cookies()`]: ::rocket::Request::cookies()
/// [`Request::cookies()`]: rocket::Request::cookies()
/// [`get()`]: #method.get
/// [`get_private()`]: #method.get_private
/// [`add()`]: #method.add
Expand Down Expand Up @@ -84,10 +84,10 @@ mod key {
/// // In practice, we'd probably fetch the user from the database.
/// struct User(usize);
///
/// impl<'a, 'r> FromRequest<'a, 'r> for User {
/// impl FromRequest<'_, '_> for User {
/// type Error = !;
///
/// fn from_request(request: &'a Request<'r>) -> request::Outcome<User, !> {
/// fn from_request(request: &Request<'_>) -> request::Outcome<User, !> {
/// request.cookies()
/// .get_private("user_id")
/// .and_then(|cookie| cookie.value().parse().ok())
Expand Down Expand Up @@ -260,7 +260,7 @@ impl<'a> Cookies<'a> {
/// WARNING: This is unstable! Do not use this method outside of Rocket!
#[inline]
#[doc(hidden)]
pub fn delta(&self) -> Delta {
pub fn delta(&self) -> Delta<'_> {
match *self {
Cookies::Jarred(ref jar, _) => jar.delta(),
Cookies::Empty(ref jar) => jar.delta()
Expand All @@ -269,7 +269,7 @@ impl<'a> Cookies<'a> {
}

#[cfg(feature = "private-cookies")]
impl<'a> Cookies<'a> {
impl Cookies<'_> {
/// Returns a reference to the `Cookie` inside this collection with the name
/// `name` and authenticates and decrypts the cookie's value, returning a
/// `Cookie` with the decrypted value. If the cookie cannot be found, or the
Expand Down Expand Up @@ -362,7 +362,7 @@ impl<'a> Cookies<'a> {
}

if cookie.expires().is_none() {
cookie.set_expires(::time::now() + ::time::Duration::weeks(1));
cookie.set_expires(time::now() + time::Duration::weeks(1));
}

if cookie.same_site().is_none() {
Expand Down Expand Up @@ -400,23 +400,23 @@ impl<'a> Cookies<'a> {
}
}

impl<'a> fmt::Debug for Cookies<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
impl fmt::Debug for Cookies<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Cookies::Jarred(ref jar, _) => write!(f, "{:?}", jar),
Cookies::Empty(ref jar) => write!(f, "{:?}", jar)
}
}
}

impl<'c> From<Cookie<'c>> for Header<'static> {
fn from(cookie: Cookie) -> Header<'static> {
impl From<Cookie<'_>> for Header<'static> {
fn from(cookie: Cookie<'_>) -> Header<'static> {
Header::new("Set-Cookie", cookie.encoded().to_string())
}
}

impl<'a, 'c> From<&'a Cookie<'c>> for Header<'static> {
fn from(cookie: &Cookie) -> Header<'static> {
impl From<&Cookie<'_>> for Header<'static> {
fn from(cookie: &Cookie<'_>) -> Header<'static> {
Header::new("Set-Cookie", cookie.encoded().to_string())
}
}
8 changes: 4 additions & 4 deletions core/http/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl<T> IntoCollection<T> for Vec<T> {

macro_rules! impl_for_slice {
($($size:tt)*) => (
impl<'a, T: Clone> IntoCollection<T> for &'a [T $($size)*] {
impl<T: Clone> IntoCollection<T> for &[T $($size)*] {
#[inline(always)]
fn into_collection<A: Array<Item=T>>(self) -> SmallVec<A> {
self.iter().cloned().collect()
Expand Down Expand Up @@ -90,7 +90,7 @@ impl<T: IntoOwned> IntoOwned for Option<T> {
}
}

impl<'a, B: 'static + ToOwned + ?Sized> IntoOwned for Cow<'a, B> {
impl<B: 'static + ToOwned + ?Sized> IntoOwned for Cow<'_, B> {
type Owned = Cow<'static, B>;

#[inline(always)]
Expand All @@ -104,7 +104,7 @@ use std::path::Path;
// Outside of http, this is used by a test.
#[doc(hidden)]
pub trait Normalize {
fn normalized_str(&self) -> Cow<str>;
fn normalized_str(&self) -> Cow<'_, str>;
}

impl<T: AsRef<Path>> Normalize for T {
Expand All @@ -114,7 +114,7 @@ impl<T: AsRef<Path>> Normalize for T {
}

#[cfg(not(windows))]
fn normalized_str(&self) -> Cow<str> {
fn normalized_str(&self) -> Cow<'_, str> {
self.as_ref().to_string_lossy()
}
}
17 changes: 9 additions & 8 deletions core/http/src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::fmt;

use indexmap::IndexMap;

use uncased::{Uncased, UncasedStr};
use crate::uncased::{Uncased, UncasedStr};

/// Simple representation of an HTTP header.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
Expand All @@ -17,8 +17,9 @@ pub struct Header<'h> {
impl<'h> Header<'h> {
/// Constructs a new header. This method should be used rarely and only for
/// non-standard headers. Instead, prefer to use the `Into<Header>`
/// implementations of many types, including [`ContentType`] and all of the
/// headers in [`http::hyper::header`](hyper::header).
/// implementations of many types, including
/// [`ContentType`](crate::ContentType) and all of the headers in
/// [`http::hyper::header`](hyper::header).
///
/// # Examples
///
Expand Down Expand Up @@ -104,9 +105,9 @@ impl<'h> Header<'h> {
}
}

impl<'h> fmt::Display for Header<'h> {
impl fmt::Display for Header<'_> {
#[inline(always)]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.name, self.value)
}
}
Expand Down Expand Up @@ -222,7 +223,7 @@ impl<'h> HeaderMap<'h> {
/// assert_eq!(values.next(), None);
/// ```
#[inline]
pub fn get<'a>(&'a self, name: &str) -> impl Iterator<Item=&'a str> {
pub fn get(&self, name: &str) -> impl Iterator<Item=&str> {
self.headers
.get(UncasedStr::new(name))
.into_iter()
Expand Down Expand Up @@ -520,7 +521,7 @@ impl<'h> HeaderMap<'h> {
/// ```
#[inline(always)]
pub fn remove_all(&mut self) -> Vec<Header<'h>> {
let old_map = ::std::mem::replace(self, HeaderMap::new());
let old_map = std::mem::replace(self, HeaderMap::new());
old_map.into_iter().collect()
}

Expand Down Expand Up @@ -560,7 +561,7 @@ impl<'h> HeaderMap<'h> {
/// }
/// }
/// ```
pub fn iter(&self) -> impl Iterator<Item=Header> {
pub fn iter(&self) -> impl Iterator<Item=Header<'_>> {
self.headers.iter().flat_map(|(key, values)| {
values.iter().map(move |val| {
Header::new(key.as_str(), &**val)
Expand Down
36 changes: 17 additions & 19 deletions core/http/src/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,38 @@
//! These types will, with certainty, be removed with time, but they reside here
//! while necessary.

extern crate hyper;
#[doc(hidden)] pub use hyper::server::Request as Request;
#[doc(hidden)] pub use hyper::server::Response as Response;
#[doc(hidden)] pub use hyper::server::Server as Server;
#[doc(hidden)] pub use hyper::server::Handler as Handler;

#[doc(hidden)] pub use self::hyper::server::Request as Request;
#[doc(hidden)] pub use self::hyper::server::Response as Response;
#[doc(hidden)] pub use self::hyper::server::Server as Server;
#[doc(hidden)] pub use self::hyper::server::Handler as Handler;
#[doc(hidden)] pub use hyper::net;

#[doc(hidden)] pub use self::hyper::net;
#[doc(hidden)] pub use hyper::method::Method;
#[doc(hidden)] pub use hyper::status::StatusCode;
#[doc(hidden)] pub use hyper::error::Error;
#[doc(hidden)] pub use hyper::uri::RequestUri;
#[doc(hidden)] pub use hyper::http::h1;
#[doc(hidden)] pub use hyper::buffer;

#[doc(hidden)] pub use self::hyper::method::Method;
#[doc(hidden)] pub use self::hyper::status::StatusCode;
#[doc(hidden)] pub use self::hyper::error::Error;
#[doc(hidden)] pub use self::hyper::uri::RequestUri;
#[doc(hidden)] pub use self::hyper::http::h1;
#[doc(hidden)] pub use self::hyper::buffer;
pub use hyper::mime;

pub use self::hyper::mime;

/// Type alias to `self::hyper::Response<'a, self::hyper::net::Fresh>`.
/// Type alias to `hyper::Response<'a, hyper::net::Fresh>`.
#[doc(hidden)] pub type FreshResponse<'a> = self::Response<'a, self::net::Fresh>;

/// Reexported Hyper header types.
pub mod header {
use Header;
use crate::Header;

use super::hyper::header::Header as HyperHeaderTrait;
use hyper::header::Header as HyperHeaderTrait;

macro_rules! import_hyper_items {
($($item:ident),*) => ($(pub use super::hyper::header::$item;)*)
($($item:ident),*) => ($(pub use hyper::header::$item;)*)
}

macro_rules! import_hyper_headers {
($($name:ident),*) => ($(
impl ::std::convert::From<self::$name> for Header<'static> {
impl std::convert::From<self::$name> for Header<'static> {
fn from(header: self::$name) -> Header<'static> {
Header::new($name::header_name(), header.to_string())
}
Expand Down
31 changes: 13 additions & 18 deletions core/http/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#![feature(doc_cfg)]
#![recursion_limit="512"]

#![warn(rust_2018_idioms)]

//! Types that map to concepts in HTTP.
//!
//! This module exports types that map to HTTP concepts or to the underlying
Expand All @@ -13,13 +15,6 @@
//! [#17]: https://github.com/SergioBenitez/Rocket/issues/17

#[macro_use] extern crate pear;
extern crate percent_encoding;
extern crate smallvec;
extern crate cookie;
extern crate time;
extern crate indexmap;
extern crate state;
extern crate unicode_xid;

pub mod hyper;
pub mod uri;
Expand Down Expand Up @@ -54,20 +49,20 @@ pub mod private {
// We need to export these for codegen, but otherwise it's unnecessary.
// TODO: Expose a `const fn` from ContentType when possible. (see RFC#1817)
// FIXME(rustc): These show up in the rexported module.
pub use parse::Indexed;
pub use media_type::{MediaParams, Source};
pub use crate::parse::Indexed;
pub use crate::media_type::{MediaParams, Source};
pub use smallvec::{SmallVec, Array};

// This one we need to expose for core.
pub use cookies::{Key, CookieJar};
pub use crate::cookies::{Key, CookieJar};
}

pub use method::Method;
pub use content_type::ContentType;
pub use accept::{Accept, QMediaType};
pub use status::{Status, StatusClass};
pub use header::{Header, HeaderMap};
pub use raw_str::RawStr;
pub use crate::method::Method;
pub use crate::content_type::ContentType;
pub use crate::accept::{Accept, QMediaType};
pub use crate::status::{Status, StatusClass};
pub use crate::header::{Header, HeaderMap};
pub use crate::raw_str::RawStr;

pub use media_type::MediaType;
pub use cookies::{Cookie, SameSite, Cookies};
pub use crate::media_type::MediaType;
pub use crate::cookies::{Cookie, SameSite, Cookies};
Loading

0 comments on commit 90e37be

Please sign in to comment.