Skip to main content

qubit_executor/service/
submission_error.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 std::{
11    io,
12    sync::Arc,
13};
14
15use thiserror::Error;
16
17/// Error returned when an executor service refuses to accept a task.
18///
19/// This error is about task acceptance only. It does not describe task
20/// execution success or failure; accepted tasks report their final result
21/// through the handle returned by the service.
22///
23#[derive(Debug, Clone, Error)]
24pub enum SubmissionError {
25    /// The service has been shut down and no longer accepts new tasks.
26    #[error("task rejected because the executor service is shut down")]
27    Shutdown,
28
29    /// The service is saturated and cannot accept more tasks.
30    #[error("task rejected because the executor service is saturated")]
31    Saturated,
32
33    /// The service accepted the task conceptually but could not create the
34    /// worker thread required to execute it.
35    #[error("task rejected because the executor service failed to spawn a worker: {source}")]
36    WorkerSpawnFailed {
37        /// I/O error reported while spawning the worker.
38        source: Arc<io::Error>,
39    },
40}
41
42impl PartialEq for SubmissionError {
43    /// Compares rejection categories.
44    ///
45    /// Worker spawn failures compare equal by variant because [`io::Error`]
46    /// does not provide value equality.
47    ///
48    /// # Parameters
49    ///
50    /// * `other` - Rejection value to compare with this value.
51    ///
52    /// # Returns
53    ///
54    /// `true` when both values have the same rejection category.
55    fn eq(&self, other: &Self) -> bool {
56        matches!(
57            (self, other),
58            (Self::Shutdown, Self::Shutdown)
59                | (Self::Saturated, Self::Saturated)
60                | (Self::WorkerSpawnFailed { .. }, Self::WorkerSpawnFailed { .. })
61        )
62    }
63}
64
65impl Eq for SubmissionError {}
66
67impl SubmissionError {
68    /// Creates a worker-spawn rejection from a thread creation error.
69    ///
70    /// # Parameters
71    ///
72    /// * `source` - I/O error returned by the thread builder.
73    ///
74    /// # Returns
75    ///
76    /// A worker-spawn rejection carrying the source error.
77    #[inline]
78    pub fn worker_spawn_failed(source: io::Error) -> Self {
79        Self::WorkerSpawnFailed {
80            source: Arc::new(source),
81        }
82    }
83}