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::{
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    #[inline]
37    pub fn log_unmet_condition(mut self, level: log::Level, message: impl Into<String>) -> Self {
38        self.logger.set_unmet_condition(Some(level), message);
39        self
40    }
41
42    /// Disables logging when the double-checked condition is not met.
43    #[inline]
44    pub fn disable_unmet_condition_logging(mut self) -> Self {
45        self.logger.disable_unmet_condition();
46        self
47    }
48
49    /// Configures logging when the prepare action fails.
50    #[inline]
51    pub fn log_prepare_failure(
52        mut self,
53        level: log::Level,
54        message_prefix: impl Into<String>,
55    ) -> Self {
56        self.logger.set_prepare_failure(Some(level), message_prefix);
57        self
58    }
59
60    /// Disables logging when the prepare action fails.
61    #[inline]
62    pub fn disable_prepare_failure_logging(mut self) -> Self {
63        self.logger.disable_prepare_failure();
64        self
65    }
66
67    /// Configures logging when the prepare commit action fails.
68    #[inline]
69    pub fn log_prepare_commit_failure(
70        mut self,
71        level: log::Level,
72        message_prefix: impl Into<String>,
73    ) -> Self {
74        self.logger
75            .set_prepare_commit_failure(Some(level), message_prefix);
76        self
77    }
78
79    /// Disables logging when the prepare commit action fails.
80    #[inline]
81    pub fn disable_prepare_commit_failure_logging(mut self) -> Self {
82        self.logger.disable_prepare_commit_failure();
83        self
84    }
85
86    /// Configures logging when the prepare rollback action fails.
87    #[inline]
88    pub fn log_prepare_rollback_failure(
89        mut self,
90        level: log::Level,
91        message_prefix: impl Into<String>,
92    ) -> Self {
93        self.logger
94            .set_prepare_rollback_failure(Some(level), message_prefix);
95        self
96    }
97
98    /// Disables logging when the prepare rollback action fails.
99    #[inline]
100    pub fn disable_prepare_rollback_failure_logging(mut self) -> Self {
101        self.logger.disable_prepare_rollback_failure();
102        self
103    }
104
105    /// Attaches the lock protected by this executor.
106    ///
107    /// # Parameters
108    ///
109    /// * `lock` - The lock handle. Arc-based lock wrappers can be cloned and
110    ///   stored here for reusable execution.
111    ///
112    /// # Returns
113    ///
114    /// The builder state that can configure the required tester.
115    #[inline]
116    pub fn on<L, T>(self, lock: L) -> ExecutorLockBuilder<L, T>
117    where
118        L: Lock<T>,
119    {
120        ExecutorLockBuilder {
121            lock,
122            logger: self.logger,
123            catch_panics: self.catch_panics,
124            _phantom: PhantomData,
125        }
126    }
127
128    /// Enables panic capture for tester, prepare callbacks, and task execution.
129    #[inline]
130    pub fn catch_panics(mut self) -> Self {
131        self.catch_panics = true;
132        self
133    }
134
135    /// Sets whether panic capture for tester, prepare callbacks, and task
136    /// execution is enabled.
137    #[inline]
138    pub fn set_catch_panics(mut self, catch_panics: bool) -> Self {
139        self.catch_panics = catch_panics;
140        self
141    }
142
143    /// Disables panic capture for tester, prepare callbacks, and task execution.
144    #[inline]
145    pub fn disable_catch_panics(mut self) -> Self {
146        self.catch_panics = false;
147        self
148    }
149}