use crate::import::*;
type Producer<T> = ringbuf::HeapProducer<T>;
type Consumer<T> = ringbuf::HeapConsumer<T>;
#[ allow( dead_code )]
pub struct RingBuffer<T: Sized + Copy>
{
pub(crate) producer : Producer<T> ,
pub(crate) consumer : Consumer<T> ,
pub(crate) read_waker : Option<Waker> ,
pub(crate) write_waker: Option<Waker> ,
pub(crate) closed : bool ,
}
impl<T: Sized + Copy> RingBuffer<T>
{
pub fn new( size: usize ) -> Self
{
let (producer, consumer) = SyncRingBuffer::new( size ).split();
Self
{
producer ,
consumer ,
read_waker : None ,
write_waker : None ,
closed : false ,
}
}
pub fn capacity( &self ) -> usize
{
self.producer.capacity()
}
pub fn is_empty( &self ) -> bool
{
self.producer.is_empty()
}
pub fn is_full(&self) -> bool
{
self.producer.is_full()
}
pub fn len(&self) -> usize
{
self.producer.len()
}
pub fn remaining(&self) -> usize
{
self.producer.free_len()
}
}
impl<T: Sized + Copy> From< (Producer<T>, Consumer<T>) > for RingBuffer<T>
{
fn from( buffer: (Producer<T>, Consumer<T>) ) -> Self
{
let (producer, consumer) = (buffer.0, buffer.1);
Self
{
producer ,
consumer ,
read_waker : None ,
write_waker : None ,
closed : false ,
}
}
}
impl<T: Sized + Copy> From< SyncRingBuffer<T> > for RingBuffer<T>
{
fn from( buffer: SyncRingBuffer<T> ) -> Self
{
let (producer, consumer) = buffer.split();
Self
{
producer ,
consumer ,
read_waker : None ,
write_waker : None ,
closed : false ,
}
}
}
impl<T: Sized + Copy> fmt::Debug for RingBuffer<T>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result
{
write!( f, "RingBuffer with capacity: {}", self.capacity() )
}
}