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