Common/Environment/HasEnvironment.rs
1//! # HasEnvironment Trait
2//!
3//! Defines the `HasEnvironment` trait for any context-holding object that
4//! contains and provides access to an `Environment`.
5
6use std::sync::Arc;
7
8use super::Environment::Environment;
9
10/// A generic trait for any type that holds a shared, reference-counted pointer
11/// to an `Environment`.
12///
13/// This is primarily implemented by the `ApplicationRunTime`, which needs to
14/// provide access to the `MountainEnvironment` it manages so that
15/// `ActionEffect`s can be executed with the correct context and capabilities.
16pub trait HasEnvironment {
17 /// The specific, concrete type of the `Environment` this struct holds.
18 type EnvironmentType: Environment;
19
20 /// Gets a shared, reference-counted pointer to the environment.
21 fn GetEnvironment(&self) -> Arc<Self::EnvironmentType>;
22}
23
24/// A blanket implementation for `Arc<T>`. This allows code to treat
25/// an `Arc<TRunTime>` as if it were `TRunTime` for the purpose of getting the
26/// environment. This is required by the blanket `impl ApplicationRunTime for
27/// Arc<TRunTime>` which has a `where Self: HasEnvironment` bound.
28impl<T:HasEnvironment> HasEnvironment for Arc<T> {
29 type EnvironmentType = T::EnvironmentType;
30
31 fn GetEnvironment(&self) -> Arc<Self::EnvironmentType> { (**self).GetEnvironment() }
32}