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    #[inline]
33    pub fn log_unmet_condition(mut self, level: log::Level, message: impl Into<String>) -> Self {
34        self.inner = self.inner.log_unmet_condition(level, message);
35        self
36    }
37
38    /// Disables logging when the double-checked condition is not met.
39    #[inline]
40    pub fn disable_unmet_condition_logging(mut self) -> Self {
41        self.inner = self.inner.disable_unmet_condition_logging();
42        self
43    }
44
45    /// Configures logging when the prepare action fails.
46    #[inline]
47    pub fn log_prepare_failure(
48        mut self,
49        level: log::Level,
50        message_prefix: impl Into<String>,
51    ) -> Self {
52        self.inner = self.inner.log_prepare_failure(level, message_prefix);
53        self
54    }
55
56    /// Disables logging when the prepare action fails.
57    #[inline]
58    pub fn disable_prepare_failure_logging(mut self) -> Self {
59        self.inner = self.inner.disable_prepare_failure_logging();
60        self
61    }
62
63    /// Configures logging when the prepare commit action fails.
64    #[inline]
65    pub fn log_prepare_commit_failure(
66        mut self,
67        level: log::Level,
68        message_prefix: impl Into<String>,
69    ) -> Self {
70        self.inner = self.inner.log_prepare_commit_failure(level, message_prefix);
71        self
72    }
73
74    /// Disables logging when the prepare commit action fails.
75    #[inline]
76    pub fn disable_prepare_commit_failure_logging(mut self) -> Self {
77        self.inner = self.inner.disable_prepare_commit_failure_logging();
78        self
79    }
80
81    /// Configures logging when the prepare rollback action fails.
82    #[inline]
83    pub fn log_prepare_rollback_failure(
84        mut self,
85        level: log::Level,
86        message_prefix: impl Into<String>,
87    ) -> Self {
88        self.inner = self
89            .inner
90            .log_prepare_rollback_failure(level, message_prefix);
91        self
92    }
93
94    /// Disables logging when the prepare rollback action fails.
95    #[inline]
96    pub fn disable_prepare_rollback_failure_logging(mut self) -> Self {
97        self.inner = self.inner.disable_prepare_rollback_failure_logging();
98        self
99    }
100
101    /// Enables panic capture for tester, prepare callbacks, and task execution.
102    #[inline]
103    pub fn catch_panics(mut self) -> Self {
104        self.inner = self.inner.catch_panics();
105        self
106    }
107
108    /// Sets whether panic capture for tester, prepare callbacks, and task
109    /// execution is enabled.
110    #[inline]
111    pub fn set_catch_panics(mut self, catch_panics: bool) -> Self {
112        self.inner = self.inner.set_catch_panics(catch_panics);
113        self
114    }
115
116    /// Disables panic capture for tester, prepare callbacks, and task execution.
117    #[inline]
118    pub fn disable_catch_panics(mut self) -> Self {
119        self.inner = self.inner.disable_catch_panics();
120        self
121    }
122
123    /// Sets the required double-checked condition.
124    #[inline]
125    pub fn when<Tst>(self, tester: Tst) -> DoubleCheckedLockReadyBuilder<L, T>
126    where
127        Tst: Tester + Send + Sync + 'static,
128    {
129        DoubleCheckedLockReadyBuilder {
130            inner: self.inner.when(tester),
131        }
132    }
133}