|
1 | 1 | //! # ApplicationRunTime Trait |
2 | 2 | //! |
3 | | -//! Defines the `ApplicationRunTime` trait, which specifies the contract for any |
4 | | -//! "engine" responsible for executing `ActionEffect`s. |
| 3 | +//! Defines the core `ApplicationRunTimeTrait`, which is the contract for any |
| 4 | +//! "engine" capable of executing `ActionEffect`s. |
5 | 5 |
|
6 | 6 | use std::sync::Arc; |
7 | 7 |
|
8 | 8 | use async_trait::async_trait; |
9 | 9 |
|
10 | 10 | use super::ActionEffect::ActionEffect; |
11 | | -use crate::Environment::{HasEnvironment::HasEnvironment, Requires::Requires}; |
| 11 | +use crate::{ |
| 12 | + Environment::{HasEnvironment::HasEnvironment, Requires::Requires}, |
| 13 | + Error::CommonError::CommonError, |
| 14 | +}; |
12 | 15 |
|
13 | | -/// A trait that defines the core contract for an application's runtime engine. |
14 | | -/// |
15 | | -/// An `ApplicationRunTime` is the component that bridges the declarative world |
16 | | -/// of `ActionEffect`s with the concrete world of execution. It is responsible |
17 | | -/// for taking an effect, providing it with the necessary capabilities from its |
18 | | -/// `Environment`, and running the resulting asynchronous operation to |
19 | | -/// completion. |
| 16 | +/// The core trait for any runtime capable of executing `ActionEffect`s. |
20 | 17 | #[async_trait] |
21 | 18 | pub trait ApplicationRunTime: HasEnvironment + Send + Sync + 'static { |
22 | | - /// Executes an `ActionEffect` that requires a specific capability from the |
23 | | - /// environment. |
| 19 | + /// Executes an effect using the environment provided by the runtime. |
24 | 20 | /// |
25 | | - /// This method is the heart of the effect execution system. It dynamically |
26 | | - /// resolves the required capability (e.g., a trait object like |
27 | | - /// `Arc<dyn FileSystemReader>`) from its managed environment and provides |
28 | | - /// it to the effect's encapsulated function, which is then awaited. |
29 | | - async fn Run<TCapability, TError, TOutput>( |
| 21 | + /// The runtime is responsible for acquiring the necessary capability from |
| 22 | + /// its environment and passing it to the effect's execution logic. |
| 23 | + async fn Run<TCapabilityProvider, TError, TOutput>( |
30 | 24 | &self, |
31 | | - Effect:ActionEffect<Arc<TCapability>, TError, TOutput>, |
| 25 | + Effect:ActionEffect<Arc<TCapabilityProvider>, TError, TOutput>, |
32 | 26 | ) -> Result<TOutput, TError> |
33 | 27 | where |
34 | | - TCapability: ?Sized + Send + Sync, |
35 | | - Self::EnvironmentType: Requires<Arc<TCapability>>, |
36 | | - TError: Send + Sync + 'static, |
| 28 | + TCapabilityProvider: ?Sized + Send + Sync + 'static, |
| 29 | + Self::EnvironmentType: Requires<TCapabilityProvider>, |
| 30 | + TError: From<CommonError> + Send + Sync + 'static, |
37 | 31 | TOutput: Send + Sync + 'static; |
38 | 32 | } |
| 33 | + |
| 34 | +/// A blanket implementation that allows a shared `Arc` of a runtime to also be |
| 35 | +/// used as a runtime. |
| 36 | +#[async_trait] |
| 37 | +impl<TRunTime:ApplicationRunTime> ApplicationRunTime for Arc<TRunTime> { |
| 38 | + async fn Run<TCapabilityProvider, TError, TOutput>( |
| 39 | + &self, |
| 40 | + Effect:ActionEffect<Arc<TCapabilityProvider>, TError, TOutput>, |
| 41 | + ) -> Result<TOutput, TError> |
| 42 | + where |
| 43 | + TCapabilityProvider: ?Sized + Send + Sync + 'static, |
| 44 | + Self::EnvironmentType: Requires<TCapabilityProvider>, |
| 45 | + TError: From<CommonError> + Send + Sync + 'static, |
| 46 | + TOutput: Send + Sync + 'static, { |
| 47 | + (**self).Run(Effect).await |
| 48 | + } |
| 49 | +} |
0 commit comments