[−][src]Struct hyperdrive::service::AsyncService
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::Error
into 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 aFromRequest
implementorR
and returns a future resolving to the response to return to the client. Shared viaArc
.R
: The request type expected by the handlerH
. ImplementsFromRequest
.F
: TheFuture
returned by the handler closureH
.
Examples
use hyperdrive::{FromRequest, service::AsyncService}; use hyper::{Server, Response, Body}; use futures::prelude::*; #[derive(FromRequest)] enum Route { #[get("/")] Index, } let service = AsyncService::new(|route: Route| { // The closure is called with the `FromRequest`-implementing type and // 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);
Methods
impl<H, R, F> AsyncService<H, R, F> where
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest<Context = NoContext>,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
[src]
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest<Context = NoContext>,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
pub fn new(handler: H) -> Self
[src]
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
.
impl<H, R, F> AsyncService<H, R, F> where
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
[src]
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
pub fn with_context(handler: H, context: R::Context) -> Self
[src]
Creates an AsyncService
that will call handler
to process incoming
requests.
Parameters
handler
: The handler closure. This is stored in anArc
and is passed every decoded requestR
. Returns a futureF
resolving to the response to return.context
: The context to pass to yourFromRequest
implementor.
Trait Implementations
impl<H, R, F> Clone for AsyncService<H, R, F> where
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
[src]
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
fn clone(&self) -> Self
[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<H, R, F> Debug for AsyncService<H, R, F> where
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone + Debug,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
[src]
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone + Debug,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
impl<H, R, F> Service for AsyncService<H, R, F> where
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
[src]
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
type ReqBody = Body
The Payload
body of the http::Request
.
type ResBody = Body
The Payload
body of the http::Response
.
type Error = BoxedError
The error type that can occur within this Service
. Read more
type Future = DefaultFuture<Response<Body>, BoxedError>
The Future
returned by this Service
.
fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future
[src]
fn poll_ready(&mut self) -> Result<Async<()>, Self::Error>
[src]
Returns Ready
when the service is able to process requests. Read more
impl<C, H, R, F> MakeService<C> for AsyncService<H, R, F> where
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
[src]
H: Fn(R) -> F + Send + Sync + 'static,
R: FromRequest,
R::Context: Clone,
R::Future: 'static,
F: Future<Item = Response<Body>, Error = BoxedError> + Send + 'static,
type ReqBody = Body
The Payload
body of the http::Request
.
type ResBody = Body
The Payload
body of the http::Response
.
type Error = BoxedError
The error type that can be returned by Service
s.
type Service = Self
The resolved Service
from new_service()
.
type Future = FutureResult<Self, BoxedError>
The future returned from new_service
of a Service
.
type MakeError = BoxedError
The error type that can be returned when creating a new Service
.
fn make_service(&mut self, _ctx: C) -> Self::Future
[src]
fn poll_ready(&mut self) -> Result<Async<()>, Self::MakeError>
[src]
Returns Ready
when the constructor is ready to create a new Service
. Read more
Auto Trait Implementations
impl<H, R, F> Send for AsyncService<H, R, F> where
<R as FromRequest>::Context: Send,
<R as FromRequest>::Context: Send,
impl<H, R, F> Sync for AsyncService<H, R, F> where
<R as FromRequest>::Context: Sync,
<R as FromRequest>::Context: Sync,
Blanket Implementations
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> From<T> for T
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Erased for T
impl<N, Ctx> MakeService<Ctx> for N where
N: NewService,
[src]
N: NewService,
type ReqBody = <N as NewService>::ReqBody
The Payload
body of the http::Request
.
type ResBody = <N as NewService>::ResBody
The Payload
body of the http::Response
.
type Error = <N as NewService>::Error
The error type that can be returned by Service
s.
type Service = <N as NewService>::Service
The resolved Service
from new_service()
.
type Future = <N as NewService>::Future
The future returned from new_service
of a Service
.
type MakeError = <N as NewService>::InitError
The error type that can be returned when creating a new Service
.
fn poll_ready(
&mut self
) -> Result<Async<()>, <N as MakeService<Ctx>>::MakeError>
[src]
&mut self
) -> Result<Async<()>, <N as MakeService<Ctx>>::MakeError>