Skip to main content

qubit_batch/execute/
batch_task_failure.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 super::batch_task_error::BatchTaskError;
11
12/// Failure record for one task inside a batch.
13///
14/// Each failure keeps the task's stable batch index so callers can map the
15/// failure back to the source task.
16///
17/// ```rust
18/// use qubit_batch::{
19///     BatchTaskError,
20///     BatchTaskFailure,
21/// };
22///
23/// let failure = BatchTaskFailure::new(2, BatchTaskError::Failed("invalid row"));
24///
25/// assert_eq!(failure.index(), 2);
26/// assert!(failure.error().is_failed());
27/// ```
28///
29/// # Type Parameters
30///
31/// * `E` - The task-specific error type.
32///
33#[derive(Debug, Clone, PartialEq, Eq)]
34pub struct BatchTaskFailure<E> {
35    /// Zero-based task index within the batch.
36    index: usize,
37    /// Error observed for this task.
38    error: BatchTaskError<E>,
39}
40
41impl<E> BatchTaskFailure<E> {
42    /// Creates a new batch task failure record.
43    ///
44    /// # Parameters
45    ///
46    /// * `index` - Zero-based task index within the batch.
47    /// * `error` - Error observed for the task at `index`.
48    ///
49    /// # Returns
50    ///
51    /// A failure record containing the task index and error.
52    #[inline]
53    pub fn new(index: usize, error: BatchTaskError<E>) -> Self {
54        Self { index, error }
55    }
56
57    /// Returns the failed task's zero-based batch index.
58    ///
59    /// # Returns
60    ///
61    /// The task index recorded for this failure.
62    #[inline]
63    pub const fn index(&self) -> usize {
64        self.index
65    }
66
67    /// Returns the task error recorded for this failure.
68    ///
69    /// # Returns
70    ///
71    /// A shared reference to the task error.
72    #[inline]
73    pub const fn error(&self) -> &BatchTaskError<E> {
74        &self.error
75    }
76
77    /// Consumes this failure record and returns the stored task error.
78    ///
79    /// # Returns
80    ///
81    /// The task error previously stored in this failure record.
82    #[inline]
83    pub fn into_error(self) -> BatchTaskError<E> {
84        self.error
85    }
86}