Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::future::Future;
- pub enum Error {
- Boom,
- }
- pub type AxiomResult = Result<String, Error>;
- pub type Message = String;
- pub struct Context {
- pub aid: u32,
- }
- // ---- REAL CODE
- pub trait AxiomFuture: Future<Output = AxiomResult> {}
- impl<T: Future<Output = AxiomResult>> AxiomFuture for T {}
- pub trait Processor<S: Send + Sync, R: AxiomFuture>:
- (FnMut(&mut S, &Context, &Message) -> R) + Send + Sync + 'static
- {
- }
- impl<F, R, S> Processor<S, R> for F
- where
- S: Send + Sync + 'static,
- R: AxiomFuture,
- F: (FnMut(&mut S, &Context, &Message) -> R) + Send + Sync + 'static,
- {
- }
- pub struct Handler<S, R, P> where
- S: Send + Sync + 'static,
- R: AxiomFuture,
- P: Processor<S, R> + 'static,
- {
- state: S,
- processor: P,
- __phantom_data: std::marker::PhantomData<R>,
- }
- impl<S, R, P> Handler<S, R, P> where
- S: Send + Sync + 'static,
- R: AxiomFuture,
- P: Processor<S, R> + 'static,
- {
- pub fn run(&mut self, context: &Context, message: &Message) -> R {
- (self.processor)(&mut self.state, context, message)
- }
- }
- // ---- Test
- pub fn do_it(_: &mut i32, _: &Context, _: &Message) -> impl AxiomFuture {
- do_it_impl()
- }
- pub async fn do_it_impl() -> AxiomResult {
- Ok("Done".to_string())
- }
- #[test]
- pub fn test_async_fn() {
- let handler = Handler {
- state: 25,
- processor: do_it,
- __phantom_data: Default::default(),
- };
- }
- // #[test]
- // pub fn async_wrap() {
- // let f = |_state: &mut usize, _context: &Context, _message: &Message| Ok("".to_string());
- // let handler = Handler {
- // state: 25,
- // processor: f,
- // __phantom_data: Default::default(),
- // };
- // }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement