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
use super::ring::AtomicRing;
use core::ops::Deref;
pub struct Sender<'a, T, const N: usize> {
mpmc: &'a Mpmc<T, N>,
}
impl<'a, T, const N: usize> Sender<'a, T, N> {
const fn new(mpmc: &'a Mpmc<T, N>) -> Self {
Sender { mpmc }
}
pub fn send(&self, t: T) -> Result<(), T> {
self.mpmc.push(t)
}
}
pub struct Receiver<'a, T, const N: usize> {
mpmc: &'a Mpmc<T, N>,
}
impl<'a, T, const N: usize> Receiver<'a, T, N> {
const fn new(mpmc: &'a Mpmc<T, N>) -> Self {
Receiver { mpmc }
}
pub fn try_recv(&self) -> Option<T> {
self.mpmc.pop()
}
}
pub struct Mpmc<T, const N: usize> {
ring: AtomicRing<T, N>,
}
impl<T, const N: usize> Mpmc<T, N> {
pub const fn new() -> Self {
Mpmc {
ring: AtomicRing::new(),
}
}
pub fn sender(&self) -> Sender<T, N> {
Sender::new(self)
}
pub fn recver(&self) -> Receiver<T, N> {
Receiver::new(self)
}
}
impl<T, const N: usize> Deref for Mpmc<T, N> {
type Target = AtomicRing<T, N>;
fn deref(&self) -> &Self::Target {
&self.ring
}
}