ipc_communication/
labor.rs

1//! Labor traits.
2
3/// A result returned by a "loafer".
4#[derive(Debug, Clone, Copy)]
5pub enum LoafingResult {
6    /// The caller can block on receiving data, since the loafer has done all it needed.
7    ImDone,
8
9    /// A hint to call the loafer again.
10    TouchMeAgain,
11}
12
13/// The one that does the hard job.
14pub trait Proletarian<Request, Response> {
15    /// Processes a request.
16    fn process_request(&mut self, request: Request) -> Response;
17
18    /// Loafs a bit, e.g. when there are no incoming requests.
19    fn loaf(&mut self) -> LoafingResult {
20        LoafingResult::ImDone
21    }
22}
23
24/// A `Proletarian` fabric.
25pub trait Socium<Request, Response> {
26    /// The one that does the hard job.
27    type Proletarian: Proletarian<Request, Response>;
28
29    /// Constructs an instance of a `Proletarian`.
30    fn construct_proletarian(&self, channel_id: usize) -> Self::Proletarian;
31}
32
33impl<F: FnMut(Req) -> Resp, Req, Resp> Proletarian<Req, Resp> for F {
34    fn process_request(&mut self, request: Req) -> Resp {
35        (self)(request)
36    }
37}
38
39impl<F, P, Req, Resp> Socium<Req, Resp> for F
40where
41    F: Fn(usize) -> P,
42    P: Proletarian<Req, Resp>,
43{
44    type Proletarian = P;
45
46    fn construct_proletarian(&self, channel_id: usize) -> Self::Proletarian {
47        (self)(channel_id)
48    }
49}