Skip to main content

qubit_dcl/double_checked/
executor_builder.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 ******************************************************************************/
10//! Initial builder for [`super::DoubleCheckedLockExecutor`].
11//!
12
13use std::marker::PhantomData;
14
15use super::{ExecutionLogger, executor_lock_builder::ExecutorLockBuilder};
16use crate::lock::Lock;
17
18/// Initial builder for [`super::DoubleCheckedLockExecutor`].
19///
20/// This state has no lock yet. Call [`Self::on`] to attach the lock.
21///
22#[derive(Debug, Default, Clone)]
23pub struct ExecutorBuilder {
24    /// Logger carried forward to later builder states.
25    logger: ExecutionLogger,
26
27    /// Whether panics from tester, callbacks, and task are captured as result errors.
28    catch_panics: bool,
29}
30
31impl ExecutorBuilder {
32    /// Configures logging when the double-checked condition is not met.
33    #[inline]
34    pub fn log_unmet_condition(mut self, level: log::Level, message: impl Into<String>) -> Self {
35        self.logger.set_unmet_condition(Some(level), message);
36        self
37    }
38
39    /// Disables logging when the double-checked condition is not met.
40    #[inline]
41    pub fn disable_unmet_condition_logging(mut self) -> Self {
42        self.logger.disable_unmet_condition();
43        self
44    }
45
46    /// Configures logging when the prepare action fails.
47    #[inline]
48    pub fn log_prepare_failure(
49        mut self,
50        level: log::Level,
51        message_prefix: impl Into<String>,
52    ) -> Self {
53        self.logger.set_prepare_failure(Some(level), message_prefix);
54        self
55    }
56
57    /// Disables logging when the prepare action fails.
58    #[inline]
59    pub fn disable_prepare_failure_logging(mut self) -> Self {
60        self.logger.disable_prepare_failure();
61        self
62    }
63
64    /// Configures logging when the prepare commit action fails.
65    #[inline]
66    pub fn log_prepare_commit_failure(
67        mut self,
68        level: log::Level,
69        message_prefix: impl Into<String>,
70    ) -> Self {
71        self.logger
72            .set_prepare_commit_failure(Some(level), message_prefix);
73        self
74    }
75
76    /// Disables logging when the prepare commit action fails.
77    #[inline]
78    pub fn disable_prepare_commit_failure_logging(mut self) -> Self {
79        self.logger.disable_prepare_commit_failure();
80        self
81    }
82
83    /// Configures logging when the prepare rollback action fails.
84    #[inline]
85    pub fn log_prepare_rollback_failure(
86        mut self,
87        level: log::Level,
88        message_prefix: impl Into<String>,
89    ) -> Self {
90        self.logger
91            .set_prepare_rollback_failure(Some(level), message_prefix);
92        self
93    }
94
95    /// Disables logging when the prepare rollback action fails.
96    #[inline]
97    pub fn disable_prepare_rollback_failure_logging(mut self) -> Self {
98        self.logger.disable_prepare_rollback_failure();
99        self
100    }
101
102    /// Attaches the lock protected by this executor.
103    ///
104    /// # Parameters
105    ///
106    /// * `lock` - The lock handle. Arc-based lock wrappers can be cloned and
107    ///   stored here for reusable execution.
108    ///
109    /// # Returns
110    ///
111    /// The builder state that can configure the required tester.
112    #[inline]
113    pub fn on<L, T>(self, lock: L) -> ExecutorLockBuilder<L, T>
114    where
115        L: Lock<T>,
116    {
117        ExecutorLockBuilder {
118            lock,
119            logger: self.logger,
120            catch_panics: self.catch_panics,
121            _phantom: PhantomData,
122        }
123    }
124
125    /// Enables panic capture for tester, prepare callbacks, and task execution.
126    #[inline]
127    pub fn catch_panics(mut self) -> Self {
128        self.catch_panics = true;
129        self
130    }
131
132    /// Sets whether panic capture for tester, prepare callbacks, and task
133    /// execution is enabled.
134    #[inline]
135    pub fn set_catch_panics(mut self, catch_panics: bool) -> Self {
136        self.catch_panics = catch_panics;
137        self
138    }
139
140    /// Disables panic capture for tester, prepare callbacks, and task execution.
141    #[inline]
142    pub fn disable_catch_panics(mut self) -> Self {
143        self.catch_panics = false;
144        self
145    }
146}