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
47
48
49
50
51
52
53
54
55
56
57
58
59
use core::marker::PhantomData;
use std::sync::{
  Arc,
  Mutex,
};

use ipc_channel::ipc;
use tokio::sync::{
  mpsc,
  oneshot,
  Mutex as AsyncMutex,
};

use super::types::*;

pub fn once_channel<T>() -> (SenderOnce<T>, ReceiverOnce<T>)
{
  let (sender, receiver) = oneshot::channel();

  (SenderOnce(sender), ReceiverOnce(receiver))
}

pub fn unbounded<T>() -> (Sender<T>, Receiver<T>)
{
  let (sender, receiver) = mpsc::unbounded_channel();

  (
    Sender(sender),
    Receiver(Arc::new(AsyncMutex::new(receiver))),
  )
}

pub fn ipc_channel<T>() -> (IpcSender<T>, IpcReceiver<T>)
where
  IpcReceiver<T>: Send,
{
  let (sender, receiver) = opaque_channel();

  (
    IpcSender {
      sender,
      phantom: PhantomData,
    },
    IpcReceiver {
      receiver,
      phantom: PhantomData,
    },
  )
}

pub fn opaque_channel() -> (OpaqueSender, OpaqueReceiver)
{
  let (sender, receiver) = ipc::channel::<()>().unwrap();

  (
    OpaqueSender(Arc::new(Mutex::new(Some(sender.to_opaque())))),
    OpaqueReceiver(Arc::new(Mutex::new(Some(receiver.to_opaque())))),
  )
}