pub enum ExecutionResult<T, E> {
Success(T),
ConditionNotMet,
Failed(ExecutorError<E>),
}Expand description
Task execution result
Represents the result of executing a task using an enum to clearly distinguish between success, unmet conditions, and failure.
§Type Parameters
T- The type of the return value when execution succeedsE- The type of the error when execution fails
§Examples
use qubit_dcl::double_checked::{ExecutionResult, ExecutorError};
let success: ExecutionResult<i32, String> = ExecutionResult::success(42);
if let ExecutionResult::Success(val) = success {
println!("Value: {}", val);
}
let unmet: ExecutionResult<i32, String> = ExecutionResult::unmet();
let failed: ExecutionResult<i32, String> =
ExecutionResult::task_failed("Task failed".to_string());Variants§
Success(T)
Execution succeeded with a value
ConditionNotMet
Double-checked locking condition was not met
Failed(ExecutorError<E>)
Execution failed with an error
Implementations§
Source§impl<T, E> ExecutionResult<T, E>
impl<T, E> ExecutionResult<T, E>
Sourcepub fn success(value: T) -> Self
pub fn success(value: T) -> Self
Builds ExecutionResult::Success with value.
§Parameters
value- Successful task value.
§Returns
A success result containing value.
Sourcepub fn unmet() -> Self
pub fn unmet() -> Self
Builds ExecutionResult::ConditionNotMet.
§Returns
A result representing a failed double-check condition.
Sourcepub fn task_failed(err: E) -> Self
pub fn task_failed(err: E) -> Self
Builds a failed result with ExecutorError::TaskFailed.
§Parameters
err- Error returned by the executed task.
§Returns
A failed result wrapping the task error.
Sourcepub fn prepare_failed(msg: impl Display) -> Self
pub fn prepare_failed(msg: impl Display) -> Self
Builds a failed result with ExecutorError::PrepareFailed.
Accepts any std::fmt::Display value (including std::error::Error and String);
the message is stored in a CallbackError wrapper.
§Parameters
msg- Prepare error message or displayable error value.
§Returns
A failed result containing the prepare failure message.
Sourcepub fn prepare_commit_failed(msg: impl Display) -> Self
pub fn prepare_commit_failed(msg: impl Display) -> Self
Builds a failed result with ExecutorError::PrepareCommitFailed.
§Parameters
msg- Commit error message or displayable error value.
§Returns
A failed result containing the prepare-commit failure message.
Sourcepub fn prepare_commit_failed_with_callback_type(
callback_type: &'static str,
msg: impl Display,
) -> Self
pub fn prepare_commit_failed_with_callback_type( callback_type: &'static str, msg: impl Display, ) -> Self
Builds a failed result with ExecutorError::PrepareCommitFailed and
explicit callback type metadata.
The callback type can later be read from
ExecutorError::callback_type.
§Parameters
callback_type- Callback type tag, e.g."prepare_commit".msg- Commit error message or displayable error value.
§Returns
A failed result containing the typed prepare-commit failure message.
Sourcepub fn prepare_failed_with_callback_type(
callback_type: &'static str,
msg: impl Display,
) -> Self
pub fn prepare_failed_with_callback_type( callback_type: &'static str, msg: impl Display, ) -> Self
Builds a failed result with ExecutorError::PrepareFailed and explicit
callback type metadata.
The callback type can later be read from
ExecutorError::callback_type.
§Parameters
callback_type- Callback type tag, e.g."prepare".msg- Failure message.
§Returns
A failed result containing the typed prepare failure message.
Sourcepub fn prepare_rollback_failed(
original: impl Display,
rollback: impl Display,
) -> Self
pub fn prepare_rollback_failed( original: impl Display, rollback: impl Display, ) -> Self
Builds a failed result with ExecutorError::PrepareRollbackFailed.
§Parameters
original- Original failure that triggered prepare rollback.rollback- Failure produced by the rollback action.
§Returns
A failed result containing both original and rollback messages.
Sourcepub fn prepare_rollback_failed_with_callback_type(
rollback_callback_type: &'static str,
original: impl Display,
rollback: impl Display,
) -> Self
pub fn prepare_rollback_failed_with_callback_type( rollback_callback_type: &'static str, original: impl Display, rollback: impl Display, ) -> Self
Builds a failed result with ExecutorError::PrepareRollbackFailed and
explicit callback type metadata for the rollback action.
The rollback callback type can later be read from
ExecutorError::callback_type.
§Parameters
rollback_callback_type- Callback type tag, e.g."prepare_rollback".original- Original failure that triggered prepare rollback.rollback- Failure produced by the rollback action.
§Returns
A failed result containing both original and typed rollback messages.
Sourcepub fn from_executor_error(err: ExecutorError<E>) -> Self
pub fn from_executor_error(err: ExecutorError<E>) -> Self
Wraps an arbitrary ExecutorError as ExecutionResult::Failed.
§Parameters
err- Executor error to store in the failed result.
§Returns
A failed result containing err.
Sourcepub fn is_success(&self) -> bool
pub fn is_success(&self) -> bool
Examples found in repository?
29fn main() -> Result<(), Box<dyn std::error::Error>> {
30 // Create shared state
31 let running = Arc::new(AtomicBool::new(false));
32 let data = ArcMutex::new(42);
33
34 println!("Initial state: running = {}", running.load(Ordering::Acquire));
35 println!("Initial data: {}", data.read(|d| *d));
36
37 let executor = DoubleCheckedLockExecutor::builder()
38 .on(data.clone())
39 .when({
40 let running = running.clone();
41 move || running.load(Ordering::Acquire)
42 })
43 .build();
44
45 // Try to execute when service is not running (should fail)
46 let result = executor
47 .call_with(|value: &mut i32| {
48 *value += 1;
49 Ok::<_, std::io::Error>(*value)
50 })
51 .get_result();
52
53 if result.is_success() {
54 println!("Unexpected success: {}", result.unwrap());
55 } else {
56 println!("Expected failure: Condition not met.");
57 }
58
59 // Start the service
60 running.store(true, Ordering::Release);
61 println!("Service started: running = {}", running.load(Ordering::Acquire));
62
63 // Now execute should succeed
64 let result = executor
65 .call_with(|value: &mut i32| {
66 *value += 1;
67 Ok::<_, std::io::Error>(*value)
68 })
69 .get_result();
70
71 if result.is_success() {
72 println!("Success: new value = {}", result.unwrap());
73 } else {
74 println!("Unexpected failure: {:?}", result);
75 }
76
77 // Verify the data was updated
78 println!("Final data: {}", data.read(|d| *d));
79
80 // Stop the service
81 running.store(false, Ordering::Release);
82 println!("Service stopped: running = {}", running.load(Ordering::Acquire));
83
84 // Try to execute when service is stopped (should fail)
85 let result = executor
86 .call_with(|value: &mut i32| {
87 *value += 1;
88 Ok::<_, std::io::Error>(*value)
89 })
90 .get_result();
91
92 if result.is_success() {
93 println!("Unexpected success: {}", result.unwrap());
94 } else {
95 println!("Expected failure: Condition not met.");
96 }
97
98 Ok(())
99}Sourcepub fn is_unmet(&self) -> bool
pub fn is_unmet(&self) -> bool
Checks if the condition was not met.
§Returns
true if this result is ExecutionResult::ConditionNotMet.
Sourcepub fn into_result(self) -> Result<Option<T>, ExecutorError<E>>
pub fn into_result(self) -> Result<Option<T>, ExecutorError<E>>
Converts the result to a standard Result
§Returns
Ok(Some(T))- If execution was successfulOk(None)- If condition was not metErr(ExecutorError<E>)- If execution failed
§Errors
Returns the stored ExecutorError when this value is
ExecutionResult::Failed.
Source§impl<T, E> ExecutionResult<T, E>where
E: Display,
impl<T, E> ExecutionResult<T, E>where
E: Display,
Sourcepub fn unwrap(self) -> T
pub fn unwrap(self) -> T
Unwraps the success value, panicking if not successful
§Returns
The success value stored in ExecutionResult::Success.
§Panics
Panics if this result is ExecutionResult::ConditionNotMet or
ExecutionResult::Failed.
Examples found in repository?
29fn main() -> Result<(), Box<dyn std::error::Error>> {
30 // Create shared state
31 let running = Arc::new(AtomicBool::new(false));
32 let data = ArcMutex::new(42);
33
34 println!("Initial state: running = {}", running.load(Ordering::Acquire));
35 println!("Initial data: {}", data.read(|d| *d));
36
37 let executor = DoubleCheckedLockExecutor::builder()
38 .on(data.clone())
39 .when({
40 let running = running.clone();
41 move || running.load(Ordering::Acquire)
42 })
43 .build();
44
45 // Try to execute when service is not running (should fail)
46 let result = executor
47 .call_with(|value: &mut i32| {
48 *value += 1;
49 Ok::<_, std::io::Error>(*value)
50 })
51 .get_result();
52
53 if result.is_success() {
54 println!("Unexpected success: {}", result.unwrap());
55 } else {
56 println!("Expected failure: Condition not met.");
57 }
58
59 // Start the service
60 running.store(true, Ordering::Release);
61 println!("Service started: running = {}", running.load(Ordering::Acquire));
62
63 // Now execute should succeed
64 let result = executor
65 .call_with(|value: &mut i32| {
66 *value += 1;
67 Ok::<_, std::io::Error>(*value)
68 })
69 .get_result();
70
71 if result.is_success() {
72 println!("Success: new value = {}", result.unwrap());
73 } else {
74 println!("Unexpected failure: {:?}", result);
75 }
76
77 // Verify the data was updated
78 println!("Final data: {}", data.read(|d| *d));
79
80 // Stop the service
81 running.store(false, Ordering::Release);
82 println!("Service stopped: running = {}", running.load(Ordering::Acquire));
83
84 // Try to execute when service is stopped (should fail)
85 let result = executor
86 .call_with(|value: &mut i32| {
87 *value += 1;
88 Ok::<_, std::io::Error>(*value)
89 })
90 .get_result();
91
92 if result.is_success() {
93 println!("Unexpected success: {}", result.unwrap());
94 } else {
95 println!("Expected failure: Condition not met.");
96 }
97
98 Ok(())
99}