Skip to main content

qubit_dcl/double_checked/
double_checked_lock_ready_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 ready-builder state for [`super::DoubleCheckedLock`].
11
12use std::fmt::Display;
13
14use qubit_function::{
15    Callable,
16    CallableWith,
17    Runnable,
18    RunnableWith,
19};
20
21use super::{
22    DoubleCheckedLockExecutor,
23    ExecutionContext,
24    executor_ready_builder::ExecutorReadyBuilder,
25};
26use crate::lock::Lock;
27
28/// Convenience builder state with tester attached.
29#[derive(Clone)]
30pub struct DoubleCheckedLockReadyBuilder<L, T> {
31    /// Reusable-executor builder delegated to by the convenience API.
32    pub(in crate::double_checked) inner: ExecutorReadyBuilder<L, T>,
33}
34
35impl<L, T> DoubleCheckedLockReadyBuilder<L, T>
36where
37    L: Lock<T>,
38{
39    /// Configures logging when the double-checked condition is not met.
40    #[inline]
41    pub fn log_unmet_condition(mut self, level: log::Level, message: impl Into<String>) -> Self {
42        self.inner = self.inner.log_unmet_condition(level, message);
43        self
44    }
45
46    /// Disables logging when the double-checked condition is not met.
47    #[inline]
48    pub fn disable_unmet_condition_logging(mut self) -> Self {
49        self.inner = self.inner.disable_unmet_condition_logging();
50        self
51    }
52
53    /// Configures logging when the prepare action fails.
54    #[inline]
55    pub fn log_prepare_failure(
56        mut self,
57        level: log::Level,
58        message_prefix: impl Into<String>,
59    ) -> Self {
60        self.inner = self.inner.log_prepare_failure(level, message_prefix);
61        self
62    }
63
64    /// Disables logging when the prepare action fails.
65    #[inline]
66    pub fn disable_prepare_failure_logging(mut self) -> Self {
67        self.inner = self.inner.disable_prepare_failure_logging();
68        self
69    }
70
71    /// Configures logging when the prepare commit action fails.
72    #[inline]
73    pub fn log_prepare_commit_failure(
74        mut self,
75        level: log::Level,
76        message_prefix: impl Into<String>,
77    ) -> Self {
78        self.inner = self.inner.log_prepare_commit_failure(level, message_prefix);
79        self
80    }
81
82    /// Disables logging when the prepare commit action fails.
83    #[inline]
84    pub fn disable_prepare_commit_failure_logging(mut self) -> Self {
85        self.inner = self.inner.disable_prepare_commit_failure_logging();
86        self
87    }
88
89    /// Configures logging when the prepare rollback action fails.
90    #[inline]
91    pub fn log_prepare_rollback_failure(
92        mut self,
93        level: log::Level,
94        message_prefix: impl Into<String>,
95    ) -> Self {
96        self.inner = self
97            .inner
98            .log_prepare_rollback_failure(level, message_prefix);
99        self
100    }
101
102    /// Disables logging when the prepare rollback action fails.
103    #[inline]
104    pub fn disable_prepare_rollback_failure_logging(mut self) -> Self {
105        self.inner = self.inner.disable_prepare_rollback_failure_logging();
106        self
107    }
108
109    /// Enables panic capture for tester, prepare callbacks, and task execution.
110    #[inline]
111    pub fn catch_panics(mut self) -> Self {
112        self.inner = self.inner.catch_panics();
113        self
114    }
115
116    /// Sets whether panic capture for tester, prepare callbacks, and task
117    /// execution is enabled.
118    #[inline]
119    pub fn set_catch_panics(mut self, catch_panics: bool) -> Self {
120        self.inner = self.inner.set_catch_panics(catch_panics);
121        self
122    }
123
124    /// Disables panic capture for tester, prepare callbacks, and task execution.
125    #[inline]
126    pub fn disable_catch_panics(mut self) -> Self {
127        self.inner = self.inner.disable_catch_panics();
128        self
129    }
130
131    /// Sets the prepare action.
132    #[inline]
133    pub fn prepare<Rn, E>(mut self, prepare_action: Rn) -> Self
134    where
135        Rn: Runnable<E> + Send + 'static,
136        E: Display,
137    {
138        self.inner = self.inner.prepare(prepare_action);
139        self
140    }
141
142    /// Sets the rollback action for prepare.
143    #[inline]
144    pub fn rollback_prepare<Rn, E>(mut self, rollback_prepare_action: Rn) -> Self
145    where
146        Rn: Runnable<E> + Send + 'static,
147        E: Display,
148    {
149        self.inner = self.inner.rollback_prepare(rollback_prepare_action);
150        self
151    }
152
153    /// Sets the commit action for prepare.
154    #[inline]
155    pub fn commit_prepare<Rn, E>(mut self, commit_prepare_action: Rn) -> Self
156    where
157        Rn: Runnable<E> + Send + 'static,
158        E: Display,
159    {
160        self.inner = self.inner.commit_prepare(commit_prepare_action);
161        self
162    }
163
164    /// Builds a reusable [`DoubleCheckedLockExecutor`].
165    #[inline]
166    pub fn build(self) -> DoubleCheckedLockExecutor<L, T> {
167        self.inner.build()
168    }
169
170    /// Runs a callable task with one-shot executor creation.
171    #[inline]
172    pub fn call<C, R, E>(self, task: C) -> ExecutionContext<R, E>
173    where
174        C: Callable<R, E>,
175        E: Display,
176    {
177        self.inner.build().call(task)
178    }
179
180    /// Runs a runnable task with one-shot executor creation.
181    #[inline]
182    pub fn execute<Rn, E>(self, task: Rn) -> ExecutionContext<(), E>
183    where
184        Rn: Runnable<E>,
185        E: Display,
186    {
187        self.inner.build().execute(task)
188    }
189
190    /// Runs a callable task with mutable protected data.
191    #[inline]
192    pub fn call_with<C, R, E>(self, task: C) -> ExecutionContext<R, E>
193    where
194        C: CallableWith<T, R, E>,
195        E: Display,
196    {
197        self.inner.build().call_with(task)
198    }
199
200    /// Runs a runnable task with mutable protected data.
201    #[inline]
202    pub fn execute_with<Rn, E>(self, task: Rn) -> ExecutionContext<(), E>
203    where
204        Rn: RunnableWith<T, E>,
205        E: Display,
206    {
207        self.inner.build().execute_with(task)
208    }
209}