Module ump_server::task

source ·
Available on crate feature tokio only.
Expand description

ump server running in an async task.

use std::ops::ControlFlow;
use ump_server::{
  async_trait,
  task::{Handler, spawn},
  ump::ReplyContext
};
enum Request {
  Add(usize, usize)
}
enum Reply {
  Sum(usize)
}
enum MyError { }
struct MyHandler {};
#[async_trait]
impl Handler<Request, Reply, MyError, ()> for MyHandler {
  async fn proc_req(
    &mut self,
    msg: Request,
    rctx: ReplyContext<Reply, MyError>
  ) -> ControlFlow<(), ()> {
    match msg {
      Request::Add(a, b) => {
        rctx.reply(Reply::Sum(a + b));
        ControlFlow::Continue(())
      }
    }
  }
}

let (clnt, jh) = spawn(|clnt| {
  MyHandler { }
});

let Ok(Reply::Sum(sum)) = clnt.areq(Request::Add(3, 7)).await else {
  panic!("Unexpected reply");
};
assert_eq!(sum, 10);

// Dropping the only client will terminate the dispatch loop
drop(clnt);

let _ = jh.await;

Traits§

  • Message processing trait for an async handler.

Functions§

  • Run a task which will process incoming messages from an ump server end-point.