use std::ops::ControlFlow;
#[derive(Clone)]
pub(super) struct RawAllAny<F, const ALL: bool> {
pred: Option<F>,
}
impl<F, const ALL: bool> RawAllAny<F, ALL> {
#[inline]
pub const fn new(pred: F) -> Self {
Self { pred: Some(pred) }
}
#[inline]
pub const fn get(&self) -> bool {
ALL ^ self.pred.is_none()
}
pub fn collect_impl(&mut self, f: impl FnOnce(&mut F) -> bool) -> ControlFlow<()> {
let Some(ref mut pred) = self.pred else {
return ControlFlow::Break(());
};
if ALL ^ f(pred) {
self.pred = None;
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}
}
pub fn collect_then_finish_impl(self, f: impl FnOnce(F) -> bool) -> bool {
self.pred.map_or(!ALL, f)
}
#[inline]
pub fn debug_impl(&self, mut f: std::fmt::DebugStruct<'_, '_>) -> std::fmt::Result {
f
.field(if ALL { "all" } else { "any" }, &self.get())
.finish()
}
#[inline]
pub fn break_hint(&self) -> ControlFlow<()> {
if self.pred.is_some() {
ControlFlow::Continue(())
} else {
ControlFlow::Break(())
}
}
}