use std::sync::Arc;
use qubit_function::Runnable;
pub(crate) struct ForEachTask<Item, E, F>
where
F: Fn(Item) -> Result<(), E> + Send + Sync,
{
item: Option<Item>,
action: Arc<F>,
}
impl<Item, E, F> ForEachTask<Item, E, F>
where
F: Fn(Item) -> Result<(), E> + Send + Sync,
{
#[inline]
pub(crate) fn new(item: Item, action: Arc<F>) -> Self {
Self {
item: Some(item),
action,
}
}
}
impl<Item, E, F> Runnable<E> for ForEachTask<Item, E, F>
where
F: Fn(Item) -> Result<(), E> + Send + Sync,
{
fn run(&mut self) -> Result<(), E> {
let item = self.item.take().expect("for_each task may only run once");
(self.action)(item)
}
}