Skip to main content

beekeeper/hive/outcome/
batch.rs

1use super::{DerefOutcomes, Outcome, OwnedOutcomes};
2use crate::bee::{TaskId, Worker};
3use derive_more::Debug;
4use std::any;
5use std::collections::HashMap;
6use std::ops::{Deref, DerefMut};
7
8/// A batch of `Outcome`s.
9#[derive(Debug)]
10#[debug("OutcomeBatch<{}>", any::type_name::<W>())]
11pub struct OutcomeBatch<W: Worker>(HashMap<TaskId, Outcome<W>>);
12
13impl<W: Worker> OutcomeBatch<W> {
14    pub(crate) fn new(outcomes: HashMap<TaskId, Outcome<W>>) -> Self {
15        Self(outcomes)
16    }
17}
18
19impl<W: Worker, I: IntoIterator<Item = Outcome<W>>> From<I> for OutcomeBatch<W> {
20    fn from(value: I) -> Self {
21        OutcomeBatch::new(
22            value
23                .into_iter()
24                .map(|outcome| (*outcome.task_id(), outcome))
25                .collect(),
26        )
27    }
28}
29
30impl<W: Worker> OwnedOutcomes<W> for OutcomeBatch<W> {
31    #[inline]
32    fn outcomes(self) -> HashMap<TaskId, Outcome<W>> {
33        self.0
34    }
35
36    #[inline]
37    fn outcomes_ref(&self) -> &HashMap<TaskId, Outcome<W>> {
38        &self.0
39    }
40}
41
42impl<W: Worker> DerefOutcomes<W> for OutcomeBatch<W> {
43    #[inline]
44    fn outcomes_deref(&self) -> impl Deref<Target = HashMap<TaskId, Outcome<W>>> {
45        &self.0
46    }
47
48    #[inline]
49    fn outcomes_deref_mut(&mut self) -> impl DerefMut<Target = HashMap<TaskId, Outcome<W>>> {
50        &mut self.0
51    }
52}
53
54/// Functions only used in testing.
55#[cfg(test)]
56#[cfg_attr(coverage_nightly, coverage(off))]
57impl<W: Worker> OutcomeBatch<W> {
58    pub(crate) fn empty() -> Self {
59        OutcomeBatch::new(HashMap::new())
60    }
61
62    pub(crate) fn insert(&mut self, outcome: Outcome<W>) {
63        self.0.insert(*outcome.task_id(), outcome);
64    }
65}