Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. use std::future::Future;
  2.  
  3. pub enum AxiomError {
  4. Boom,
  5. }
  6. pub type AxiomResult = Result<String, AxiomError>;
  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=AxiomResult>>:
  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=AxiomResult>,
  23. F: (FnMut(&mut State, &Context, &Message) -> R) + Send + Sync + 'static,
  24. {
  25. }
  26.  
  27. pub struct Handler<T, R: Future<Output=AxiomResult>> {
  28. state: T,
  29. processor: dyn Processor<T, R>,
  30. }
  31.  
  32. impl<T, R: Future<Output=AxiomResult>> Handler<T, R> {
  33. pub async fn run(&mut self, context: &Context, message: &Message) -> R {
  34. (self.processor)(&mut self.state, context, message)
  35. }
  36. }
  37.  
  38. // ---- Test
  39.  
  40. pub async fn do_it(_: &mut bool, _: &Context, _: &Message) -> AxiomResult {
  41. Ok("Done".to_string())
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement