1use std::io::Error;
6
7use ipc_channel::{IpcError, TryRecvError, ipc};
8use serde::{Deserialize, Serialize};
9
10use crate::time::{ProfilerCategory, ProfilerChan};
11use crate::time_profile;
12
13pub struct IpcReceiver<T>
14where
15 T: for<'de> Deserialize<'de> + Serialize,
16{
17 ipc_receiver: ipc::IpcReceiver<T>,
18 time_profile_chan: ProfilerChan,
19}
20
21impl<T> IpcReceiver<T>
22where
23 T: for<'de> Deserialize<'de> + Serialize,
24{
25 pub fn recv(&self) -> Result<T, IpcError> {
26 time_profile!(
27 ProfilerCategory::IpcReceiver,
28 None,
29 self.time_profile_chan.clone(),
30 move || self.ipc_receiver.recv(),
31 )
32 }
33
34 pub fn try_recv(&self) -> Result<T, TryRecvError> {
35 self.ipc_receiver.try_recv()
36 }
37
38 pub fn to_ipc_receiver(self) -> ipc::IpcReceiver<T> {
39 self.ipc_receiver
40 }
41}
42
43pub fn channel<T>(
44 time_profile_chan: ProfilerChan,
45) -> Result<(ipc::IpcSender<T>, IpcReceiver<T>), Error>
46where
47 T: for<'de> Deserialize<'de> + Serialize,
48{
49 let (ipc_sender, ipc_receiver) = ipc::channel()?;
50 let profiled_ipc_receiver = IpcReceiver {
51 ipc_receiver,
52 time_profile_chan,
53 };
54 Ok((ipc_sender, profiled_ipc_receiver))
55}