1use ach_util::Error;
2use async_ach_ring::Ring;
3use core::ops::Deref;
4
5pub struct Sender<'a, T, const N: usize, const MP: usize, const MC: usize> {
6 mpmc: &'a Mpmc<T, N, MP, MC>,
7}
8impl<'a, T, const N: usize, const MP: usize, const MC: usize> Sender<'a, T, N, MP, MC> {
9 const fn new(mpmc: &'a Mpmc<T, N, MP, MC>) -> Self {
10 Sender { mpmc }
11 }
12}
13impl<'a, T: Unpin, const N: usize, const MP: usize, const MC: usize> Sender<'a, T, N, MP, MC> {
14 pub fn try_send(&self, t: T) -> Result<(), Error<T>> {
18 self.mpmc.try_push(t)
19 }
20 pub async fn send(&self, t: T) {
22 self.mpmc.push(t).await
23 }
24}
25
26pub struct Receiver<'a, T, const N: usize, const MP: usize, const MC: usize> {
27 mpmc: &'a Mpmc<T, N, MP, MC>,
28}
29impl<'a, T, const N: usize, const MP: usize, const MC: usize> Receiver<'a, T, N, MP, MC> {
30 const fn new(mpmc: &'a Mpmc<T, N, MP, MC>) -> Self {
31 Receiver { mpmc }
32 }
33}
34impl<'a, T: Unpin, const N: usize, const MP: usize, const MC: usize> Receiver<'a, T, N, MP, MC> {
35 pub fn try_recv(&self) -> Result<T, Error<()>> {
39 self.mpmc.try_pop()
40 }
41 pub async fn recv(&self) -> T {
43 self.mpmc.pop().await
44 }
45}
46
47pub struct Mpmc<T, const N: usize, const MP: usize, const MC: usize> {
48 ring: Ring<T, N, MP, MC>,
49}
50impl<T, const N: usize, const MP: usize, const MC: usize> Mpmc<T, N, MP, MC> {
51 pub const fn new() -> Self {
52 Self { ring: Ring::new() }
53 }
54 pub const fn sender(&'_ self) -> Sender<'_, T, N, MP, MC> {
55 Sender::new(self)
56 }
57 pub const fn recver(&'_ self) -> Receiver<'_, T, N, MP, MC> {
58 Receiver::new(self)
59 }
60}
61impl<T, const N: usize, const MP: usize, const MC: usize> Deref for Mpmc<T, N, MP, MC> {
62 type Target = Ring<T, N, MP, MC>;
63 fn deref(&self) -> &Self::Target {
64 &self.ring
65 }
66}