Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. use std::future::Future;
  2.  
  3. pub enum Error {
  4. Boom,
  5. }
  6. pub type AxiomResult = 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<S: Send + Sync, R: Future<Output = AxiomResult>>:
  15. (FnMut(&mut S, &Context, &Message) -> R) + Send + Sync + 'static
  16. {
  17. }
  18.  
  19. impl<F, R, S> Processor<S, R> for F
  20. where
  21. S: Send + Sync + 'static,
  22. R: Future<Output = AxiomResult>,
  23. F: (FnMut(&mut S, &Context, &Message) -> R) + Send + Sync + 'static,
  24. {
  25. }
  26.  
  27. pub struct Handler<S, R, P> where
  28. S: Send + Sync + 'static,
  29. R: Future<Output=AxiomResult>,
  30. P: Processor<S, R> + 'static,
  31. {
  32. state: S,
  33. processor: P,
  34. __phantom_data: std::marker::PhantomData<R>,
  35. }
  36.  
  37. impl<S, R, P> Handler<S, R, P> where
  38. S: Send + Sync + 'static,
  39. R: Future<Output=AxiomResult>,
  40. P: Processor<S, R> + 'static,
  41. {
  42. pub fn run(&mut self, context: &Context, message: &Message) -> R {
  43. (self.processor)(&mut self.state, context, message)
  44. }
  45. }
  46.  
  47. // ---- Test
  48. pub fn do_it(_: &mut i32, _: &Context, _: &Message) -> impl Future<Output = AxiomResult> {
  49. do_it_impl()
  50. }
  51.  
  52. pub async fn do_it_impl() -> AxiomResult {
  53. Ok("Done".to_string())
  54. }
  55.  
  56. #[test]
  57. pub fn test_async_fn() {
  58. let handler = Handler {
  59. state: 25,
  60. processor: do_it,
  61. __phantom_data: Default::default(),
  62. };
  63. }
  64.  
  65. // #[test]
  66. // pub fn async_wrap() {
  67. // let f = |_state: &mut usize, _context: &Context, _message: &Message| Ok("".to_string());
  68. // let handler = Handler {
  69. // state: 25,
  70. // processor: f,
  71. // __phantom_data: Default::default(),
  72. // };
  73. // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement