pub struct SyncService<H, R>where
H: Fn(R, Arc<Request<()>>) -> Response<Body> + Send + Sync + 'static,
R: FromRequest + Send + 'static,
R::Context: Clone,{ /* private fields */ }
Expand description
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 the original request. It has to return theResponse<Body>
to send to the client.R
: The request type implementingFromRequest
.
§Examples
use hyperdrive::{FromRequest, service::SyncService};
use hyper::{Request, Response, Body, Server};
use std::sync::Arc;
#[derive(FromRequest)]
enum Route {
#[get("/")]
Index,
}
let service = SyncService::new(|route: Route, orig: Arc<Request<()>>| {
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);
Implementations§
Source§impl<H, R> SyncService<H, R>
impl<H, R> SyncService<H, R>
Source§impl<H, R> SyncService<H, R>
impl<H, R> SyncService<H, R>
Sourcepub fn with_context(handler: H, context: R::Context) -> Self
pub fn with_context(handler: H, context: R::Context) -> Self
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§
Source§impl<H, R> Clone for SyncService<H, R>
impl<H, R> Clone for SyncService<H, R>
Source§impl<H, R> Debug for SyncService<H, R>
impl<H, R> Debug for SyncService<H, R>
Source§impl<C, H, R> MakeService<C> for SyncService<H, R>
impl<C, H, R> MakeService<C> for SyncService<H, R>
Source§type Service = SyncService<H, R>
type Service = SyncService<H, R>
The resolved
Service
from new_service()
.Source§type Future = FutureResult<SyncService<H, R>, Box<dyn Error + Sync + Send>>
type Future = FutureResult<SyncService<H, R>, Box<dyn Error + Sync + Send>>
The future returned from
new_service
of a Service
.Source§type MakeError = Box<dyn Error + Sync + Send>
type MakeError = Box<dyn Error + Sync + Send>
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> Service for SyncService<H, R>
impl<H, R> Service for SyncService<H, R>
Source§type Error = Box<dyn Error + Sync + Send>
type Error = Box<dyn Error + Sync + Send>
The error type that can occur within this
Service
. Read moreSource§type Future = Box<dyn Future<Item = Response<Body>, Error = Box<dyn Error + Sync + Send>> + Send>
type Future = Box<dyn Future<Item = Response<Body>, Error = Box<dyn Error + Sync + Send>> + Send>
The
Future
returned by this Service
.Auto Trait Implementations§
impl<H, R> Freeze for SyncService<H, R>
impl<H, R> RefUnwindSafe for SyncService<H, R>
impl<H, R> Send for SyncService<H, R>
impl<H, R> Sync for SyncService<H, R>
impl<H, R> Unpin for SyncService<H, R>
impl<H, R> UnwindSafe for SyncService<H, R>
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