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::{
16 ExecutionLogger,
17 executor_lock_builder::ExecutorLockBuilder,
18};
19use crate::lock::Lock;
20
21/// Initial builder for [`super::DoubleCheckedLockExecutor`].
22///
23/// This state has no lock yet. Call [`Self::on`] to attach the lock.
24///
25#[derive(Debug, Default, Clone)]
26pub struct ExecutorBuilder {
27 /// Logger carried forward to later builder states.
28 logger: ExecutionLogger,
29
30 /// Whether panics from tester, callbacks, and task are captured as result errors.
31 catch_panics: bool,
32}
33
34impl ExecutorBuilder {
35 /// Configures logging when the double-checked condition is not met.
36 ///
37 /// # Parameters
38 ///
39 /// * `level` - Log level used for unmet-condition messages.
40 /// * `message` - Full log message emitted when the condition is not met.
41 ///
42 /// # Returns
43 ///
44 /// This builder with unmet-condition logging configured.
45 #[inline]
46 #[must_use = "assign or chain the returned builder"]
47 pub fn log_unmet_condition(mut self, level: log::Level, message: impl Into<String>) -> Self {
48 self.logger.set_unmet_condition(Some(level), message);
49 self
50 }
51
52 /// Disables logging when the double-checked condition is not met.
53 ///
54 /// # Returns
55 ///
56 /// This builder with unmet-condition logging disabled.
57 #[inline]
58 #[must_use = "assign or chain the returned builder"]
59 pub fn disable_unmet_condition_logging(mut self) -> Self {
60 self.logger.disable_unmet_condition();
61 self
62 }
63
64 /// Configures logging when the prepare action fails.
65 ///
66 /// # Parameters
67 ///
68 /// * `level` - Log level used for prepare failure messages.
69 /// * `message_prefix` - Prefix placed before the prepare failure text.
70 ///
71 /// # Returns
72 ///
73 /// This builder with prepare failure logging configured.
74 #[inline]
75 #[must_use = "assign or chain the returned builder"]
76 pub fn log_prepare_failure(
77 mut self,
78 level: log::Level,
79 message_prefix: impl Into<String>,
80 ) -> Self {
81 self.logger.set_prepare_failure(Some(level), message_prefix);
82 self
83 }
84
85 /// Disables logging when the prepare action fails.
86 ///
87 /// # Returns
88 ///
89 /// This builder with prepare failure logging disabled.
90 #[inline]
91 #[must_use = "assign or chain the returned builder"]
92 pub fn disable_prepare_failure_logging(mut self) -> Self {
93 self.logger.disable_prepare_failure();
94 self
95 }
96
97 /// Configures logging when the prepare commit action fails.
98 ///
99 /// # Parameters
100 ///
101 /// * `level` - Log level used for prepare-commit failure messages.
102 /// * `message_prefix` - Prefix placed before the prepare-commit failure
103 /// text.
104 ///
105 /// # Returns
106 ///
107 /// This builder with prepare-commit failure logging configured.
108 #[inline]
109 #[must_use = "assign or chain the returned builder"]
110 pub fn log_prepare_commit_failure(
111 mut self,
112 level: log::Level,
113 message_prefix: impl Into<String>,
114 ) -> Self {
115 self.logger
116 .set_prepare_commit_failure(Some(level), message_prefix);
117 self
118 }
119
120 /// Disables logging when the prepare commit action fails.
121 ///
122 /// # Returns
123 ///
124 /// This builder with prepare-commit failure logging disabled.
125 #[inline]
126 #[must_use = "assign or chain the returned builder"]
127 pub fn disable_prepare_commit_failure_logging(mut self) -> Self {
128 self.logger.disable_prepare_commit_failure();
129 self
130 }
131
132 /// Configures logging when the prepare rollback action fails.
133 ///
134 /// # Parameters
135 ///
136 /// * `level` - Log level used for prepare-rollback failure messages.
137 /// * `message_prefix` - Prefix placed before the prepare-rollback failure
138 /// text.
139 ///
140 /// # Returns
141 ///
142 /// This builder with prepare-rollback failure logging configured.
143 #[inline]
144 #[must_use = "assign or chain the returned builder"]
145 pub fn log_prepare_rollback_failure(
146 mut self,
147 level: log::Level,
148 message_prefix: impl Into<String>,
149 ) -> Self {
150 self.logger
151 .set_prepare_rollback_failure(Some(level), message_prefix);
152 self
153 }
154
155 /// Disables logging when the prepare rollback action fails.
156 ///
157 /// # Returns
158 ///
159 /// This builder with prepare-rollback failure logging disabled.
160 #[inline]
161 #[must_use = "assign or chain the returned builder"]
162 pub fn disable_prepare_rollback_failure_logging(mut self) -> Self {
163 self.logger.disable_prepare_rollback_failure();
164 self
165 }
166
167 /// Attaches the lock protected by this executor.
168 ///
169 /// # Parameters
170 ///
171 /// * `lock` - The lock handle. Arc-based lock wrappers can be cloned and
172 /// stored here for reusable execution.
173 ///
174 /// # Returns
175 ///
176 /// The builder state that can configure the required tester.
177 #[inline]
178 #[must_use = "assign or chain the returned builder"]
179 pub fn on<L, T>(self, lock: L) -> ExecutorLockBuilder<L, T>
180 where
181 L: Lock<T>,
182 {
183 ExecutorLockBuilder {
184 lock,
185 logger: self.logger,
186 catch_panics: self.catch_panics,
187 _phantom: PhantomData,
188 }
189 }
190
191 /// Enables panic capture for tester, prepare callbacks, and task execution.
192 ///
193 /// # Returns
194 ///
195 /// This builder with panic capture enabled.
196 #[inline]
197 #[must_use = "assign or chain the returned builder"]
198 pub fn catch_panics(mut self) -> Self {
199 self.catch_panics = true;
200 self
201 }
202
203 /// Derives a builder with panic capture enabled or disabled for tester,
204 /// prepare callbacks, and task execution.
205 ///
206 /// # Parameters
207 ///
208 /// * `catch_panics` - `true` to capture panics as execution errors, or
209 /// `false` to let panics unwind.
210 ///
211 /// # Returns
212 ///
213 /// A reconfigured builder with the updated panic-capture setting.
214 #[inline]
215 #[must_use = "assign or chain the returned builder"]
216 pub fn with_panic_capture(mut self, catch_panics: bool) -> Self {
217 self.catch_panics = catch_panics;
218 self
219 }
220
221 /// Disables panic capture for tester, prepare callbacks, and task execution.
222 ///
223 /// # Returns
224 ///
225 /// This builder with panic capture disabled.
226 #[inline]
227 #[must_use = "assign or chain the returned builder"]
228 pub fn disable_catch_panics(mut self) -> Self {
229 self.catch_panics = false;
230 self
231 }
232}