SHARE
TWEET

Untitled

a guest Oct 20th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. use std::future::Future;
  2.  
  3. pub enum Error {
  4.     Boom,
  5. }
  6. pub type Result = Result<String, Error>;
  7. pub type Message = String;
  8. pub struct Context {
  9.     pub aid: u32,
  10. }
  11.  
  12. // ---- REAL CODE
  13.  
  14. pub trait Processor<State: Send + Sync, R: Future<Output = Result>>:
  15.     (FnMut(&mut State, &Context, &Message) -> R) + Send + Sync
  16. {
  17. }
  18.  
  19. impl<F, R, State> Processor<State, R> for F
  20. where
  21.     State: Send + Sync + 'static,
  22.     R: Future<Output = Result>,
  23.     F: (FnMut(&mut State, &Context, &Message) -> R) + Send + Sync + 'static,
  24. {
  25. }
  26.  
  27. pub struct Handler<T, R>
  28. where
  29.     R: Future<Output = Result>,
  30. {
  31.     state: T,
  32.     processor: dyn Processor<T, R>,
  33. }
  34.  
  35. impl<T, R: Future<Output = Result>> Handler<T, R> {
  36.     pub async fn run(&mut self, context: &Context, message: &Message) -> R {
  37.         (self.processor)(&mut self.state, context, message)
  38.     }
  39. }
  40.  
  41. // ---- Test
  42.  
  43. #[cfg(Test)]
  44. mod test {
  45.     pub fn do_it(_: &mut bool, _: &Context, _: &Message) -> Result {
  46.         Ok("Done".to_string())
  47.     }
  48.  
  49.     #[test]
  50.     pub fn test_async_fn() {
  51.         let handler: Handler<i32> = Handler {
  52.             state: 25,
  53.             processor: do_it,
  54.         };
  55.     }
  56.  
  57.     #[test]
  58.     pub fn async_wrap() {
  59.         let f = |_state: &mut usize, _context: &Context, _message: &Message| Ok(Status::Done);
  60.         let handler: Handler<i32> = Handler {
  61.             state: 25,
  62.             processor: f,
  63.         };
  64.     }
  65. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top