Skip to main content

qubit_dcl/double_checked/
executor_builder.rs

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