use ach_ring as ach; use ach_util::Error; use event_listener::Event as Notify; pub struct Ring { buf: ach::Ring, consumer: Notify, producer: Notify, } impl Ring { pub fn new() -> Self { Self { buf: ach::Ring::new(), consumer: Notify::new(), producer: Notify::new(), } } } impl Ring { pub fn try_push(&self, val: T) -> Result<(), Error> { self.buf.push(val).map(|x| { self.producer.notify(1); x }) } pub async fn push(&self, mut val: T) { loop { let l =self.consumer.listen(); if let Err(err) = self.try_push(val) { val = err.input; l.await; } else { self.consumer.notify(1); break; } } } pub fn try_pop(&self) -> Result> { self.buf.pop().map(|x| { self.consumer.notify(1); x }) } pub async fn pop(&self) -> T { loop { let l=self.producer.listen(); if let Ok(v) = self.try_pop() { self.producer.notify(1); break v; } else { l.await; } } } }