pub struct Environment { /* private fields */ }Expand description
An Environment stores a map of types to values.
Each type can have at most one value in the environment. The environment is used to pass contextual information from parent views to child views.
§Examples
use waterui_core::Environment;
let mut env = Environment::new();
env.insert(String::from("hello"));
// Get the value back
assert_eq!(env.get::<String>(), Some(&String::from("hello")));
// Remove the value
env.remove::<String>();
assert_eq!(env.get::<String>(), None);Implementations§
Source§impl Environment
impl Environment
Sourcepub fn query<K: 'static, V: 'static>(&self) -> Option<&V>
pub fn query<K: 'static, V: 'static>(&self) -> Option<&V>
Queries for a value in the environment indexed by type K.
§Returns
An optional reference to the stored value
Sourcepub fn install(&mut self, plugin: impl Plugin) -> &mut Self
pub fn install(&mut self, plugin: impl Plugin) -> &mut Self
Installs a plugin into the environment.
Plugins can register values or modifiers that will be available to all views.
Sourcepub fn insert<T: 'static>(&mut self, value: T)
pub fn insert<T: 'static>(&mut self, value: T)
Inserts a value into the environment.
If a value of the same type already exists, it will be replaced.
Sourcepub fn insert_hook<T: ViewConfiguration, V: View>(
&mut self,
hook: impl Fn(&Self, T) -> V + 'static,
)
pub fn insert_hook<T: ViewConfiguration, V: View>( &mut self, hook: impl Fn(&Self, T) -> V + 'static, )
Inserts a view configuration hook into the environment.
Hooks allow you to intercept and modify view configurations globally.
Sourcepub fn with<T: 'static>(&mut self, value: T) -> &mut Self
pub fn with<T: 'static>(&mut self, value: T) -> &mut Self
Adds a value to the environment and returns the modified environment.
This is a fluent interface for chaining multiple additions.
Sourcepub fn get<T: 'static>(&self) -> Option<&T>
pub fn get<T: 'static>(&self) -> Option<&T>
Retrieves a reference to a value from the environment by its type.
Returns None if no value of the requested type exists.
§Panics
This function will panic if a value of the requested type exists in the environment,
but the stored value cannot be downcast to the requested type. This should never happen
if only insert and with are used to add values.
Trait Implementations§
Source§impl Clone for Environment
impl Clone for Environment
Source§fn clone(&self) -> Environment
fn clone(&self) -> Environment
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more