Skip to main content

qubit_executor/service/
rejected_execution.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 RejectedExecution {
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 RejectedExecution {
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                | (
61                    Self::WorkerSpawnFailed { .. },
62                    Self::WorkerSpawnFailed { .. }
63                )
64        )
65    }
66}
67
68impl Eq for RejectedExecution {}