[][src]Struct hyperdrive::service::SyncService

pub struct SyncService<H, R> where
    H: Fn(R) -> Response<Body> + Send + Sync + 'static,
    R: FromRequest + Send + 'static,
    R::Context: Clone
{ /* fields omitted */ }

A hyper Service that dispatches requests to a blocking handler.

Just like AsyncService, 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 is effectively a bridge between async hyper and a synchronous, blocking app. Writing sync code is much simpler than writing async code (even with async/await syntax), so depending on your app this might be a good tradeoff.

Type Parameters

  • H: The handler closure. It is called with the request type R and has to return the Response<Body> to send to the client.
  • R: The request type implementing FromRequest.

Examples

use hyperdrive::{FromRequest, service::SyncService};
use hyper::{Response, Body, Server};

#[derive(FromRequest)]
enum Route {
    #[get("/")]
    Index,
}

let service = SyncService::new(|route: Route| {
    match route {
        Route::Index => Response::new(Body::from("Hello world!")),
    }
});

// Create the server future:
let srv = Server::bind(&"127.0.0.1:0".parse().unwrap())
   .serve(service);

Methods

impl<H, R> SyncService<H, R> where
    H: Fn(R) -> Response<Body> + Send + Sync + 'static,
    R: FromRequest<Context = NoContext> + Send + 'static, 
[src]

pub fn new(handler: H) -> Self[src]

Creates a SyncService that will call handler to process incoming requests.

impl<H, R> SyncService<H, R> where
    H: Fn(R) -> Response<Body> + Send + Sync + 'static,
    R: FromRequest + Send + 'static,
    R::Context: Clone
[src]

pub fn with_context(handler: H, context: R::Context) -> Self[src]

Creates a SyncService that will call handler to process incoming requests.

Parameters

  • handler: The handler closure. This is stored in an Arc and is called with every decoded request R. Returns the response to return to the client.
  • context: The context to pass to your FromRequest implementor. If you don't need a special context, new() should be called instead.

Trait Implementations

impl<H, R> Clone for SyncService<H, R> where
    H: Fn(R) -> Response<Body> + Send + Sync + 'static,
    R: FromRequest + Send + 'static,
    R::Context: Clone
[src]

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<H, R> Debug for SyncService<H, R> where
    H: Fn(R) -> Response<Body> + Send + Sync + 'static,
    R: FromRequest + Send + 'static,
    R::Context: Clone + Debug
[src]

impl<H, R> Service for SyncService<H, R> where
    H: Fn(R) -> Response<Body> + Send + Sync + 'static,
    R: FromRequest + Send + 'static,
    R::Context: Clone
[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> MakeService<C> for SyncService<H, R> where
    H: Fn(R) -> Response<Body> + Send + Sync + 'static,
    R: FromRequest + Send + 'static,
    R::Context: Clone
[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> Send for SyncService<H, R> where
    <R as FromRequest>::Context: Send

impl<H, R> Sync for SyncService<H, R> 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.