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