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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
pub mod broadcast;

#[cfg(feature = "asyncstd")]
mod inner_sync {
    pub use async_std::sync::Arc;
    pub use async_std::sync::Barrier;
    pub use async_std::sync::BarrierWaitResult;
    pub use async_std::sync::Mutex;
    pub use async_std::sync::MutexGuard;
    pub use async_std::sync::RwLock;
    pub use async_std::sync::RwLockReadGuard;
    pub use async_std::sync::RwLockWriteGuard;
    pub use async_std::sync::Weak;
}
#[cfg(feature = "asyncstd")]
pub use inner_sync::*;

pub mod mpsc {

    #[cfg(feature = "asyncstd")]
    pub use async_std::sync::Receiver;
    pub use async_std::sync::Sender;
    pub use async_std::sync::channel;


    #[cfg(feature = "tokio2")]
    pub use tokio::sync::broadcast::*;
}


pub use inner::Channel;

mod inner {

    use super::mpsc::Receiver;
    use super::mpsc::Sender;
    use super::mpsc::channel;

    /// abstraction for multi sender receiver channel
    #[derive(Debug)]
    pub struct Channel<T> {
        receiver: Receiver<T>,
        sender: Sender<T>
    }

    impl <T>Channel<T> {

        pub fn new(capacity: usize) -> Self {

            let (sender,receiver) = channel(capacity);
            Self {
                receiver,
                sender
            }
        }

        /// create new clone of sender
        pub fn sender(&self) -> Sender<T> {
            self.sender.clone()
        }

        #[cfg(feature = "asyncstd")]
        pub fn receiver(&self) -> Receiver<T> {
            self.receiver.clone()
        }

        #[cfg(feature = "tokio2")]
        pub fn receiver(&self) -> Receiver<T> {
            self.sender.subscribe()
        }
    }

}