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