use core::cell::UnsafeCell;
#[cfg(feature = "async")]
use crate::iters_components::async_iters::{mutable::AsyncCompMut, non_mutable::AsyncComp};
use crate::{
iters_components::shared_iters::{mutable::SharedCompMut, non_mutable::SharedComp},
ring_buffer::{iters_components::IterComponent, storage_components::PStorageComponent},
};
pub mod impls;
pub mod iters_components;
pub mod storage_components;
pub mod types;
pub mod wrappers;
pub(crate) trait SharedRB {}
impl<S: PStorageComponent> SharedRB for OneRingBuf<S, SharedComp> {}
impl<S: PStorageComponent> SharedRB for OneRingBuf<S, SharedCompMut> {}
#[cfg(feature = "async")]
impl<S: PStorageComponent> SharedRB for OneRingBuf<S, AsyncComp> {}
#[cfg(feature = "async")]
impl<S: PStorageComponent> SharedRB for OneRingBuf<S, AsyncCompMut> {}
pub trait OneRB {
type Item;
type Storage: PStorageComponent<Item = Self::Item>;
type Iters: IterComponent;
fn iters(&self) -> &Self::Iters;
fn storage(&self) -> &Self::Storage;
fn storage_mut(&self) -> &mut Self::Storage;
fn len(&self) -> usize;
}
pub struct OneRingBuf<S: PStorageComponent, I: IterComponent> {
pub(crate) inner: UnsafeCell<S>,
pub(crate) iters: I,
}
impl<S: PStorageComponent, I: IterComponent> OneRB for OneRingBuf<S, I> {
type Item = S::Item;
type Storage = S;
type Iters = I;
#[inline]
fn iters(&self) -> &Self::Iters {
&self.iters
}
#[inline]
fn storage(&self) -> &Self::Storage {
unsafe { &*self.inner.get() }
}
#[inline]
fn storage_mut(&self) -> &mut Self::Storage {
unsafe { &mut *self.inner.get() }
}
#[inline]
fn len(&self) -> usize {
self.storage().len()
}
}
impl<S: PStorageComponent, I: IterComponent> OneRingBuf<S, I> {
pub(crate) const fn _from(value: S, iters: I) -> Self {
Self {
inner: UnsafeCell::new(value),
iters: iters,
}
}
}