From 01aaf12c54de47e7637a7a51a125e59c0e48b6af Mon Sep 17 00:00:00 2001 From: dbcfd Date: Wed, 23 Oct 2019 15:00:35 -0600 Subject: [PATCH 1/2] Hyper 0.13 Update to hyper 0.13 and std::future::Future. --- Cargo.toml | 19 ++--- src/add_context.rs | 61 ++++++++------- src/auth.rs | 92 +++++++++++----------- src/base64_format.rs | 2 +- src/client.rs | 34 ++++----- src/composites.rs | 49 ++++++------ src/connector.rs | 2 +- src/context.rs | 168 +++++++++++++++++++++++++---------------- src/drop_context.rs | 59 ++++++++------- src/lib.rs | 18 ----- src/nullable_format.rs | 1 + 11 files changed, 270 insertions(+), 235 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d845ee5c6c..e64307639b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ homepage = "https://github.com/Metaswitch/swagger-rs" repository = "https://github.com/Metaswitch/swagger-rs" readme = "README.md" keywords = ["swagger"] +edition = "2018" [badges.travis-ci] repository = "Metaswitch/swagger-rs" @@ -15,21 +16,21 @@ repository = "Metaswitch/swagger-rs" [features] default = ["serdejson"] multipart = ["mime"] -serdejson = ["serde", "serde_json", "serde_derive"] +serdejson = ["serde", "serde_json"] [dependencies] +async-trait = "0.1" base64 = "0.10" mime = { version = "0.3", optional = true } -serde = { version = "1.0", optional = true } +serde = { version = "1.0", optional = true, features = ["derive"] } serde_json = { version = "1.0", optional = true } -serde_derive = { version = "1.0", optional = true } -hyper = "0.12.25" -hyper-tls = "0.2.1" -native-tls = "0.1.4" -openssl = "0.9.14" +hyper = "0.13.0-alpha.4" +hyper-tls = "0.4.0-alpha.4" +hyper-old-types = "0.11.0" +native-tls = "0.2" +openssl = "0.10" slog = { version = "2", features = [ "max_level_trace", "release_max_level_debug"] } -futures = "0.1" +futures-preview = "0.3.0-alpha.19" uuid = {version = "0.7", features = ["serde", "v4"]} -hyper-old-types = "0.11.0" chrono = "0.4.6" diff --git a/src/add_context.rs b/src/add_context.rs index c98ba51185..7274a0a310 100644 --- a/src/add_context.rs +++ b/src/add_context.rs @@ -1,14 +1,15 @@ //! Hyper service that adds a context to an incoming request and passes it on //! to a wrapped service. -use super::{Push, XSpanIdString}; -use context::ContextualPayload; -use futures::Future; +use crate::{ErrorBound, Push, XSpanIdString}; +use crate::context::ContextualPayload; +use futures::FutureExt; use hyper; use hyper::Request; +use std::future::Future; use std::marker::PhantomData; - -use ErrorBound; +use std::pin::Pin; +use std::task::{Context, Poll}; /// Middleware wrapper service, that should be used as the outermost layer in a /// stack of hyper services. Adds a context to a plain `hyper::Request` that can be @@ -37,43 +38,38 @@ where } } -impl<'a, T, SC, RC, E, ME, S, F> hyper::service::MakeService<&'a SC> +impl<'a, T, SC, RC, E, ME, S> hyper::service::Service<&'a SC> for AddContextMakeService where RC: Default + Push + 'static + Send, RC::Result: Send + 'static, - T: hyper::service::MakeService< + T: hyper::service::Service< &'a SC, - Service = S, - ReqBody = ContextualPayload, - ResBody = hyper::Body, - Error = E, - MakeError = ME, - Future = F, + ResBody = S, + Error = ME, + Future = Pin>>>, >, S: hyper::service::Service< - ReqBody = ContextualPayload, + ContextualPayload, ResBody = hyper::Body, Error = E, > + 'static, ME: ErrorBound, E: ErrorBound, - F: Future + Send + 'static, S::Future: Send, { - type ReqBody = hyper::Body; - type ResBody = hyper::Body; - type Error = E; - type Service = AddContextService; - type MakeError = ME; - type Future = Box + Send>; + type ResBody = T; + type Error = ME; + type Future = Pin, Self::Error>> + Send>>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } - fn make_service(&mut self, service_ctx: &'a SC) -> Self::Future { - Box::new( - self.inner - .make_service(service_ctx) - .map(AddContextService::new), - ) + fn call(&mut self, service_ctx: &'a SC) -> Self::Future { + self.inner + .call(service_ctx) + .map(AddContextService::new) } } @@ -106,23 +102,26 @@ where } } -impl hyper::service::Service for AddContextService +impl hyper::service::Service> for AddContextService where C: Default + Push, C::Result: Send + 'static, T: hyper::service::Service< - ReqBody = ContextualPayload, + ContextualPayload, ResBody = hyper::Body, Error = E, >, E: ErrorBound, { - type ReqBody = hyper::Body; type ResBody = hyper::Body; type Error = E; type Future = T::Future; - fn call(&mut self, req: Request) -> Self::Future { + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, req: Request>) -> Self::Future { let x_span_id = XSpanIdString::get_or_generate(&req); let (head, body) = req.into_parts(); let context = C::default().push(x_span_id); diff --git a/src/auth.rs b/src/auth.rs index ca5922f0f9..288b3b187c 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -2,7 +2,7 @@ use crate::context::ContextualPayload; use crate::{ErrorBound, Push}; -use futures::future::Future; +use futures::FutureExt; use hyper; use hyper::body::Payload; use hyper::header::AUTHORIZATION; @@ -13,8 +13,11 @@ use hyper_old_types::header::Header as HeaderTrait; pub use hyper_old_types::header::{Basic, Bearer}; use hyper_old_types::header::{Raw, Scheme}; use std::collections::BTreeSet; +use std::future::Future; use std::marker::PhantomData; +use std::pin::Pin; use std::string::ToString; +use std::task::{Context, Poll}; /// Authorization scopes. #[derive(Clone, Debug, PartialEq)] @@ -117,41 +120,37 @@ where } } -impl<'a, T, SC, RC, E, ME, S, OB, F> MakeService<&'a SC> for MakeAllowAllAuthenticator +impl<'a, T, SC, RC, E, ME, S, OB> Service<&'a SC> for MakeAllowAllAuthenticator where RC: RcBound, RC::Result: Send + 'static, - T: MakeService< + T: Service< &'a SC, - Error = E, - MakeError = ME, - Service = S, - ReqBody = ContextualPayload, - ResBody = OB, - Future = F, + Error = ME, + ResBody = S, + Future = Pin>>>, >, - S: Service, ResBody = OB> + S: Service, Error = E, ResBody = OB> + 'static, ME: ErrorBound, E: ErrorBound, - F: Future + Send + 'static, S::Future: Send, OB: Payload, { - type ReqBody = ContextualPayload; - type ResBody = OB; - type Error = E; - type MakeError = ME; - type Service = AllowAllAuthenticator; - type Future = Box + Send>; - - fn make_service(&mut self, service_ctx: &'a SC) -> Self::Future { + type ResBody = AllowAllAuthenticator; + type Error = ME; + type Future = Pin, ME>> + Send>>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, service_ctx: &'a SC) -> Self::Future { let subject = self.subject.clone(); - Box::new( - self.inner - .make_service(service_ctx) - .map(|s| AllowAllAuthenticator::new(s, subject)), - ) + self.inner + .call(service_ctx) + .map(|s| AllowAllAuthenticator::new(s, subject)) + .boxed() } } @@ -175,19 +174,22 @@ impl AllowAllAuthenticator { } } -impl hyper::service::Service for AllowAllAuthenticator +impl hyper::service::Service> for AllowAllAuthenticator where RC: RcBound, RC::Result: Send + 'static, - T: Service>, - T::Future: Future, Error = T::Error> + Send + 'static, + T: Service>, + T::Future: Future, T::Error>> + Send + 'static, { - type ReqBody = ContextualPayload; type ResBody = T::ResBody; type Error = T::Error; - type Future = Box, Error = T::Error> + Send>; + type Future = Pin, T::Error>> + Send>>; - fn call(&mut self, req: Request) -> Self::Future { + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, req: Request>) -> Self::Future { let (head, body) = req.into_parts(); let body = ContextualPayload { inner: body.inner, @@ -198,7 +200,7 @@ where })), }; - Box::new(self.inner.call(Request::from_parts(head, body))) + self.inner.call(Request::from_parts(head, body)) } } @@ -227,7 +229,6 @@ pub fn api_key_from_header(headers: &HeaderMap, header: &str) -> Option mod tests { use super::*; use crate::EmptyContext; - use futures::future; use hyper::server::conn::AddrStream; use hyper::service::{make_service_fn, MakeService, MakeServiceRef, Service}; use hyper::{body::Payload, Body, Response}; @@ -238,17 +239,17 @@ mod tests { where T: MakeService< &'a SC, + IB, Error = E, MakeError = ME, Service = S, Future = F, - ReqBody = IB, ResBody = OB, >, E: ErrorBound, ME: ErrorBound, - S: Service, - F: Future, + S: Service, + F: Future>, IB: Payload, OB: Payload, { @@ -257,7 +258,7 @@ mod tests { fn check_type(_: &S) where - S: MakeServiceRef, + S: MakeServiceRef, S::Error: ErrorBound, S::Service: 'static, B: Payload, @@ -268,23 +269,26 @@ mod tests { struct TestService(std::net::SocketAddr, PhantomData); - impl Service for TestService { - type ReqBody = IB; + impl Service for TestService { type ResBody = Body; type Error = std::io::Error; - type Future = future::FutureResult, Self::Error>; + type Future = Pin, Self::Error>>>>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } - fn call(&mut self, req: Request) -> Self::Future { - future::ok(Response::new(Body::from(format!("{:?} {}", req, self.0)))) + fn call(&mut self, req: Request) -> Self::Future { + futures::future::ok( + Response::new(Body::from(format!("{:?} {}", req, self.0))) + ).boxed() } } #[test] fn test_make_service() { let make_svc = make_service_fn(|socket: &AddrStream| { - let f: future::FutureResult, io::Error> = - future::ok(TestService(socket.remote_addr(), PhantomData)); - f + futures::future::ok(Ok(TestService(socket.remote_addr(), PhantomData))); }); check_inner_type(&make_svc); diff --git a/src/base64_format.rs b/src/base64_format.rs index b00f1e875a..df4ee4e6ef 100644 --- a/src/base64_format.rs +++ b/src/base64_format.rs @@ -29,7 +29,7 @@ impl<'de> Deserialize<'de> for ByteArray { where D: Deserializer<'de>, { - let s = try!(String::deserialize(deserializer)); + let s = String::deserialize(deserializer)?; match decode(&s) { Ok(bin) => Ok(ByteArray(bin)), _ => Err(D::Error::custom("invalid base64")), diff --git a/src/client.rs b/src/client.rs index f8982eaa25..010764d538 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,44 +1,44 @@ +use async_trait::async_trait; +use crate::ContextualPayload; + +pub type HyperResult = Result, hyper::Error>; + /// Common trait for swagger based client middleware -pub trait Service { +#[async_trait] +pub trait Service { /// Request body taken by client. /// Likely either `hyper::Body`, `hyper::Chunk` or `swagger::ContextualPayload`. type ReqBody: hyper::body::Payload; - /// Future response from client service. - /// Likely: `Future, Error=hyper::Error>` - type Future: futures::Future; - /// Handle the given request - fn request(&self, req: hyper::Request) -> Self::Future; + async fn request(&self, req: hyper::Request) -> T; } -impl Service for hyper::Client +#[async_trait] +impl Service for hyper::Client where - B: hyper::body::Payload + Send + 'static, - B::Data: Send, + B: hyper::body::Payload + Unpin + Send + 'static, + B::Data: Send + Unpin, C: hyper::client::connect::Connect + Sync + 'static, C::Transport: 'static, C::Future: 'static, { type ReqBody = B; - type Future = hyper::client::ResponseFuture; - fn request(&self, req: hyper::Request) -> Self::Future { - hyper::Client::request(self, req) + async fn request(&self, req: hyper::Request) -> HyperResult { + hyper::Client::request(self, req).await } } /// Factory trait for creating Services - swagger based client middleware +#[async_trait] pub trait MakeService { /// Service that this creates - type Service: Service; + type Service: Service>; /// Potential error from creating the service. type Error; - /// Future response creating the service. - type Future: futures::Future; - /// Handle the given request - fn make_service(&self, ctx: Context) -> Self::Future; + async fn make_service(&self, ctx: Context) -> Result; } diff --git a/src/composites.rs b/src/composites.rs index 40cb936487..bc727a4671 100644 --- a/src/composites.rs +++ b/src/composites.rs @@ -2,10 +2,13 @@ //! //! Use by passing `hyper::server::MakeService` instances to a `CompositeMakeService` //! together with the base path for requests that should be handled by that service. -use futures::{future, Future}; +use futures::FutureExt; use hyper::service::{MakeService, Service}; use hyper::{Request, Response, StatusCode}; +use std::future::Future; use std::ops::{Deref, DerefMut}; +use std::pin::Pin; +use std::task::{Context, Poll}; use std::{fmt, io}; /// Trait for generating a default "not found" response. Must be implemented on @@ -25,11 +28,11 @@ impl NotFound for hyper::Body { } } -type BoxedFuture = Box>; +type BoxedFuture = Pin>>>; type CompositeMakeServiceVec = Vec<(&'static str, Box>)>; type BoxedService = - Box, W>>>; + Box, W>>>; /// Trait for wrapping hyper `MakeService`s to make the return type of `make_service` uniform. /// This is necessary in order for the `MakeService`s with different `Instance` types to @@ -41,14 +44,14 @@ pub trait BoxedMakeService { impl<'a, SC, T, Rq, Rs, Er, S> BoxedMakeService<&'a SC, Rq, Rs, Er> for T where - S: Service, Er>> + S: Service, Er>> + 'static, T: MakeService< &'a SC, - ReqBody = Rq, + Rq, ResBody = Rs, Error = Er, - Future = futures::future::FutureResult, + Future = Pin>>>, Service = S, MakeError = io::Error, >, @@ -109,23 +112,24 @@ impl, W> CompositeMakeService { } } -impl<'a, C, U, V, W> MakeService<&'a C> for CompositeMakeService<&'a C, U, V, W> +impl<'a, C, U, V, W> Service<&'a C> for CompositeMakeService<&'a C, U, V, W> where U: hyper::body::Payload, V: NotFound + 'static + hyper::body::Payload, W: std::error::Error + Send + Sync + 'static, { - type ReqBody = U; - type ResBody = V; - type Error = W; - type Service = CompositeService; - type MakeError = io::Error; - type Future = futures::future::FutureResult; + type ResBody = CompositeService; + type Error = io::Error; + type Future = Pin, Self::Error>>>>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } - fn make_service( + fn call( &mut self, _service_ctx: &'a C, - ) -> futures::future::FutureResult { + ) -> Self::Future { let mut vec = Vec::new(); for &mut (base_path, ref mut make_service) in &mut self.0 { @@ -137,7 +141,7 @@ where )) } - future::FutureResult::from(Ok(CompositeService(vec))) + futures::future::ok(Ok(CompositeService(vec))).boxed() } } @@ -185,18 +189,21 @@ where V: NotFound + 'static, W: 'static; -impl Service for CompositeService +impl Service for CompositeService where U: hyper::body::Payload, V: NotFound + 'static + hyper::body::Payload, W: 'static + std::error::Error + Send + Sync, { - type ReqBody = U; type ResBody = V; type Error = W; - type Future = Box, Error = W>>; + type Future = Pin, W>>>>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } - fn call(&mut self, req: Request) -> Self::Future { + fn call(&mut self, req: Request) -> Self::Future { let mut result = None; for &mut (base_path, ref mut service) in &mut self.0 { @@ -206,7 +213,7 @@ where } } - result.unwrap_or_else(|| Box::new(future::ok(V::not_found()))) + result.unwrap_or_else(|| futures::future::ok(Ok(V::not_found()))).boxed() } } diff --git a/src/connector.rs b/src/connector.rs index 8f302817a4..a2f0559888 100644 --- a/src/connector.rs +++ b/src/connector.rs @@ -12,7 +12,7 @@ use hyper; /// Returns a function which creates an http-connector. Used for instantiating /// clients with custom connectors pub fn http_connector() -> Box hyper::client::HttpConnector + Send + Sync> { - Box::new(move || hyper::client::HttpConnector::new(4)) + Box::new(move || hyper::client::HttpConnector::new()) } /// Returns a function which creates an https-connector diff --git a/src/context.rs b/src/context.rs index f3dc24afcb..40805afd9d 100644 --- a/src/context.rs +++ b/src/context.rs @@ -7,23 +7,23 @@ //! See the `context_tests` module below for examples of how to use. use super::XSpanIdString; -use auth::{AuthData, Authorization}; -use futures::future::Future; +use crate::auth::{AuthData, Authorization}; use hyper; +use std::future::Future; use std::marker::Sized; +use std::pin::Pin; +use std::task::{Context, Poll}; /// Defines methods for accessing, modifying, adding and removing the data stored /// in a context. Used to specify the requirements that a hyper service makes on /// a generic context type that it receives with a request, e.g. /// /// ```rust -/// # extern crate hyper; -/// # extern crate swagger; -/// # extern crate futures; -/// # /// # use swagger::context::*; -/// # use futures::future::{Future, ok}; +/// # use std::future::Future; /// # use std::marker::PhantomData; +/// # use std::pin::Pin; +/// # use std::task::{Context, Poll}; /// # /// # struct MyItem; /// # fn do_something_with_my_item(item: &MyItem) {} @@ -35,14 +35,18 @@ use std::marker::Sized; /// impl hyper::service::Service for MyService /// where C: Has + Send + 'static /// { -/// type ReqBody = ContextualPayload; /// type ResBody = hyper::Body; /// type Error = std::io::Error; /// type Future = Box, Error=Self::Error>>; +/// +/// fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { +/// Poll::Ready(Ok(())) +/// } +/// /// fn call(&mut self, req : hyper::Request) -> Self::Future { /// let (head, body) = req.into_parts(); /// do_something_with_my_item(Has::::get(&body.context)); -/// Box::new(ok(hyper::Response::new(hyper::Body::empty()))) +/// futures::future::ok(hyper::Response::new(hyper::Body::empty()))) /// } /// } /// @@ -62,13 +66,12 @@ pub trait Has { /// making it unavailable to later layers, e.g. /// /// ```rust -/// # extern crate hyper; -/// # extern crate swagger; -/// # extern crate futures; /// # /// # use swagger::context::*; -/// # use futures::future::{Future, ok}; +/// # use std::future::Future; /// # use std::marker::PhantomData; +/// # use std::pin::Pin; +/// # use std::task::{Context, Poll}; /// # /// struct MyItem1; /// struct MyItem2; @@ -87,10 +90,14 @@ pub trait Has { /// E::Result: Send + 'static, /// T: hyper::service::Service> /// { -/// type ReqBody = ContextualPayload; /// type ResBody = T::ResBody; /// type Error = T::Error; /// type Future = T::Future; +/// +/// fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { +/// Poll::Ready(Ok(())) +/// } +/// /// fn call(&mut self, req : hyper::Request) -> Self::Future { /// let (head, body) = req.into_parts(); /// let context = body.context; @@ -118,13 +125,12 @@ pub trait Pop { /// making it available to later layers, e.g. /// /// ```rust -/// # extern crate hyper; -/// # extern crate swagger; -/// # extern crate futures; /// # /// # use swagger::context::*; -/// # use futures::future::{Future, ok}; +/// # use std::future::Future; /// # use std::marker::PhantomData; +/// # use std::pin::Pin; +/// # use std::task::{Context, Poll}; /// # /// struct MyItem1; /// struct MyItem2; @@ -143,10 +149,14 @@ pub trait Pop { /// E::Result: Send + 'static, /// T: hyper::service::Service> /// { -/// type ReqBody = ContextualPayload; /// type ResBody = T::ResBody; /// type Error = T::Error; /// type Future = T::Future; +/// +/// fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { +/// Poll::Ready(Ok(())) +/// } +/// /// fn call(&mut self, req : hyper::Request) -> Self::Future { /// let (head, body) = req.into_parts(); /// let context = body.context @@ -163,7 +173,7 @@ pub trait Push { /// The type that results from adding an item. type Result; /// Inserts a value. - fn push(self, T) -> Self::Result; + fn push(self, v: T) -> Self::Result; } /// Defines a struct that can be used to build up contexts recursively by @@ -518,10 +528,11 @@ where /// Trait designed to ensure consistency in context used by swagger middlewares /// /// ```rust -/// # extern crate hyper; -/// # extern crate swagger; /// # use swagger::context::*; +/// # use std::future::Future; /// # use std::marker::PhantomData; +/// # use std::pin::Pin; +/// # use std::task::{Context, Poll}; /// # use swagger::auth::{AuthData, Authorization}; /// # use swagger::XSpanIdString; /// @@ -540,10 +551,14 @@ where /// Send + /// 'static, /// { -/// type ReqBody = ContextualPayload; /// type ResBody = T::ResBody; /// type Error = T::Error; /// type Future = T::Future; +/// +/// fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { +/// Poll::Ready(Ok(())) +/// } +/// /// fn call(&mut self, req: hyper::Request) -> Self::Future { /// self.inner.call(req) /// } @@ -552,10 +567,10 @@ where pub trait SwaggerService: Clone + hyper::service::Service< - ReqBody = ContextualPayload, + ContextualPayload, ResBody = hyper::Body, Error = hyper::Error, - Future = Box, Error = hyper::Error> + Send>, + Future = Pin, hyper::Error>> + Send>>, > where C: Has> @@ -571,12 +586,12 @@ impl SwaggerService for T where T: Clone + hyper::service::Service< - ReqBody = ContextualPayload, + ContextualPayload, ResBody = hyper::Body, Error = hyper::Error, - Future = Box< - dyn Future, Error = hyper::Error> + Send, - >, + Future = Pin, hyper::Error>> + Send, + >>, >, C: Has> + Has> @@ -608,7 +623,7 @@ where type Data = P::Data; type Error = P::Error; - fn poll_data(&mut self) -> futures::Poll, Self::Error> { + fn poll_data(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll>> { self.inner.poll_data() } } @@ -616,12 +631,14 @@ where #[cfg(test)] mod context_tests { use super::*; - use futures::future::{ok, Future, FutureResult}; + use futures::FutureExt; use hyper::service::{MakeService, Service}; use hyper::{Body, Error, Method, Request, Response, Uri}; use std::io; + use std::future::Future; use std::marker::PhantomData; use std::str::FromStr; + use std::task::{Context, Poll}; struct ContextItem1; struct ContextItem2; @@ -643,15 +660,19 @@ mod context_tests { // Use trait bounds to indicate what your service will use from the context. // use `Pop` if you want to take ownership of a value stored in the context, // or `Has` if a reference is enough. - impl Service for InnerService + impl Service> for InnerService where C: Has + Pop + Send + 'static, { - type ReqBody = ContextualPayload; type ResBody = Body; type Error = Error; - type Future = Box, Error = Error>>; - fn call(&mut self, req: Request) -> Self::Future { + type Future = Pin, Error>>>>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, req: Request) -> Self::Future { use_item_2(Has::::get(&req.body().context)); let (_, body) = req.into_parts(); @@ -659,7 +680,7 @@ mod context_tests { let (item3, _): (ContextItem3, _) = body.context.pop(); use_item_3_owned(item3); - Box::new(ok(Response::new(Body::empty()))) + futures::future::ok(Response::new(Body::empty())) } } @@ -681,19 +702,22 @@ mod context_tests { } } - impl MakeService for InnerMakeService + impl MakeService for InnerMakeService where RC: Has + Pop + Send + 'static, { - type ReqBody = ContextualPayload; type ResBody = Body; type Error = Error; type Service = InnerService; - type Future = FutureResult; + type Future = Pin>>>; type MakeError = io::Error; - fn make_service(&mut self, _: SC) -> FutureResult { - ok(InnerService { + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn make_service(&mut self, _: SC) -> Self::Future { + futures::future::ok(InnerService { marker: PhantomData, }) } @@ -709,7 +733,7 @@ mod context_tests { >::Result: Push, <>::Result as Push>::Result: Send + 'static, T: Service< - ReqBody = ContextualPayload< + ContextualPayload< Body, <>::Result as Push>::Result, >, @@ -721,19 +745,23 @@ mod context_tests { // Use trait bounds to indicate what modifications your service will make // to the context, chaining them as below. - impl Service for MiddleService + impl Service> for MiddleService where C: Pop + Send + 'static, D: Push, E: Push, - T: Service>, + T: Service>, E::Result: Send + 'static, { - type ReqBody = ContextualPayload; type ResBody = T::ResBody; type Error = T::Error; type Future = T::Future; - fn call(&mut self, req: Request) -> Self::Future { + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, req: Request>) -> Self::Future { let (head, body) = req.into_parts(); let (item, context) = body.context.pop(); use_item_1_owned(item); @@ -757,7 +785,7 @@ mod context_tests { <>::Result as Push>::Result: Send + 'static, T: MakeService< SC, - ReqBody = ContextualPayload< + ContextualPayload< Body, <>::Result as Push>::Result, >, @@ -768,27 +796,30 @@ mod context_tests { marker2: PhantomData, } - impl MakeService for MiddleMakeService + impl MakeService> for MiddleMakeService where RC: Pop + Send + 'static, D: Push, E: Push, - T: MakeService>, + T: MakeService>, T::Future: 'static, E::Result: Send + 'static, { - type ReqBody = ContextualPayload; type ResBody = T::ResBody; type Error = T::Error; type Service = MiddleService; - type Future = Box>; + type Future = Pin>>>; type MakeError = T::MakeError; + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + fn make_service(&mut self, sc: SC) -> Self::Future { - Box::new(self.inner.make_service(sc).map(|s| MiddleService { + self.inner.make_service(sc).map(|s| MiddleService { inner: s, marker1: PhantomData, - })) + }) } } @@ -797,7 +828,7 @@ mod context_tests { RC: Pop, D: Push, E: Push, - T: MakeService>, + T: MakeService>, E::Result: Send + 'static, { fn new(inner: T) -> Self { @@ -814,7 +845,7 @@ mod context_tests { struct OuterService where C: Default + Push, - T: Service>, + T: Service>, C::Result: Send + 'static, { inner: T, @@ -824,17 +855,21 @@ mod context_tests { // Use a `Default` trait bound so that the context can be created. Use // `Push` trait bounds for each type that you will add to the newly // created context. - impl Service for OuterService + impl Service> for OuterService where C: Default + Push, - T: Service>, + T: Service>, C::Result: Send + 'static, { - type ReqBody = Body; type ResBody = T::ResBody; type Error = T::Error; type Future = T::Future; - fn call(&mut self, req: Request) -> Self::Future { + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, req: Request>) -> Self::Future { let context = C::default().push(ContextItem1 {}); let (header, body) = req.into_parts(); let req = Request::from_parts( @@ -851,7 +886,7 @@ mod context_tests { struct OuterMakeService where RC: Default + Push, - T: MakeService>, + T: MakeService>, RC::Result: Send + 'static, { inner: T, @@ -859,20 +894,23 @@ mod context_tests { marker2: PhantomData, } - impl MakeService for OuterMakeService + impl MakeService> for OuterMakeService where RC: Default + Push, RC::Result: Send + 'static, - T: MakeService>, + T: MakeService>, T::Future: 'static, { - type ReqBody = Body; type ResBody = T::ResBody; type Error = T::Error; type Service = OuterService; - type Future = Box>; + type Future = Pin>>>; type MakeError = T::MakeError; + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + fn make_service(&mut self, sc: SC) -> Self::Future { Box::new(self.inner.make_service(sc).map(|s| OuterService { inner: s, @@ -885,7 +923,7 @@ mod context_tests { where RC: Default + Push, RC::Result: Send + 'static, - T: MakeService>, + T: MakeService>, { fn new(inner: T) -> Self { OuterMakeService { diff --git a/src/drop_context.rs b/src/drop_context.rs index 44068c032e..49b1998ada 100644 --- a/src/drop_context.rs +++ b/src/drop_context.rs @@ -1,12 +1,15 @@ //! Hyper service that drops a context to an incoming request and passes it on //! to a wrapped service. -use context::ContextualPayload; -use futures::Future; +use crate::context::ContextualPayload; +use futures::FutureExt; use hyper; use hyper::{Error, Request}; +use std::future::Future; use std::io; use std::marker::PhantomData; +use std::pin::Pin; +use std::task::{Context, Poll}; /// Middleware wrapper service that drops the context from the incoming request /// and passes the plain `hyper::Request` to the wrapped service. @@ -53,36 +56,33 @@ where } } -impl<'a, SC, RC, T, S, F> hyper::service::MakeService<&'a SC> for DropContextMakeService +impl<'a, SC, RC, T, S, F> hyper::service::Service<&'a SC> for DropContextMakeService where RC: Send + 'static, - T: hyper::service::MakeService< + T: hyper::service::Service< &'a SC, - ReqBody = hyper::Body, - ResBody = hyper::Body, - Error = Error, - MakeError = io::Error, - Service = S, + ResBody = S, + Error = io::Error, Future = F, >, T::Future: 'static, - S: hyper::service::Service + S: hyper::service::Service + 'static, - F: Future, + F: Future>, { - type ReqBody = ContextualPayload; - type ResBody = hyper::Body; + type ResBody = DropContextService; type Error = Error; - type MakeError = io::Error; - type Future = Box>; - type Service = DropContextService; + type Future = Pin>>>; + + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } - fn make_service(&mut self, service_ctx: &'a SC) -> Self::Future { - Box::new( - self.inner - .make_service(service_ctx) - .map(DropContextService::new), - ) + fn call(&mut self, service_ctx: &'a SC) -> Self::Future { + self.inner + .call(service_ctx) + .map(DropContextService::new) + .boxed() } } @@ -93,7 +93,7 @@ where pub struct DropContextService where C: Send + 'static, - T: hyper::service::Service, + T: hyper::service::Service, { inner: T, marker: PhantomData, @@ -102,7 +102,7 @@ where impl DropContextService where C: Send + 'static, - T: hyper::service::Service, + T: hyper::service::Service, { /// Create a new DropContextService struct wrapping a value pub fn new(inner: T) -> Self { @@ -112,17 +112,20 @@ where } } } -impl hyper::service::Service for DropContextService +impl hyper::service::Service for DropContextService where C: Send + 'static, - T: hyper::service::Service, + T: hyper::service::Service, { - type ReqBody = ContextualPayload; type ResBody = hyper::Body; type Error = Error; type Future = T::Future; - fn call(&mut self, req: Request) -> Self::Future { + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + Poll::Ready(Ok(())) + } + + fn call(&mut self, req: Request) -> Self::Future { let (head, body) = req.into_parts(); let body = body.inner; self.inner.call(Request::from_parts(head, body)) diff --git a/src/lib.rs b/src/lib.rs index 78e167693f..fde3972b92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,24 +2,6 @@ #![warn(missing_docs, missing_debug_implementations)] #![deny(unused_extern_crates)] -#[cfg(feature = "serdejson")] -extern crate serde; -#[cfg(feature = "serdejson")] -extern crate serde_json; -#[cfg(feature = "serdejson")] -#[cfg(test)] -#[macro_use] -extern crate serde_derive; - -extern crate base64; -extern crate chrono; -extern crate futures; -extern crate hyper; -extern crate hyper_old_types; -#[cfg(feature = "multipart")] -extern crate mime; -extern crate uuid; - use std::error; use std::fmt; diff --git a/src/nullable_format.rs b/src/nullable_format.rs index 4460ad402b..7a180d65e8 100644 --- a/src/nullable_format.rs +++ b/src/nullable_format.rs @@ -640,6 +640,7 @@ where #[cfg(feature = "serdejson")] mod serde_tests { use super::*; + use serde::{Deserialize, Serialize}; // Set up: #[derive(Clone, Debug, Deserialize, Serialize)] From d428629a1087323d48fafadd94a5112087619591 Mon Sep 17 00:00:00 2001 From: Danny Browning Date: Mon, 28 Oct 2019 12:09:53 -0600 Subject: [PATCH 2/2] Work on upgrading hyper --- Cargo.toml | 3 +- src/add_context.rs | 14 ++++----- src/auth.rs | 37 ++++++++++------------- src/client.rs | 9 ++++-- src/composites.rs | 23 +++++++-------- src/context.rs | 72 +++++++++++++++++++++------------------------ src/drop_context.rs | 14 ++++----- 7 files changed, 81 insertions(+), 91 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e64307639b..8cb607aad1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,8 @@ base64 = "0.10" mime = { version = "0.3", optional = true } serde = { version = "1.0", optional = true, features = ["derive"] } serde_json = { version = "1.0", optional = true } -hyper = "0.13.0-alpha.4" +#hyper = "0.13.0-alpha.4" +hyper = { git = "https://github.com/hyperium/hyper.git", branch = "master" } hyper-tls = "0.4.0-alpha.4" hyper-old-types = "0.11.0" native-tls = "0.2" diff --git a/src/add_context.rs b/src/add_context.rs index 7274a0a310..aed7f04e7a 100644 --- a/src/add_context.rs +++ b/src/add_context.rs @@ -45,20 +45,20 @@ where RC::Result: Send + 'static, T: hyper::service::Service< &'a SC, - ResBody = S, + Response = S, Error = ME, Future = Pin>>>, >, S: hyper::service::Service< ContextualPayload, - ResBody = hyper::Body, + Response = hyper::Body, Error = E, > + 'static, ME: ErrorBound, E: ErrorBound, S::Future: Send, { - type ResBody = T; + type Response = T; type Error = ME; type Future = Pin, Self::Error>> + Send>>; @@ -102,18 +102,18 @@ where } } -impl hyper::service::Service> for AddContextService +impl hyper::service::Service for AddContextService where C: Default + Push, C::Result: Send + 'static, T: hyper::service::Service< ContextualPayload, - ResBody = hyper::Body, + Response = hyper::Body, Error = E, >, E: ErrorBound, { - type ResBody = hyper::Body; + type Response = hyper::Body; type Error = E; type Future = T::Future; @@ -121,7 +121,7 @@ where Poll::Ready(Ok(())) } - fn call(&mut self, req: Request>) -> Self::Future { + fn call(&mut self, req: Request) -> Self::Future { let x_span_id = XSpanIdString::get_or_generate(&req); let (head, body) = req.into_parts(); let context = C::default().push(x_span_id); diff --git a/src/auth.rs b/src/auth.rs index 288b3b187c..c4f5a9ac4a 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -6,7 +6,7 @@ use futures::FutureExt; use hyper; use hyper::body::Payload; use hyper::header::AUTHORIZATION; -use hyper::service::{MakeService, Service}; +use hyper::service::Service; use hyper::{HeaderMap, Request, Response}; pub use hyper_old_types::header::Authorization as Header; use hyper_old_types::header::Header as HeaderTrait; @@ -127,17 +127,17 @@ where T: Service< &'a SC, Error = ME, - ResBody = S, + Response = S, Future = Pin>>>, >, - S: Service, Error = E, ResBody = OB> + S: Service, Error = E, Response = OB> + 'static, ME: ErrorBound, E: ErrorBound, S::Future: Send, OB: Payload, { - type ResBody = AllowAllAuthenticator; + type Response = AllowAllAuthenticator; type Error = ME; type Future = Pin, ME>> + Send>>; @@ -179,11 +179,11 @@ where RC: RcBound, RC::Result: Send + 'static, T: Service>, - T::Future: Future, T::Error>> + Send + 'static, + T::Future: Future, T::Error>> + Send + 'static, { - type ResBody = T::ResBody; + type Response = T::Response; type Error = T::Error; - type Future = Pin, T::Error>> + Send>>; + type Future = Pin, T::Error>> + Send>>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) @@ -230,25 +230,22 @@ mod tests { use super::*; use crate::EmptyContext; use hyper::server::conn::AddrStream; - use hyper::service::{make_service_fn, MakeService, MakeServiceRef, Service}; + use hyper::service::{make_service_fn, Service}; use hyper::{body::Payload, Body, Response}; use std::fmt::Debug; use std::io; fn check_inner_type<'a, T, SC: 'a, E, ME, S, F, IB, OB>(_: &T) where - T: MakeService< + T: Service< &'a SC, - IB, - Error = E, - MakeError = ME, - Service = S, + Error = ME, Future = F, - ResBody = OB, + Response = S, >, E: ErrorBound, ME: ErrorBound, - S: Service, + S: Service, F: Future>, IB: Payload, OB: Payload, @@ -256,13 +253,11 @@ mod tests { // This function is here merely to force a type check against the given bounds. } - fn check_type(_: &S) + fn check_type(_: &S) where - S: MakeServiceRef, + S: Service, S::Error: ErrorBound, - S::Service: 'static, B: Payload, - C: Payload, { // This function is here merely to force a type check against the given bounds. } @@ -270,9 +265,9 @@ mod tests { struct TestService(std::net::SocketAddr, PhantomData); impl Service for TestService { - type ResBody = Body; + type Response = Body; type Error = std::io::Error; - type Future = Pin, Self::Error>>>>; + type Future = Pin, Self::Error>>>>; fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) diff --git a/src/client.rs b/src/client.rs index 010764d538..5657da3848 100644 --- a/src/client.rs +++ b/src/client.rs @@ -19,9 +19,12 @@ impl Service for hyper::Client where B: hyper::body::Payload + Unpin + Send + 'static, B::Data: Send + Unpin, - C: hyper::client::connect::Connect + Sync + 'static, - C::Transport: 'static, - C::Future: 'static, + C: hyper::service::Service + + Clone + Send + Sync, + C::Future: Unpin, +// C: Service + Sync + 'static, +// C::Transport: 'static, +// C::Future: 'static, { type ReqBody = B; diff --git a/src/composites.rs b/src/composites.rs index bc727a4671..9fe6591ac0 100644 --- a/src/composites.rs +++ b/src/composites.rs @@ -1,9 +1,9 @@ //! Module for combining hyper services //! -//! Use by passing `hyper::server::MakeService` instances to a `CompositeMakeService` +//! Use by passing `hyper::server::Service` instances to a `CompositeMakeService` //! together with the base path for requests that should be handled by that service. use futures::FutureExt; -use hyper::service::{MakeService, Service}; +use hyper::service::Service; use hyper::{Request, Response, StatusCode}; use std::future::Future; use std::ops::{Deref, DerefMut}; @@ -32,7 +32,7 @@ type BoxedFuture = Pin>>>; type CompositeMakeServiceVec = Vec<(&'static str, Box>)>; type BoxedService = - Box, W>>>; + Box, W>>>; /// Trait for wrapping hyper `MakeService`s to make the return type of `make_service` uniform. /// This is necessary in order for the `MakeService`s with different `Instance` types to @@ -44,16 +44,13 @@ pub trait BoxedMakeService { impl<'a, SC, T, Rq, Rs, Er, S> BoxedMakeService<&'a SC, Rq, Rs, Er> for T where - S: Service, Er>> + S: Service, Er>> + 'static, - T: MakeService< + T: Service< &'a SC, - Rq, - ResBody = Rs, - Error = Er, + Response = S, + Error = io::Error, Future = Pin>>>, - Service = S, - MakeError = io::Error, >, Rq: hyper::body::Payload, Rs: hyper::body::Payload, @@ -64,7 +61,7 @@ where &mut self, context: &'a SC, ) -> Result, io::Error> { - let service = self.make_service(context).wait()?; + let service = self.call(context).wait()?; Ok(Box::new(service)) } } @@ -118,7 +115,7 @@ where V: NotFound + 'static + hyper::body::Payload, W: std::error::Error + Send + Sync + 'static, { - type ResBody = CompositeService; + type Response = CompositeService; type Error = io::Error; type Future = Pin, Self::Error>>>>; @@ -195,7 +192,7 @@ where V: NotFound + 'static + hyper::body::Payload, W: 'static + std::error::Error + Send + Sync, { - type ResBody = V; + type Response = V; type Error = W; type Future = Pin, W>>>>; diff --git a/src/context.rs b/src/context.rs index 40805afd9d..d0ec386dee 100644 --- a/src/context.rs +++ b/src/context.rs @@ -568,7 +568,7 @@ pub trait SwaggerService: Clone + hyper::service::Service< ContextualPayload, - ResBody = hyper::Body, + Response = hyper::Body, Error = hyper::Error, Future = Pin, hyper::Error>> + Send>>, > @@ -587,7 +587,7 @@ where T: Clone + hyper::service::Service< ContextualPayload, - ResBody = hyper::Body, + Response = hyper::Body, Error = hyper::Error, Future = Pin, hyper::Error>> + Send, @@ -632,7 +632,7 @@ where mod context_tests { use super::*; use futures::FutureExt; - use hyper::service::{MakeService, Service}; + use hyper::service::Service; use hyper::{Body, Error, Method, Request, Response, Uri}; use std::io; use std::future::Future; @@ -664,7 +664,7 @@ mod context_tests { where C: Has + Pop + Send + 'static, { - type ResBody = Body; + type Response = Body; type Error = Error; type Future = Pin, Error>>>>; @@ -702,21 +702,19 @@ mod context_tests { } } - impl MakeService for InnerMakeService + impl Service for InnerMakeService where RC: Has + Pop + Send + 'static, { - type ResBody = Body; - type Error = Error; - type Service = InnerService; - type Future = Pin>>>; - type MakeError = io::Error; + type Response = InnerService; + type Error = io::Error; + type Future = Pin>>>; - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } - fn make_service(&mut self, _: SC) -> Self::Future { + fn call(&mut self, _: SC) -> Self::Future { futures::future::ok(InnerService { marker: PhantomData, }) @@ -753,7 +751,7 @@ mod context_tests { T: Service>, E::Result: Send + 'static, { - type ResBody = T::ResBody; + type Response = T::Response; type Error = T::Error; type Future = T::Future; @@ -783,9 +781,9 @@ mod context_tests { RC::Result: Push, >::Result: Push, <>::Result as Push>::Result: Send + 'static, - T: MakeService< + T: Service< SC, - ContextualPayload< + Response=ContextualPayload< Body, <>::Result as Push>::Result, >, @@ -796,27 +794,25 @@ mod context_tests { marker2: PhantomData, } - impl MakeService> for MiddleMakeService + impl Service for MiddleMakeService where RC: Pop + Send + 'static, D: Push, E: Push, - T: MakeService>, + T: Service>, T::Future: 'static, E::Result: Send + 'static, { - type ResBody = T::ResBody; - type Error = T::Error; - type Service = MiddleService; - type Future = Pin>>>; - type MakeError = T::MakeError; + type Response = MiddleService; + type Error = std::io::Error; + type Future = Pin>>>; - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } - fn make_service(&mut self, sc: SC) -> Self::Future { - self.inner.make_service(sc).map(|s| MiddleService { + fn call(&mut self, sc: SC) -> Self::Future { + self.inner.call(sc).map(|s| MiddleService { inner: s, marker1: PhantomData, }) @@ -828,7 +824,7 @@ mod context_tests { RC: Pop, D: Push, E: Push, - T: MakeService>, + T: Service>, E::Result: Send + 'static, { fn new(inner: T) -> Self { @@ -861,7 +857,7 @@ mod context_tests { T: Service>, C::Result: Send + 'static, { - type ResBody = T::ResBody; + type Response = T::Response; type Error = T::Error; type Future = T::Future; @@ -886,7 +882,7 @@ mod context_tests { struct OuterMakeService where RC: Default + Push, - T: MakeService>, + T: Service>, RC::Result: Send + 'static, { inner: T, @@ -894,25 +890,23 @@ mod context_tests { marker2: PhantomData, } - impl MakeService> for OuterMakeService + impl Service for OuterMakeService where RC: Default + Push, RC::Result: Send + 'static, - T: MakeService>, + T: Service>, T::Future: 'static, { - type ResBody = T::ResBody; - type Error = T::Error; - type Service = OuterService; - type Future = Pin>>>; - type MakeError = T::MakeError; + type Response = OuterService; + type Error = std::io::Error; + type Future = Pin>>>; - fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { + fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll> { Poll::Ready(Ok(())) } - fn make_service(&mut self, sc: SC) -> Self::Future { - Box::new(self.inner.make_service(sc).map(|s| OuterService { + fn call(&mut self, sc: SC) -> Self::Future { + Box::new(self.inner.call(sc).map(|s| OuterService { inner: s, marker: PhantomData, })) @@ -923,7 +917,7 @@ mod context_tests { where RC: Default + Push, RC::Result: Send + 'static, - T: MakeService>, + T: Service>, { fn new(inner: T) -> Self { OuterMakeService { diff --git a/src/drop_context.rs b/src/drop_context.rs index 49b1998ada..f9bca75651 100644 --- a/src/drop_context.rs +++ b/src/drop_context.rs @@ -61,16 +61,16 @@ where RC: Send + 'static, T: hyper::service::Service< &'a SC, - ResBody = S, + Response = S, Error = io::Error, Future = F, >, T::Future: 'static, - S: hyper::service::Service + S: hyper::service::Service + 'static, F: Future>, { - type ResBody = DropContextService; + type Response = DropContextService; type Error = Error; type Future = Pin>>>; @@ -93,7 +93,7 @@ where pub struct DropContextService where C: Send + 'static, - T: hyper::service::Service, + T: hyper::service::Service, { inner: T, marker: PhantomData, @@ -102,7 +102,7 @@ where impl DropContextService where C: Send + 'static, - T: hyper::service::Service, + T: hyper::service::Service, { /// Create a new DropContextService struct wrapping a value pub fn new(inner: T) -> Self { @@ -115,9 +115,9 @@ where impl hyper::service::Service for DropContextService where C: Send + 'static, - T: hyper::service::Service, + T: hyper::service::Service, { - type ResBody = hyper::Body; + type Response = hyper::Body; type Error = Error; type Future = T::Future;