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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#![no_std]
use ach_ring as ach;
use ach_util::Error;
use async_ach_notify::{Listener, Notify};
use core::future::Future;
use core::pin::Pin;
use core::task::{Context, Poll};
pub struct Ring<T, const N: usize, const MP: usize, const MC: usize> {
buf: ach::Ring<T, N>,
consumer: Notify<MP>,
producer: Notify<MC>,
}
impl<T, const N: usize, const MP: usize, const MC: usize> Ring<T, N, MP, MC> {
pub const fn new() -> Self {
Self {
buf: ach::Ring::new(),
consumer: Notify::new(),
producer: Notify::new(),
}
}
}
impl<T: Unpin, const N: usize, const MP: usize, const MC: usize> Ring<T, N, MP, MC> {
pub fn try_push(&self, val: T) -> Result<(), Error<T>> {
self.buf.try_push(val).map(|x| {
self.producer.notify();
x
})
}
pub fn push(&self, val: T) -> Push<T, N, MP, MC> {
Push {
parent: self,
wait_c: self.consumer.listen(),
val: Some(val),
}
}
pub fn try_pop(&self) -> Result<T, Error<()>> {
self.buf.try_pop().map(|x| {
self.consumer.notify();
x
})
}
pub fn pop(&self) -> Pop<T, N, MP, MC> {
Pop {
parent: self,
wait_p: self.producer.listen(),
}
}
}
pub struct Push<'a, T: Unpin, const N: usize, const MP: usize, const MC: usize> {
parent: &'a Ring<T, N, MP, MC>,
wait_c: Listener<'a, MP>,
val: Option<T>,
}
impl<'a, T: Unpin, const N: usize, const MP: usize, const MC: usize> Future
for Push<'a, T, N, MP, MC>
{
type Output = ();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let val = if let Some(val) = self.val.take() {
val
} else {
return Poll::Ready(());
};
match self.parent.try_push(val) {
Ok(v) => Poll::Ready(v),
Err(err) => {
self.val = Some(err.input);
if Pin::new(&mut self.wait_c).poll(cx).is_ready() {
self.poll(cx)
} else {
Poll::Pending
}
}
}
}
}
pub struct Pop<'a, T, const N: usize, const MP: usize, const MC: usize> {
parent: &'a Ring<T, N, MP, MC>,
wait_p: Listener<'a, MC>,
}
impl<'a, T: Unpin, const N: usize, const MP: usize, const MC: usize> Future
for Pop<'a, T, N, MP, MC>
{
type Output = T;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
match self.parent.try_pop() {
Ok(v) => Poll::Ready(v),
Err(_) => {
if Pin::new(&mut self.wait_p).poll(cx).is_ready() {
self.poll(cx)
} else {
Poll::Pending
}
}
}
}
}