Expand description
ump server running on a thread.
§Example
use std::ops::ControlFlow;
use ump_server::{
thread::{Handler, spawn},
ReplyContext, WeakClient
};
enum Request {
Add(usize, usize)
}
enum Reply {
Sum(usize)
}
#[derive(Debug)]
enum MyError { }
struct MyHandler {
wclnt: WeakClient<Request, Reply, MyError>
};
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| {
// Store a weak client in the handler so it doesn't keep the dispatch
// loop alive when the Client returned to the application is dropped.
Ok(MyHandler {
wclnt: clnt.weak()
})
}).unwrap();
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§
- Handler
- Message processing trait for a threaded handler.
Functions§
- spawn
- Run a thread which will process incoming messages from an ump server end-point.