[][src]Struct hyperdrive::service::AsyncService

pub struct 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, 
{ /* fields omitted */ }

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 a FromRequest implementor R and returns a future resolving to the response to return to the client. Shared via Arc.
  • R: The request type expected by the handler H. Implements FromRequest.
  • F: The Future returned by the handler closure H.

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]

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]

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 an Arc and is passed every decoded request R. Returns a future F resolving to the response to return.
  • context: The context to pass to your FromRequest 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]

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]

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]

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 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]

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 Services.

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 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

impl<H, R, F> Sync for AsyncService<H, R, F> where
    <R as FromRequest>::Context: Sync

Blanket Implementations

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Erased for T

impl<N, Ctx> MakeService<Ctx> for N where
    N: NewService
[src]

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 Services.

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.