1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use crate::errors::SendError;
use anyhow::Result;
use futures::channel::oneshot;
#[derive(Debug)]
pub struct Request<Input, Output> {
message: Input,
respond_channel: oneshot::Sender<Output>,
}
impl<Input, Output> Request<Input, Output> {
pub fn new(message: Input) -> (Self, oneshot::Receiver<Output>) {
let (respond_channel, receiver_channel) = oneshot::channel();
let message = Self {
message,
respond_channel,
};
(message, receiver_channel)
}
pub fn message(&self) -> &Input {
&self.message
}
pub fn respond(self, output: Output) -> Result<(), SendError> {
self.respond_channel
.send(output)
.map_err(|_| SendError::ReceiverDisconnected)?;
Ok(())
}
pub fn into_inner(self) -> (Input, oneshot::Sender<Output>) {
(self.message, self.respond_channel)
}
}