[−][src]Struct hyperdrive::service::SyncService
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 typeR
and has to return theResponse<Body>
to send to the client.R
: The request type implementingFromRequest
.
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]
H: Fn(R) -> Response<Body> + Send + Sync + 'static,
R: FromRequest<Context = NoContext> + Send + 'static,
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]
H: Fn(R) -> Response<Body> + Send + Sync + 'static,
R: FromRequest + Send + 'static,
R::Context: Clone,
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 anArc
and is called with every decoded requestR
. Returns the response to return to the client.context
: The context to pass to yourFromRequest
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]
H: Fn(R) -> Response<Body> + Send + Sync + 'static,
R: FromRequest + Send + 'static,
R::Context: Clone,
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> Debug for SyncService<H, R> where
H: Fn(R) -> Response<Body> + Send + Sync + 'static,
R: FromRequest + Send + 'static,
R::Context: Clone + Debug,
[src]
H: Fn(R) -> Response<Body> + Send + Sync + 'static,
R: FromRequest + Send + 'static,
R::Context: Clone + Debug,
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]
H: Fn(R) -> Response<Body> + Send + Sync + 'static,
R: FromRequest + Send + 'static,
R::Context: Clone,
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> MakeService<C> for SyncService<H, R> where
H: Fn(R) -> Response<Body> + Send + Sync + 'static,
R: FromRequest + Send + 'static,
R::Context: Clone,
[src]
H: Fn(R) -> Response<Body> + Send + Sync + 'static,
R: FromRequest + Send + 'static,
R::Context: Clone,
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> Send for SyncService<H, R> where
<R as FromRequest>::Context: Send,
<R as FromRequest>::Context: Send,
impl<H, R> Sync for SyncService<H, R> 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>