Common/Effect/
ApplicationRunTime.rs

1//! # ApplicationRunTime Trait
2//!
3//! Defines the core `ApplicationRunTimeTrait`, which is the contract for any
4//! "engine" capable of executing `ActionEffect`s.
5
6use std::sync::Arc;
7
8use async_trait::async_trait;
9
10use super::ActionEffect::ActionEffect;
11use crate::{
12	Environment::{HasEnvironment::HasEnvironment, Requires::Requires},
13	Error::CommonError::CommonError,
14};
15
16/// The core trait for any runtime capable of executing `ActionEffect`s.
17#[async_trait]
18pub trait ApplicationRunTime: HasEnvironment + Send + Sync + 'static {
19	/// Executes an effect using the environment provided by the runtime.
20	///
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>(
24		&self,
25
26		Effect:ActionEffect<Arc<TCapabilityProvider>, TError, TOutput>,
27	) -> Result<TOutput, TError>
28	where
29		TCapabilityProvider: ?Sized + Send + Sync + 'static,
30		Self::EnvironmentType: Requires<TCapabilityProvider>,
31		TError: From<CommonError> + Send + Sync + 'static,
32		TOutput: Send + Sync + 'static;
33}
34
35/// A blanket implementation that allows a shared `Arc` of a runtime to also be
36/// used as a runtime.
37#[async_trait]
38impl<TRunTime:ApplicationRunTime> ApplicationRunTime for Arc<TRunTime> {
39	async fn Run<TCapabilityProvider, TError, TOutput>(
40		&self,
41
42		Effect:ActionEffect<Arc<TCapabilityProvider>, TError, TOutput>,
43	) -> Result<TOutput, TError>
44	where
45		TCapabilityProvider: ?Sized + Send + Sync + 'static,
46		Self::EnvironmentType: Requires<TCapabilityProvider>,
47		TError: From<CommonError> + Send + Sync + 'static,
48		TOutput: Send + Sync + 'static, {
49		(**self).Run(Effect).await
50	}
51}