use std::fmt::{self, Debug};
use std::sync::Arc;
use zrx_scheduler::{Id, Key, Value};
use zrx_store::stash::Items;
pub mod advance;
mod lifecycle;
pub mod set;
pub use advance::Advance;
use lifecycle::Lifecycle;
pub use set::Barriers;
trait BarrierFn<I>: Send + Sync {
fn contains(&self, scope: &Key<I>) -> bool;
}
#[derive(Clone)]
pub struct Barrier<I> {
function: Arc<dyn BarrierFn<I>>,
items: Items,
}
impl<I> Barrier<I>
where
I: Id,
{
#[must_use]
pub fn new<F>(f: F) -> Self
where
F: Fn(&Key<I>) -> bool + Send + Sync + 'static,
{
Self {
function: Arc::new(f),
items: Items::new(),
}
}
#[inline]
#[must_use]
pub fn contains(&self, scope: &Key<I>) -> bool {
self.function.contains(scope)
}
#[inline]
fn insert(&mut self, index: usize) -> bool {
self.items.insert(index)
}
#[inline]
fn remove(&mut self, index: usize) -> bool {
self.items.remove(index)
}
#[inline]
fn is_complete(&self, lifecycle: &Lifecycle) -> bool {
lifecycle.is_complete(&self.items)
}
}
#[allow(clippy::must_use_candidate)]
impl<I> Barrier<I> {
#[inline]
pub fn items(&self) -> &Items {
&self.items
}
}
impl<I> Value for Barrier<I> where I: Id {}
impl<I> PartialEq for Barrier<I> {
#[inline]
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.function, &other.function)
}
}
impl<I> Eq for Barrier<I> {}
impl<I> Debug for Barrier<I> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let function = "Box<dyn BarrierFn>";
f.debug_struct("Barrier")
.field("function", &function)
.field("items", &self.items)
.finish()
}
}
impl<F, I> BarrierFn<I> for F
where
F: Fn(&Key<I>) -> bool + Send + Sync,
{
#[inline]
fn contains(&self, scope: &Key<I>) -> bool {
self(scope)
}
}