Struct SyncService

Source
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 type R and the original request. It 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::{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>
where H: Fn(R, Arc<Request<()>>) -> Response<Body> + Send + Sync + 'static, R: FromRequest<Context = NoContext> + Send + 'static,

Source

pub fn new(handler: H) -> Self

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

Source§

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

Source

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

Source§

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

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<C, H, R> MakeService<C> for SyncService<H, R>
where H: Fn(R, Arc<Request<()>>) -> Response<Body> + Send + Sync + 'static, R: FromRequest + Send + 'static, R::Context: Clone,

Source§

type ReqBody = Body

The Payload body of the http::Request.
Source§

type ResBody = Body

The Payload body of the http::Response.
Source§

type Error = Box<dyn Error + Sync + Send>

The error type that can be returned by Services.
Source§

type Service = SyncService<H, R>

The resolved Service from new_service().
Source§

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>

The error type that can be returned when creating a new Service.
Source§

fn make_service(&mut self, _ctx: C) -> Self::Future

Create a new Service.
Source§

fn poll_ready(&mut self) -> Result<Async<()>, Self::MakeError>

Returns Ready when the constructor is ready to create a new Service. Read more
Source§

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

Source§

type ReqBody = Body

The Payload body of the http::Request.
Source§

type ResBody = Body

The Payload body of the http::Response.
Source§

type Error = Box<dyn Error + Sync + Send>

The error type that can occur within this Service. Read more
Source§

type Future = Box<dyn Future<Item = Response<Body>, Error = Box<dyn Error + Sync + Send>> + Send>

The Future returned by this Service.
Source§

fn call(&mut self, req: Request<Self::ReqBody>) -> Self::Future

Calls this Service with a request, returning a Future of the response.
Source§

fn poll_ready(&mut self) -> Result<Async<()>, Self::Error>

Returns Ready when the service is able to process requests. Read more

Auto Trait Implementations§

§

impl<H, R> Freeze for SyncService<H, R>
where <R as FromRequest>::Context: Sized + Freeze,

§

impl<H, R> RefUnwindSafe for SyncService<H, R>

§

impl<H, R> Send for SyncService<H, R>
where <R as FromRequest>::Context: Sized + Send,

§

impl<H, R> Sync for SyncService<H, R>
where <R as FromRequest>::Context: Sized + Sync,

§

impl<H, R> Unpin for SyncService<H, R>
where <R as FromRequest>::Context: Sized + Unpin,

§

impl<H, R> UnwindSafe for SyncService<H, R>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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 more
Source§

impl<T> ServiceExt for T
where T: Service,

Source§

fn catch_unwind<H, R>(self, handler: H) -> CatchUnwind<T, R, H>
where T: Service<ResBody = Body, Error = Box<dyn Error + Sync + Send>> + Sync, <T as Service>::Future: Send, H: Fn(Box<dyn Any + Send>) -> R + Send + Sync + 'static, R: IntoFuture<Item = Response<Body>, Error = Box<dyn Error + Sync + Send>>, <R as IntoFuture>::Future: Send + 'static,

Catches any panics that occur in the service self, and calls an asynchronous panic handler with the panic payload. Read more
Source§

fn make_service_by_cloning(self) -> MakeServiceByCloning<T>
where T: Clone,

Creates a type implementing MakeService by cloning self for every incoming connection. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,