Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate futures;
- use futures::Future;
- use futures::Poll;
- use tokio::runtime::Runtime;
- pub trait Service<Request> {
- /// Responses given by the service.
- type Response;
- /// Errors produced by the service.
- type Error;
- /// The future response value.
- type Future: Future<Item = Self::Response, Error = Self::Error>;
- /// Returns `Ready` when the service is able to process requests.
- ///
- /// If the service is at capacity, then `NotReady` is returned and the task
- /// is notified when the service becomes ready again. This function is
- /// expected to be called while on a task.
- ///
- /// If `Err` is returned, the service is no longer able to service requests
- /// and the caller should discard the service instance.
- ///
- /// Once `poll_ready` returns `Ready`, a request may be dispatched to the
- /// service using `call`. Until a request is dispatched, repeated calls to
- /// `poll_ready` must return either `Ready` or `Err`.
- fn poll_ready(&mut self) -> Poll<(), Self::Error>;
- /// Process the request and return the response asynchronously.
- ///
- /// This function is expected to be callable off task. As such,
- /// implementations should take care to not call `poll_ready`.
- ///
- /// Before dispatching a request, `poll_ready` must be called and return
- /// `Ready`.
- ///
- /// # Panics
- ///
- /// Implementations are permitted to panic if `call` is invoked without
- /// obtaining `Ready` from `poll_ready`.
- fn call(&mut self, req: Request) -> Self::Future;
- }
- pub fn block_call<S, Request>(mut service: S, req: Request) -> Result<S::Response, S::Error>
- where
- S: Service<Request>,
- S::Future: Send + 'static,
- S::Response: Send + 'static,
- S::Error: Send + 'static,
- {
- let mut rt = Runtime::new().unwrap();
- let fut = service.call(req);
- rt.block_on(fut)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement