1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use crate::{
errors::{Error, Result},
task::{ExecutionStatus, TransactionOutput},
};
use once_cell::sync::OnceCell;
pub(crate) struct OutcomeArray<T, E> {
results: Vec<OnceCell<ExecutionStatus<T, Error<E>>>>,
}
impl<T: TransactionOutput, E: Send> OutcomeArray<T, E> {
pub fn new(len: usize) -> OutcomeArray<T, E> {
OutcomeArray {
results: (0..len).map(|_| OnceCell::new()).collect(),
}
}
pub fn set_result(&self, idx: usize, res: ExecutionStatus<T, Error<E>>) {
let entry = &self.results[idx];
assert!(entry.set(res).is_ok());
}
pub fn get_all_results(self, stop_at: usize) -> Result<Vec<T>, E> {
let len = self.results.len();
let mut final_results = Vec::with_capacity(stop_at);
for (idx, status) in self.results.into_iter().take(stop_at).enumerate() {
let t = match status.into_inner() {
Some(ExecutionStatus::Success(t)) => t,
Some(ExecutionStatus::SkipRest(t)) if idx == stop_at - 1 => t,
Some(ExecutionStatus::SkipRest(_)) => return Err(Error::InvariantViolation),
Some(ExecutionStatus::Abort(err)) => return Err(err),
Some(ExecutionStatus::Retry(_)) => return Err(Error::InvariantViolation),
None => return Err(Error::InvariantViolation),
};
final_results.push(t)
}
assert!(final_results.len() == stop_at);
final_results.resize_with(len, T::skip_output);
Ok(final_results)
}
}