Skip to main content

qubit_dcl/double_checked/
double_checked_lock_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//! Convenience builder state for [`super::DoubleCheckedLock`].
11
12use qubit_function::Tester;
13
14use super::{
15    DoubleCheckedLockReadyBuilder,
16    executor_lock_builder::ExecutorLockBuilder,
17};
18use crate::lock::Lock;
19
20/// Convenience builder state with lock attached.
21#[derive(Clone)]
22pub struct DoubleCheckedLockBuilder<L, T> {
23    /// Reusable-executor builder delegated to by the convenience API.
24    pub(in crate::double_checked) inner: ExecutorLockBuilder<L, T>,
25}
26
27impl<L, T> DoubleCheckedLockBuilder<L, T>
28where
29    L: Lock<T>,
30{
31    /// Configures logging when the double-checked condition is not met.
32    ///
33    /// # Parameters
34    ///
35    /// * `level` - Log level used for unmet-condition messages.
36    /// * `message` - Full log message emitted when the condition is not met.
37    ///
38    /// # Returns
39    ///
40    /// This builder with unmet-condition logging configured.
41    #[inline]
42    pub fn log_unmet_condition(mut self, level: log::Level, message: impl Into<String>) -> Self {
43        self.inner = self.inner.log_unmet_condition(level, message);
44        self
45    }
46
47    /// Disables logging when the double-checked condition is not met.
48    ///
49    /// # Returns
50    ///
51    /// This builder with unmet-condition logging disabled.
52    #[inline]
53    pub fn disable_unmet_condition_logging(mut self) -> Self {
54        self.inner = self.inner.disable_unmet_condition_logging();
55        self
56    }
57
58    /// Configures logging when the prepare action fails.
59    ///
60    /// # Parameters
61    ///
62    /// * `level` - Log level used for prepare failure messages.
63    /// * `message_prefix` - Prefix placed before the prepare failure text.
64    ///
65    /// # Returns
66    ///
67    /// This builder with prepare failure logging configured.
68    #[inline]
69    pub fn log_prepare_failure(
70        mut self,
71        level: log::Level,
72        message_prefix: impl Into<String>,
73    ) -> Self {
74        self.inner = self.inner.log_prepare_failure(level, message_prefix);
75        self
76    }
77
78    /// Disables logging when the prepare action fails.
79    ///
80    /// # Returns
81    ///
82    /// This builder with prepare failure logging disabled.
83    #[inline]
84    pub fn disable_prepare_failure_logging(mut self) -> Self {
85        self.inner = self.inner.disable_prepare_failure_logging();
86        self
87    }
88
89    /// Configures logging when the prepare commit action fails.
90    ///
91    /// # Parameters
92    ///
93    /// * `level` - Log level used for prepare-commit failure messages.
94    /// * `message_prefix` - Prefix placed before the prepare-commit failure
95    ///   text.
96    ///
97    /// # Returns
98    ///
99    /// This builder with prepare-commit failure logging configured.
100    #[inline]
101    pub fn log_prepare_commit_failure(
102        mut self,
103        level: log::Level,
104        message_prefix: impl Into<String>,
105    ) -> Self {
106        self.inner = self.inner.log_prepare_commit_failure(level, message_prefix);
107        self
108    }
109
110    /// Disables logging when the prepare commit action fails.
111    ///
112    /// # Returns
113    ///
114    /// This builder with prepare-commit failure logging disabled.
115    #[inline]
116    pub fn disable_prepare_commit_failure_logging(mut self) -> Self {
117        self.inner = self.inner.disable_prepare_commit_failure_logging();
118        self
119    }
120
121    /// Configures logging when the prepare rollback action fails.
122    ///
123    /// # Parameters
124    ///
125    /// * `level` - Log level used for prepare-rollback failure messages.
126    /// * `message_prefix` - Prefix placed before the prepare-rollback failure
127    ///   text.
128    ///
129    /// # Returns
130    ///
131    /// This builder with prepare-rollback failure logging configured.
132    #[inline]
133    pub fn log_prepare_rollback_failure(
134        mut self,
135        level: log::Level,
136        message_prefix: impl Into<String>,
137    ) -> Self {
138        self.inner = self
139            .inner
140            .log_prepare_rollback_failure(level, message_prefix);
141        self
142    }
143
144    /// Disables logging when the prepare rollback action fails.
145    ///
146    /// # Returns
147    ///
148    /// This builder with prepare-rollback failure logging disabled.
149    #[inline]
150    pub fn disable_prepare_rollback_failure_logging(mut self) -> Self {
151        self.inner = self.inner.disable_prepare_rollback_failure_logging();
152        self
153    }
154
155    /// Enables panic capture for tester, prepare callbacks, and task execution.
156    ///
157    /// # Returns
158    ///
159    /// This builder with panic capture enabled.
160    #[inline]
161    #[must_use = "assign or chain the returned builder"]
162    pub fn catch_panics(mut self) -> Self {
163        self.inner = self.inner.catch_panics();
164        self
165    }
166
167    /// Derives a builder with panic capture enabled or disabled for tester,
168    /// prepare callbacks, and task execution.
169    ///
170    /// # Parameters
171    ///
172    /// * `catch_panics` - `true` to capture panics as execution errors, or
173    ///   `false` to let panics unwind.
174    ///
175    /// # Returns
176    ///
177    /// A reconfigured builder with the updated panic-capture setting.
178    #[inline]
179    #[must_use = "assign or chain the returned builder"]
180    pub fn with_panic_capture(mut self, catch_panics: bool) -> Self {
181        self.inner = self.inner.with_panic_capture(catch_panics);
182        self
183    }
184
185    /// Disables panic capture for tester, prepare callbacks, and task execution.
186    ///
187    /// # Returns
188    ///
189    /// This builder with panic capture disabled.
190    #[inline]
191    #[must_use = "assign or chain the returned builder"]
192    pub fn disable_catch_panics(mut self) -> Self {
193        self.inner = self.inner.disable_catch_panics();
194        self
195    }
196
197    /// Sets the required double-checked condition.
198    ///
199    /// # Parameters
200    ///
201    /// * `tester` - Condition tester executed before and after acquiring the
202    ///   lock.
203    ///
204    /// # Returns
205    ///
206    /// A ready builder that can configure prepare callbacks or run the task.
207    #[inline]
208    pub fn when<Tst>(self, tester: Tst) -> DoubleCheckedLockReadyBuilder<L, T>
209    where
210        Tst: Tester + Send + Sync + 'static,
211    {
212        DoubleCheckedLockReadyBuilder {
213            inner: self.inner.when(tester),
214        }
215    }
216}