Skip to content

Commit

Permalink
chore(lib): add dyn keyword to trait objects (#1820)
Browse files Browse the repository at this point in the history
Requires Rust 1.27.
  • Loading branch information
seanmonstar authored Jun 3, 2019
1 parent e0f5a9c commit 01c03db
Show file tree
Hide file tree
Showing 28 changed files with 107 additions and 94 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ matrix:
- rust: stable
env: FEATURES="--no-default-features"
# Minimum Supported Rust Version
- rust: 1.26.0
- rust: 1.27.0
env: FEATURES="--no-default-features --features runtime" BUILD_ONLY="1"

before_script:
Expand Down
3 changes: 2 additions & 1 deletion examples/echo.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![deny(warnings)]
extern crate futures;
extern crate hyper;

Expand All @@ -12,7 +13,7 @@ use hyper::{Body, Method, Request, Response, Server, StatusCode};
///
/// A boxed Future (trait object) is used as it is easier to understand
/// and extend with more types. Advanced users could switch to `Either`.
type BoxFut = Box<Future<Item = Response<Body>, Error = hyper::Error> + Send>;
type BoxFut = Box<dyn Future<Item = Response<Body>, Error = hyper::Error> + Send>;

/// This is our service handler. It receives a Request, routes on its
/// path, and returns a Future of a Response.
Expand Down
2 changes: 1 addition & 1 deletion examples/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ static MISSING: &[u8] = b"Missing field";
static NOTNUMERIC: &[u8] = b"Number field is not numeric";

// Using service_fn, we can turn this function into a `Service`.
fn param_example(req: Request<Body>) -> Box<Future<Item=Response<Body>, Error=hyper::Error> + Send> {
fn param_example(req: Request<Body>) -> Box<dyn Future<Item=Response<Body>, Error=hyper::Error> + Send> {
match (req.method(), req.uri().path()) {
(&Method::GET, "/") | (&Method::GET, "/post") => {
Box::new(future::ok(Response::new(INDEX.into())))
Expand Down
2 changes: 1 addition & 1 deletion examples/send_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn main() {
hyper::rt::run(server);
}

type ResponseFuture = Box<Future<Item=Response<Body>, Error=io::Error> + Send>;
type ResponseFuture = Box<dyn Future<Item=Response<Body>, Error=io::Error> + Send>;

fn response_examples(req: Request<Body>) -> ResponseFuture {
match (req.method(), req.uri().path()) {
Expand Down
2 changes: 1 addition & 1 deletion examples/web_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static INDEX: &[u8] = b"<a href=\"test.html\">test.html</a>";
static POST_DATA: &str = r#"{"original": "data"}"#;

type GenericError = Box<dyn std::error::Error + Send + Sync>;
type ResponseFuture = Box<Future<Item=Response<Body>, Error=GenericError> + Send>;
type ResponseFuture = Box<dyn Future<Item=Response<Body>, Error=GenericError> + Send>;

fn client_request_response(client: &Client<HttpConnector>) -> ResponseFuture {
let req = Request::builder()
Expand Down
9 changes: 5 additions & 4 deletions src/body/body.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::error::Error as StdError;
use std::fmt;

use bytes::Bytes;
Expand Down Expand Up @@ -39,7 +40,7 @@ enum Kind {
content_length: Option<u64>,
recv: h2::RecvStream,
},
Wrapped(Box<Stream<Item = Chunk, Error = Box<::std::error::Error + Send + Sync>> + Send>),
Wrapped(Box<dyn Stream<Item = Chunk, Error = Box<dyn StdError + Send + Sync>> + Send>),
}

struct Extra {
Expand Down Expand Up @@ -141,7 +142,7 @@ impl Body {
pub fn wrap_stream<S>(stream: S) -> Body
where
S: Stream + Send + 'static,
S::Error: Into<Box<::std::error::Error + Send + Sync>>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
Chunk: From<S::Item>,
{
let mapped = stream.map(Chunk::from).map_err(Into::into);
Expand Down Expand Up @@ -457,13 +458,13 @@ impl From<Chunk> for Body {
}

impl
From<Box<Stream<Item = Chunk, Error = Box<::std::error::Error + Send + Sync>> + Send + 'static>>
From<Box<dyn Stream<Item = Chunk, Error = Box<dyn StdError + Send + Sync>> + Send + 'static>>
for Body
{
#[inline]
fn from(
stream: Box<
Stream<Item = Chunk, Error = Box<::std::error::Error + Send + Sync>> + Send + 'static,
dyn Stream<Item = Chunk, Error = Box<dyn StdError + Send + Sync>> + Send + 'static,
>,
) -> Body {
Body::new(Kind::Wrapped(stream))
Expand Down
4 changes: 3 additions & 1 deletion src/body/payload.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::error::Error as StdError;

use bytes::Buf;
use futures::{Async, Poll};
use http::HeaderMap;
Expand All @@ -13,7 +15,7 @@ pub trait Payload: Send + 'static {
type Data: Buf + Send;

/// The error type of this stream.
type Error: Into<Box<::std::error::Error + Send + Sync>>;
type Error: Into<Box<dyn StdError + Send + Sync>>;

/// Poll for a `Data` buffer.
///
Expand Down
4 changes: 2 additions & 2 deletions src/client/conn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ pub struct Handshake<T, B> {
pub struct ResponseFuture {
// for now, a Box is used to hide away the internal `B`
// that can be returned if canceled
inner: Box<Future<Item=Response<Body>, Error=::Error> + Send>,
inner: Box<dyn Future<Item=Response<Body>, Error=::Error> + Send>,
}

/// Deconstructed parts of a `Connection`.
Expand Down Expand Up @@ -464,7 +464,7 @@ impl Builder {
/// Provide an executor to execute background HTTP2 tasks.
pub fn executor<E>(&mut self, exec: E) -> &mut Builder
where
E: Executor<Box<Future<Item=(), Error=()> + Send>> + Send + Sync + 'static,
E: Executor<Box<dyn Future<Item=(), Error=()> + Send>> + Send + Sync + 'static,
{
self.exec = Exec::Executor(Arc::new(exec));
self
Expand Down
2 changes: 1 addition & 1 deletion src/client/connect/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ impl fmt::Debug for GaiAddrs {
}

#[derive(Clone)]
struct GaiExecutor(Arc<Executor<GaiTask> + Send + Sync>);
struct GaiExecutor(Arc<dyn Executor<GaiTask> + Send + Sync>);

impl Executor<oneshot::Execute<GaiBlocking>> for GaiExecutor {
fn execute(&self, future: oneshot::Execute<GaiBlocking>) -> Result<(), ExecuteError<oneshot::Execute<GaiBlocking>>> {
Expand Down
12 changes: 6 additions & 6 deletions src/client/connect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub trait Connect: Send + Sync {
/// The connected IO Stream.
type Transport: AsyncRead + AsyncWrite + Send + 'static;
/// An error occured when trying to connect.
type Error: Into<Box<StdError + Send + Sync>>;
type Error: Into<Box<dyn StdError + Send + Sync>>;
/// A Future that will resolve to the connected Transport.
type Future: Future<Item=(Self::Transport, Connected), Error=Self::Error> + Send;
/// Connect to a destination.
Expand All @@ -53,7 +53,7 @@ pub struct Connected {
pub(super) extra: Option<Extra>,
}

pub(super) struct Extra(Box<ExtraInner>);
pub(super) struct Extra(Box<dyn ExtraInner>);

#[derive(Clone, Copy, Debug, PartialEq)]
pub(super) enum Alpn {
Expand Down Expand Up @@ -344,7 +344,7 @@ impl fmt::Debug for Extra {
}

trait ExtraInner: Send + Sync {
fn clone_box(&self) -> Box<ExtraInner>;
fn clone_box(&self) -> Box<dyn ExtraInner>;
fn set(&self, res: &mut Response<::Body>);
}

Expand All @@ -358,7 +358,7 @@ impl<T> ExtraInner for ExtraEnvelope<T>
where
T: Clone + Send + Sync + 'static
{
fn clone_box(&self) -> Box<ExtraInner> {
fn clone_box(&self) -> Box<dyn ExtraInner> {
Box::new(self.clone())
}

Expand All @@ -367,7 +367,7 @@ where
}
}

struct ExtraChain<T>(Box<ExtraInner>, T);
struct ExtraChain<T>(Box<dyn ExtraInner>, T);

impl<T: Clone> Clone for ExtraChain<T> {
fn clone(&self) -> Self {
Expand All @@ -379,7 +379,7 @@ impl<T> ExtraInner for ExtraChain<T>
where
T: Clone + Send + Sync + 'static
{
fn clone_box(&self) -> Box<ExtraInner> {
fn clone_box(&self) -> Box<dyn ExtraInner> {
Box::new(self.clone())
}

Expand Down
6 changes: 3 additions & 3 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,11 @@ impl<C, B> fmt::Debug for Client<C, B> {
/// This is returned by `Client::request` (and `Client::get`).
#[must_use = "futures do nothing unless polled"]
pub struct ResponseFuture {
inner: Box<Future<Item=Response<Body>, Error=::Error> + Send>,
inner: Box<dyn Future<Item=Response<Body>, Error=::Error> + Send>,
}

impl ResponseFuture {
fn new(fut: Box<Future<Item=Response<Body>, Error=::Error> + Send>) -> Self {
fn new(fut: Box<dyn Future<Item=Response<Body>, Error=::Error> + Send>) -> Self {
Self {
inner: fut,
}
Expand Down Expand Up @@ -1030,7 +1030,7 @@ impl Builder {
/// Provide an executor to execute background `Connection` tasks.
pub fn executor<E>(&mut self, exec: E) -> &mut Self
where
E: Executor<Box<Future<Item=(), Error=()> + Send>> + Send + Sync + 'static,
E: Executor<Box<dyn Future<Item=(), Error=()> + Send>> + Send + Sync + 'static,
{
self.conn_builder.executor(exec);
self
Expand Down
2 changes: 1 addition & 1 deletion src/common/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub trait NewSvcExec<I, N, S: Service, E, W: Watcher<I, S, E>>: Clone {
#[derive(Clone)]
pub enum Exec {
Default,
Executor(Arc<Executor<Box<Future<Item=(), Error=()> + Send>> + Send + Sync>),
Executor(Arc<dyn Executor<Box<dyn Future<Item=(), Error=()> + Send>> + Send + Sync>),
}

// ===== impl Exec =====
Expand Down
10 changes: 5 additions & 5 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use h2;
/// Result type often returned from methods that can have hyper `Error`s.
pub type Result<T> = ::std::result::Result<T, Error>;

type Cause = Box<StdError + Send + Sync>;
type Cause = Box<dyn StdError + Send + Sync>;

/// Represents errors that can occur handling HTTP streams.
pub struct Error {
Expand Down Expand Up @@ -135,12 +135,12 @@ impl Error {

#[doc(hidden)]
#[cfg_attr(error_source, deprecated(note = "use Error::source instead"))]
pub fn cause2(&self) -> Option<&(StdError + 'static + Sync + Send)> {
pub fn cause2(&self) -> Option<&(dyn StdError + 'static + Sync + Send)> {
self.inner.cause.as_ref().map(|e| &**e)
}

/// Consumes the error, returning its cause.
pub fn into_cause(self) -> Option<Box<StdError + Sync + Send>> {
pub fn into_cause(self) -> Option<Box<dyn StdError + Sync + Send>> {
self.inner.cause
}

Expand Down Expand Up @@ -380,12 +380,12 @@ impl StdError for Error {
}

#[cfg(error_source)]
fn source(&self) -> Option<&(StdError + 'static)> {
fn source(&self) -> Option<&(dyn StdError + 'static)> {
self
.inner
.cause
.as_ref()
.map(|cause| &**cause as &(StdError + 'static))
.map(|cause| &**cause as &(dyn StdError + 'static))
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ impl Drop for DuplexHandle {
}

#[cfg(feature = "runtime")]
type BoxedConnectFut = Box<Future<Item=(Duplex, Connected), Error=io::Error> + Send>;
type BoxedConnectFut = Box<dyn Future<Item=(Duplex, Connected), Error=io::Error> + Send>;

#[cfg(feature = "runtime")]
#[derive(Clone)]
Expand Down
6 changes: 3 additions & 3 deletions src/proto/h1/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ impl ChunkedState {
trace!("Read chunk hex size");
let radix = 16;
match byte!(rdr) {
b @ b'0'...b'9' => {
b @ b'0'..=b'9' => {
*size *= radix;
*size += (b - b'0') as u64;
}
b @ b'a'...b'f' => {
b @ b'a'..=b'f' => {
*size *= radix;
*size += (b + 10 - b'a') as u64;
}
b @ b'A'...b'F' => {
b @ b'A'..=b'F' => {
*size *= radix;
*size += (b + 10 - b'A') as u64;
}
Expand Down
8 changes: 5 additions & 3 deletions src/proto/h1/dispatch.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::error::Error as StdError;

use bytes::{Buf, Bytes};
use futures::{Async, Future, Poll, Stream};
use http::{Request, Response, StatusCode};
Expand Down Expand Up @@ -44,7 +46,7 @@ type ClientRx<B> = ::client::dispatch::Receiver<Request<B>, Response<Body>>;
impl<D, Bs, I, T> Dispatcher<D, Bs, I, T>
where
D: Dispatch<PollItem=MessageHead<T::Outgoing>, PollBody=Bs, RecvItem=MessageHead<T::Incoming>>,
D::PollError: Into<Box<::std::error::Error + Send + Sync>>,
D::PollError: Into<Box<dyn StdError + Send + Sync>>,
I: AsyncRead + AsyncWrite,
T: Http1Transaction,
Bs: Payload,
Expand Down Expand Up @@ -334,7 +336,7 @@ where
impl<D, Bs, I, T> Future for Dispatcher<D, Bs, I, T>
where
D: Dispatch<PollItem=MessageHead<T::Outgoing>, PollBody=Bs, RecvItem=MessageHead<T::Incoming>>,
D::PollError: Into<Box<::std::error::Error + Send + Sync>>,
D::PollError: Into<Box<dyn StdError + Send + Sync>>,
I: AsyncRead + AsyncWrite,
T: Http1Transaction,
Bs: Payload,
Expand Down Expand Up @@ -365,7 +367,7 @@ impl<S> Server<S> where S: Service {
impl<S, Bs> Dispatch for Server<S>
where
S: Service<ReqBody=Body, ResBody=Bs>,
S::Error: Into<Box<::std::error::Error + Send + Sync>>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
Bs: Payload,
{
type PollItem = MessageHead<StatusCode>;
Expand Down
4 changes: 2 additions & 2 deletions src/proto/h1/role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,7 +717,7 @@ impl Client {
101 => {
return Ok(Some((DecodedLength::ZERO, true)));
},
100...199 => {
100..=199 => {
trace!("ignoring informational response: {}", inc.subject.as_u16());
return Ok(None);
},
Expand All @@ -729,7 +729,7 @@ impl Client {
Some(Method::HEAD) => {
return Ok(Some((DecodedLength::ZERO, false)));
}
Some(Method::CONNECT) => if let 200...299 = inc.subject.as_u16() {
Some(Method::CONNECT) => if let 200..=299 = inc.subject.as_u16() {
return Ok(Some((DecodedLength::ZERO, true)));
}
Some(_) => {},
Expand Down
12 changes: 7 additions & 5 deletions src/proto/h2/server.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::error::Error as StdError;

use futures::{Async, Future, Poll, Stream};
use h2::Reason;
use h2::server::{Builder, Connection, Handshake, SendResponse};
Expand Down Expand Up @@ -46,7 +48,7 @@ impl<T, S, B, E> Server<T, S, B, E>
where
T: AsyncRead + AsyncWrite,
S: Service<ReqBody=Body, ResBody=B>,
S::Error: Into<Box<::std::error::Error + Send + Sync>>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: Payload,
E: H2Exec<S::Future, B>,
{
Expand Down Expand Up @@ -83,7 +85,7 @@ impl<T, S, B, E> Future for Server<T, S, B, E>
where
T: AsyncRead + AsyncWrite,
S: Service<ReqBody=Body, ResBody=B>,
S::Error: Into<Box<::std::error::Error + Send + Sync>>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
B: Payload,
E: H2Exec<S::Future, B>,
{
Expand Down Expand Up @@ -126,7 +128,7 @@ where
ReqBody=Body,
ResBody=B,
>,
S::Error: Into<Box<::std::error::Error + Send + Sync>>,
S::Error: Into<Box<dyn StdError + Send + Sync>>,
E: H2Exec<S::Future, B>,
{
if self.closing.is_none() {
Expand Down Expand Up @@ -203,7 +205,7 @@ where
impl<F, B> H2Stream<F, B>
where
F: Future<Item=Response<B>>,
F::Error: Into<Box<::std::error::Error + Send + Sync>>,
F::Error: Into<Box<dyn StdError + Send + Sync>>,
B: Payload,
{
fn new(fut: F, respond: SendResponse<SendBuf<B::Data>>) -> H2Stream<F, B> {
Expand Down Expand Up @@ -296,7 +298,7 @@ where
impl<F, B> Future for H2Stream<F, B>
where
F: Future<Item=Response<B>>,
F::Error: Into<Box<::std::error::Error + Send + Sync>>,
F::Error: Into<Box<dyn StdError + Send + Sync>>,
B: Payload,
{
type Item = ();
Expand Down
Loading

0 comments on commit 01c03db

Please sign in to comment.