pub struct AsyncService<H, R, F>where
H: Fn(R, Arc<Request<()>>) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,{ /* private fields */ }Expand description
Asynchronous hyper service adapter.
This implements hyper::service::Service, decodes incoming requests using
FromRequest and passes the result to a provided handler closure.
Using this type takes a bit of boilerplate away from your app. Specifically, it handles:
- Suppressing the body of the response when the request used
HEAD. - Turning any
hyperdrive::Errorinto a proper HTTP response.
This type stores an async request handler H and the context needed by the
FromRequest implementation. The context is cloned for every request.
§Type Parameters
H: The handler closure. Takes aFromRequestimplementorR, and the original request. Returns a future resolving to the response to return to the client. Shared viaArc.R: The request type expected by the handlerH. ImplementsFromRequest.F: TheFuturereturned by the handler closureH.
§Examples
use hyperdrive::{FromRequest, service::AsyncService};
use hyper::{Server, Response, Request, Body};
use futures::prelude::*;
use std::sync::Arc;
#[derive(FromRequest)]
enum Route {
#[get("/")]
Index,
}
let service = AsyncService::new(|route: Route, orig: Arc<Request<()>>| {
// The closure is called with the `FromRequest`-implementing type and
// the original request. It has to return any type implementing
// `Future`.
match route {
Route::Index => {
Ok(Response::new(Body::from("Hello World!"))).into_future()
}
}
});
// Create the server future:
let srv = Server::bind(&"127.0.0.1:0".parse().unwrap())
.serve(service);Implementations§
Source§impl<H, R, F> AsyncService<H, R, F>
impl<H, R, F> AsyncService<H, R, F>
Sourcepub fn new(handler: H) -> Self
pub fn new(handler: H) -> Self
Creates an AsyncService from a handler closure.
This will pass a NoContext to the FromRequest implementation,
which will work fine as long as your type doesn’t require a custom
context. If you need to pass a custom context, refer to
with_context.
Source§impl<H, R, F> AsyncService<H, R, F>
impl<H, R, F> AsyncService<H, R, F>
Sourcepub fn with_context(handler: H, context: R::Context) -> Self
pub fn with_context(handler: H, context: R::Context) -> Self
Creates an AsyncService that will call handler to process incoming
requests.
§Parameters
handler: The handler closure. This is stored in anArcand is passed every decoded requestR. Returns a futureFresolving to the response to return.context: The context to pass to yourFromRequestimplementor.
Trait Implementations§
Source§impl<H, R, F> Clone for AsyncService<H, R, F>
impl<H, R, F> Clone for AsyncService<H, R, F>
Source§impl<H, R, F> Debug for AsyncService<H, R, F>
impl<H, R, F> Debug for AsyncService<H, R, F>
Source§impl<C, H, R, F> MakeService<C> for AsyncService<H, R, F>
impl<C, H, R, F> MakeService<C> for AsyncService<H, R, F>
Source§type Service = AsyncService<H, R, F>
type Service = AsyncService<H, R, F>
The resolved
Service from new_service().Source§type Future = FutureResult<AsyncService<H, R, F>, Box<dyn Error + Send + Sync>>
type Future = FutureResult<AsyncService<H, R, F>, Box<dyn Error + Send + Sync>>
The future returned from
new_service of a Service.Source§type MakeError = Box<dyn Error + Send + Sync>
type MakeError = Box<dyn Error + Send + Sync>
The error type that can be returned when creating a new
Service.Source§fn make_service(&mut self, _ctx: C) -> Self::Future
fn make_service(&mut self, _ctx: C) -> Self::Future
Create a new
Service.Source§impl<H, R, F> Service for AsyncService<H, R, F>
impl<H, R, F> Service for AsyncService<H, R, F>
Source§type Error = Box<dyn Error + Send + Sync>
type Error = Box<dyn Error + Send + Sync>
The error type that can occur within this
Service. Read moreSource§type Future = Box<dyn Future<Error = Box<dyn Error + Send + Sync>, Item = Response<Body>> + Send>
type Future = Box<dyn Future<Error = Box<dyn Error + Send + Sync>, Item = Response<Body>> + Send>
The
Future returned by this Service.Auto Trait Implementations§
impl<H, R, F> Freeze for AsyncService<H, R, F>
impl<H, R, F> RefUnwindSafe for AsyncService<H, R, F>
impl<H, R, F> Send for AsyncService<H, R, F>
impl<H, R, F> Sync for AsyncService<H, R, F>
impl<H, R, F> Unpin for AsyncService<H, R, F>
impl<H, R, F> UnwindSafe for AsyncService<H, R, F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> ServiceExt for Twhere
T: Service,
impl<T> ServiceExt for Twhere
T: Service,
Source§fn catch_unwind<H, R>(self, handler: H) -> CatchUnwind<T, R, H>
fn catch_unwind<H, R>(self, handler: H) -> CatchUnwind<T, R, H>
Catches any panics that occur in the service
self, and calls an
asynchronous panic handler with the panic payload. Read more