Skip to main content

qubit_batch/execute/
batch_call_result.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10use crate::BatchOutcome;
11
12/// Result produced by [`crate::BatchExecutor::call`].
13///
14/// The execution outcome contains the same failure aggregation as
15/// [`crate::BatchExecutor::execute`]. The value list is indexed by the original
16/// callable index; successful callables store `Some(value)`, while failed or
17/// panicked callables store `None`.
18///
19/// ```rust
20/// use qubit_batch::{
21///     BatchExecutor,
22///     SequentialBatchExecutor,
23/// };
24///
25/// fn count_users() -> Result<usize, &'static str> {
26///     Ok(3)
27/// }
28///
29/// fn count_orders() -> Result<usize, &'static str> {
30///     Ok(5)
31/// }
32///
33/// let result = SequentialBatchExecutor::new()
34///     .call([count_users, count_orders])
35///     .expect("array length should be exact");
36///
37/// assert!(result.outcome().is_success());
38/// assert_eq!(result.values(), &[Some(3), Some(5)]);
39/// ```
40///
41/// # Type Parameters
42///
43/// * `R` - Callable success value type.
44/// * `E` - Callable error type.
45///
46#[derive(Debug, Clone, PartialEq, Eq)]
47pub struct BatchCallResult<R, E> {
48    /// Execution outcome and failures for the callable batch.
49    outcome: BatchOutcome<E>,
50    /// Success values indexed by callable position.
51    values: Vec<Option<R>>,
52}
53
54impl<R, E> BatchCallResult<R, E> {
55    /// Creates a new callable batch result.
56    ///
57    /// # Parameters
58    ///
59    /// * `outcome` - Execution outcome and failures.
60    /// * `values` - Success values indexed by callable position.
61    ///
62    /// # Returns
63    ///
64    /// A callable batch result.
65    #[inline]
66    pub fn new(outcome: BatchOutcome<E>, values: Vec<Option<R>>) -> Self {
67        Self { outcome, values }
68    }
69
70    /// Returns the execution outcome for the callable batch.
71    ///
72    /// # Returns
73    ///
74    /// A shared reference to the underlying execution outcome.
75    #[inline]
76    pub const fn outcome(&self) -> &BatchOutcome<E> {
77        &self.outcome
78    }
79
80    /// Returns success values indexed by callable position.
81    ///
82    /// # Returns
83    ///
84    /// A shared slice of optional success values.
85    #[inline]
86    pub fn values(&self) -> &[Option<R>] {
87        self.values.as_slice()
88    }
89
90    /// Consumes this result and returns the execution outcome.
91    ///
92    /// # Returns
93    ///
94    /// The underlying execution outcome.
95    #[inline]
96    pub fn into_outcome(self) -> BatchOutcome<E> {
97        self.outcome
98    }
99
100    /// Consumes this result and returns success values.
101    ///
102    /// # Returns
103    ///
104    /// Success values indexed by callable position.
105    #[inline]
106    pub fn into_values(self) -> Vec<Option<R>> {
107        self.values
108    }
109
110    /// Consumes this result and returns both stored parts.
111    ///
112    /// # Returns
113    ///
114    /// A tuple containing the execution outcome and indexed success values.
115    #[inline]
116    pub fn into_parts(self) -> (BatchOutcome<E>, Vec<Option<R>>) {
117        (self.outcome, self.values)
118    }
119}