Common/Effect/ExecuteEffect.rs
1//! # ExecuteEffect Helper
2//!
3//! Defines the generic `ExecuteEffect` helper function, which provides a more
4//! ergonomic and readable way to run `ActionEffect`s within application logic.
5
6use std::sync::Arc;
7
8use super::{ActionEffect::ActionEffect, ApplicationRunTime::ApplicationRunTime};
9use crate::{Environment::Requires::Requires, Error::CommonError::CommonError};
10
11/// A generic effect execution helper that takes a runtime and an effect, and
12/// executes the effect using that runtime.
13///
14/// This function provides a more concise and declarative syntax for running
15/// effects, abstracting away the direct `RunTime.Run(Effect)` call and making
16/// the intent of the code clearer.
17///
18/// # Example
19///
20/// ```ignore
21// A function that reads a file using an effect.
22/// async fn ReadMyFile(RunTime: Arc<impl ApplicationRunTime>) ->
23/// Result<Vec<u8>, CommonError> {
24
25/// let ReadEffect =
26/// FileSystem::ReadFile(PathBuf::from("/path/to/file.txt"));
27
28/// ExecuteEffect(RunTime, ReadEffect).await
29/// }
30
31/// ```
32pub async fn ExecuteEffect<TRunTime, TCapabilityProvider, TError, TOutput>(
33 RunTime:Arc<TRunTime>,
34
35 Effect:ActionEffect<Arc<TCapabilityProvider>, TError, TOutput>,
36) -> Result<TOutput, TError>
37where
38 TRunTime: ApplicationRunTime,
39 TCapabilityProvider: ?Sized + Send + Sync + 'static,
40 TRunTime::EnvironmentType: Requires<TCapabilityProvider>,
41 TError: From<CommonError> + Send + Sync + 'static,
42 TOutput: Send + Sync + 'static, {
43 // The RunTime::Run method expects an effect whose closure takes the capability.
44 // This now matches perfectly.
45 RunTime.Run(Effect).await
46}