Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. extern crate futures;
  2.  
  3. use futures::Future;
  4. use futures::Poll;
  5. use tokio::runtime::Runtime;
  6.  
  7. pub trait Service<Request> {
  8. /// Responses given by the service.
  9. type Response;
  10.  
  11. /// Errors produced by the service.
  12. type Error;
  13.  
  14. /// The future response value.
  15. type Future: Future<Item = Self::Response, Error = Self::Error>;
  16.  
  17. /// Returns `Ready` when the service is able to process requests.
  18. ///
  19. /// If the service is at capacity, then `NotReady` is returned and the task
  20. /// is notified when the service becomes ready again. This function is
  21. /// expected to be called while on a task.
  22. ///
  23. /// If `Err` is returned, the service is no longer able to service requests
  24. /// and the caller should discard the service instance.
  25. ///
  26. /// Once `poll_ready` returns `Ready`, a request may be dispatched to the
  27. /// service using `call`. Until a request is dispatched, repeated calls to
  28. /// `poll_ready` must return either `Ready` or `Err`.
  29. fn poll_ready(&mut self) -> Poll<(), Self::Error>;
  30.  
  31. /// Process the request and return the response asynchronously.
  32. ///
  33. /// This function is expected to be callable off task. As such,
  34. /// implementations should take care to not call `poll_ready`.
  35. ///
  36. /// Before dispatching a request, `poll_ready` must be called and return
  37. /// `Ready`.
  38. ///
  39. /// # Panics
  40. ///
  41. /// Implementations are permitted to panic if `call` is invoked without
  42. /// obtaining `Ready` from `poll_ready`.
  43. fn call(&mut self, req: Request) -> Self::Future;
  44. }
  45.  
  46. pub fn block_call<S, Request>(mut service: S, req: Request) -> Result<S::Response, S::Error>
  47. where
  48. S: Service<Request>,
  49. S::Future: Send + 'static,
  50. S::Response: Send + 'static,
  51. S::Error: Send + 'static,
  52. {
  53. let mut rt = Runtime::new().unwrap();
  54. let fut = service.call(req);
  55. rt.block_on(fut)
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement