use core::mem::{self, MaybeUninit};
use alloc::sync::Arc;
use crate::{Guard, RawGuard, SimpleBatch, qsbr, stack::Stack};
pub struct Cleanups<B = SimpleBatch> {
pub(crate) schedule: qsbr::Schedule,
pub(crate) deferred: [Stack<B>; 3],
pub(crate) reused: [Stack<B>; 3],
pub(crate) slots: [Stack<MaybeUninit<B>>; 3],
}
impl<B> Cleanups<B> {
pub fn new() -> Self {
Self {
schedule: qsbr::Schedule::new(),
deferred: Default::default(),
reused: Default::default(),
slots: Default::default(),
}
}
pub fn register(self: Arc<Self>) -> Guard<B> {
Guard::new(self)
}
pub fn register_raw(&self) -> RawGuard<B> {
RawGuard::new(self)
}
pub fn take_leftovers(&mut self) -> impl IntoIterator<Item = B> {
mem::take(&mut self.deferred)
.into_iter()
.chain(mem::take(&mut self.reused))
.flatten()
}
}
impl<B> Default for Cleanups<B> {
fn default() -> Self {
Self::new()
}
}