Skip to content
This repository has been archived by the owner on Jun 21, 2020. It is now read-only.

Update for futures v0.3.0-alpha.15 #23

Merged
merged 1 commit into from
Apr 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: rust
rust:
- nightly-2019-04-16
- nightly-2019-04-25

before_script: |
rustup component add rustfmt clippy
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ http = "0.1.17"
http-service-hyper = { path = "http-service-hyper", version = "0.1.1" }

[dependencies.futures-preview]
version = "0.3.0-alpha.14"
version = "0.3.0-alpha.15"
2 changes: 1 addition & 1 deletion examples/simple_response.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![feature(futures_api, async_await, await_macro, existential_type)]
#![feature(async_await, await_macro, existential_type)]

use futures::future::{self, FutureObj};
use http_service::{HttpService, Response};
Expand Down
2 changes: 1 addition & 1 deletion http-service-hyper/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ hyper = "0.12.27"

[dependencies.futures-preview]
features = ["compat"]
version = "0.3.0-alpha.14"
version = "0.3.0-alpha.15"
22 changes: 12 additions & 10 deletions http-service-hyper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs, missing_doc_code_examples)]
#![cfg_attr(test, deny(warnings))]
#![feature(futures_api, async_await, await_macro)]
#![feature(async_await, await_macro)]

use futures::{
compat::{Compat, Compat01As03, Future01CompatExt},
future::FutureObj,
future::BoxFuture,
prelude::*,
};
use http_service::{Body, HttpService};
Expand All @@ -33,20 +33,21 @@ where
type ResBody = hyper::Body;
type Error = std::io::Error;
type Service = WrapConnection<H>;
type Future = Compat<FutureObj<'static, Result<Self::Service, Self::Error>>>;
type Future = Compat<BoxFuture<'static, Result<Self::Service, Self::Error>>>;
type MakeError = std::io::Error;

fn make_service(&mut self, _ctx: Ctx) -> Self::Future {
let service = self.service.clone();
let error = std::io::Error::from(std::io::ErrorKind::Other);
FutureObj::new(Box::new(async move {
async move {
let connection = await!(service.connect().into_future()).map_err(|_| error)?;
Ok(WrapConnection {
service,
connection,
})
}))
.compat()
}
.boxed()
.compat()
}
}

Expand All @@ -57,7 +58,7 @@ where
type ReqBody = hyper::Body;
type ResBody = hyper::Body;
type Error = std::io::Error;
type Future = Compat<FutureObj<'static, Result<http::Response<hyper::Body>, Self::Error>>>;
type Future = Compat<BoxFuture<'static, Result<http::Response<hyper::Body>, Self::Error>>>;

fn call(&mut self, req: http::Request<hyper::Body>) -> Self::Future {
let error = std::io::Error::from(std::io::ErrorKind::Other);
Expand All @@ -70,11 +71,12 @@ where
});
let fut = self.service.respond(&mut self.connection, req);

FutureObj::new(Box::new(async move {
async move {
let res: http::Response<_> = await!(fut.into_future()).map_err(|_| error)?;
Ok(res.map(|body| hyper::Body::wrap_stream(body.compat())))
}))
.compat()
}
.boxed()
.compat()
}
}

Expand Down
4 changes: 2 additions & 2 deletions http-service-mock/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repository = "https://github.com/rust-net-web/http-service"
version = "0.1.1"

[dependencies]
http-service = "0.1.5"
http-service = { version = "0.1.5", path = ".." }

[dependencies.futures-preview]
version = "0.3.0-alpha.14"
version = "0.3.0-alpha.15"
2 changes: 1 addition & 1 deletion http-service-mock/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs, missing_doc_code_examples)]
#![cfg_attr(test, deny(warnings))]
#![feature(futures_api, async_await)]
#![feature(async_await)]

use futures::{executor::block_on, prelude::*};
use http_service::{HttpService, Request, Response};
Expand Down
21 changes: 13 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@
#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs, missing_doc_code_examples)]
#![cfg_attr(test, deny(warnings))]
#![feature(futures_api, async_await, await_macro, arbitrary_self_types)]
#![feature(async_await, await_macro, arbitrary_self_types)]

use bytes::Bytes;
use futures::{
future,
prelude::*,
stream::{self, StreamObj},
stream::{self, BoxStream},
task::Context,
Poll,
};

use std::fmt;
use std::marker::Unpin;
use std::pin::Pin;

Expand All @@ -76,9 +77,8 @@ use std::pin::Pin;
/// A body is a stream of `Bytes` values, which are shared handles to byte buffers.
/// Both `Body` and `Bytes` values can be easily created from standard owned byte buffer types
/// like `Vec<u8>` or `String`, using the `From` trait.
#[derive(Debug)]
pub struct Body {
stream: StreamObj<'static, Result<Bytes, std::io::Error>>,
stream: BoxStream<'static, Result<Bytes, std::io::Error>>,
}

impl Body {
Expand All @@ -92,12 +92,11 @@ impl Body {
where
S: Stream<Item = Result<Bytes, std::io::Error>> + Send + 'static,
{
Self {
stream: StreamObj::new(Box::new(s)),
}
Self { stream: s.boxed() }
}

/// Reads the stream into a new `Vec`.
#[allow(clippy::wrong_self_convention)] // https://github.com/rust-lang/rust-clippy/issues/4037
pub async fn into_vec(mut self) -> std::io::Result<Vec<u8>> {
let mut bytes = Vec::new();
while let Some(chunk) = await!(self.next()) {
Expand All @@ -118,7 +117,13 @@ impl Unpin for Body {}
impl Stream for Body {
type Item = Result<Bytes, std::io::Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.stream).poll_next(cx)
self.stream.poll_next_unpin(cx)
}
}

impl fmt::Debug for Body {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Body").finish()
}
}

Expand Down