Advertisement
Guest User

Untitled

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