Module ump_server::thread

source ·
Expand description

ump server running on a thread.

use std::ops::ControlFlow;
use ump_server::{
  thread::{Handler, spawn},
  ump::ReplyContext
};
enum Request {
  Add(usize, usize)
}
enum Reply {
  Sum(usize)
}
enum MyError { }
struct MyHandler {};
impl Handler<Request, Reply, MyError, ()> for MyHandler {
  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.req(Request::Add(3, 7)) else {
  panic!("Unexpected reply");
};
assert_eq!(sum, 10);

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

let _ = jh.join();

Traits§

  • Message processing trait for a threaded handler.

Functions§

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