Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 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>
  28. where
  29. R: Future<Output = AxiomResult>,
  30. {
  31. state: T,
  32. processor: dyn Processor<T, R>,
  33. }
  34.  
  35. impl<T, R: Future<Output = AxiomResult>> 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) -> AxiomResult {
  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.  
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement