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    #[must_use = "assign or chain the returned builder"]
43    pub fn log_unmet_condition(mut self, level: log::Level, message: impl Into<String>) -> Self {
44        self.inner = self.inner.log_unmet_condition(level, message);
45        self
46    }
47
48    /// Disables logging when the double-checked condition is not met.
49    ///
50    /// # Returns
51    ///
52    /// This builder with unmet-condition logging disabled.
53    #[inline]
54    #[must_use = "assign or chain the returned builder"]
55    pub fn disable_unmet_condition_logging(mut self) -> Self {
56        self.inner = self.inner.disable_unmet_condition_logging();
57        self
58    }
59
60    /// Configures logging when the prepare action fails.
61    ///
62    /// # Parameters
63    ///
64    /// * `level` - Log level used for prepare failure messages.
65    /// * `message_prefix` - Prefix placed before the prepare failure text.
66    ///
67    /// # Returns
68    ///
69    /// This builder with prepare failure logging configured.
70    #[inline]
71    #[must_use = "assign or chain the returned builder"]
72    pub fn log_prepare_failure(
73        mut self,
74        level: log::Level,
75        message_prefix: impl Into<String>,
76    ) -> Self {
77        self.inner = self.inner.log_prepare_failure(level, message_prefix);
78        self
79    }
80
81    /// Disables logging when the prepare action fails.
82    ///
83    /// # Returns
84    ///
85    /// This builder with prepare failure logging disabled.
86    #[inline]
87    #[must_use = "assign or chain the returned builder"]
88    pub fn disable_prepare_failure_logging(mut self) -> Self {
89        self.inner = self.inner.disable_prepare_failure_logging();
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    #[must_use = "assign or chain the returned builder"]
106    pub fn log_prepare_commit_failure(
107        mut self,
108        level: log::Level,
109        message_prefix: impl Into<String>,
110    ) -> Self {
111        self.inner = self.inner.log_prepare_commit_failure(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    #[must_use = "assign or chain the returned builder"]
122    pub fn disable_prepare_commit_failure_logging(mut self) -> Self {
123        self.inner = self.inner.disable_prepare_commit_failure_logging();
124        self
125    }
126
127    /// Configures logging when the prepare rollback action fails.
128    ///
129    /// # Parameters
130    ///
131    /// * `level` - Log level used for prepare-rollback failure messages.
132    /// * `message_prefix` - Prefix placed before the prepare-rollback failure
133    ///   text.
134    ///
135    /// # Returns
136    ///
137    /// This builder with prepare-rollback failure logging configured.
138    #[inline]
139    #[must_use = "assign or chain the returned builder"]
140    pub fn log_prepare_rollback_failure(
141        mut self,
142        level: log::Level,
143        message_prefix: impl Into<String>,
144    ) -> Self {
145        self.inner = self
146            .inner
147            .log_prepare_rollback_failure(level, message_prefix);
148        self
149    }
150
151    /// Disables logging when the prepare rollback action fails.
152    ///
153    /// # Returns
154    ///
155    /// This builder with prepare-rollback failure logging disabled.
156    #[inline]
157    #[must_use = "assign or chain the returned builder"]
158    pub fn disable_prepare_rollback_failure_logging(mut self) -> Self {
159        self.inner = self.inner.disable_prepare_rollback_failure_logging();
160        self
161    }
162
163    /// Enables panic capture for tester, prepare callbacks, and task execution.
164    ///
165    /// # Returns
166    ///
167    /// This builder with panic capture enabled.
168    #[inline]
169    #[must_use = "assign or chain the returned builder"]
170    pub fn catch_panics(mut self) -> Self {
171        self.inner = self.inner.catch_panics();
172        self
173    }
174
175    /// Derives a builder with panic capture enabled or disabled for tester,
176    /// prepare callbacks, and task execution.
177    ///
178    /// # Parameters
179    ///
180    /// * `catch_panics` - `true` to capture panics as execution errors, or
181    ///   `false` to let panics unwind.
182    ///
183    /// # Returns
184    ///
185    /// A reconfigured builder with the updated panic-capture setting.
186    #[inline]
187    #[must_use = "assign or chain the returned builder"]
188    pub fn with_panic_capture(mut self, catch_panics: bool) -> Self {
189        self.inner = self.inner.with_panic_capture(catch_panics);
190        self
191    }
192
193    /// Disables panic capture for tester, prepare callbacks, and task execution.
194    ///
195    /// # Returns
196    ///
197    /// This builder with panic capture disabled.
198    #[inline]
199    #[must_use = "assign or chain the returned builder"]
200    pub fn disable_catch_panics(mut self) -> Self {
201        self.inner = self.inner.disable_catch_panics();
202        self
203    }
204
205    /// Sets the required double-checked condition.
206    ///
207    /// # Parameters
208    ///
209    /// * `tester` - Condition tester executed before and after acquiring the
210    ///   lock.
211    ///
212    /// # Returns
213    ///
214    /// A ready builder that can configure prepare callbacks or run the task.
215    #[inline]
216    #[must_use = "assign or chain the returned builder"]
217    pub fn when<Tst>(self, tester: Tst) -> DoubleCheckedLockReadyBuilder<L, T>
218    where
219        Tst: Tester + Send + Sync + 'static,
220    {
221        DoubleCheckedLockReadyBuilder {
222            inner: self.inner.when(tester),
223        }
224    }
225}