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 extending<T: 'static>(&self, value: T) -> Self
pub fn extending<T: 'static>(&self, value: T) -> Self
Returns a new environment that overlays a value on top of the current state.
This is an O(1) operation backed by structural sharing and avoids copying the underlying map.
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.
Sourcepub fn get_nth<T: 'static>(&self, index: usize) -> Option<&T>
pub fn get_nth<T: 'static>(&self, index: usize) -> Option<&T>
Retrieves the index-th visible value of type T, counting from the
nearest (most recently overlaid) value outwards.
get_nth(0) therefore always agrees with get.
This is primarily used by action extractors to support repeated
same-typed crate::extract::State values in a single handler, and
nearest-first is what gives those handlers positional correspondence:
.state(&a).state(&b) wraps b outermost, so b’s overlay is applied
first and a ends up nearest — and a is what the first State<T>
handler parameter must bind to.
Sourcepub fn get_or_insert_with<T: 'static, F: FnOnce() -> T>(&mut self, f: F) -> &T
pub fn get_or_insert_with<T: 'static, F: FnOnce() -> T>(&mut self, f: F) -> &T
Retrieves a reference to a value from the environment by its type, inserting a new value if it does not already exist.
The new value is created by calling the provided closure f.
§Panics
Panics if insertion succeeds but the inserted value cannot be retrieved
back as T, which indicates a corrupted environment entry.
Sourcepub fn extract<T: Extractor>(&self) -> Result<T, Error>
pub fn extract<T: Extractor>(&self) -> Result<T, Error>
Extracts a value from the environment using the Extractor trait.
This is a convenience method for extracting values that implement Extractor.
§Errors
Returns an error if extraction fails (e.g., value not found).
Sourcepub fn layered_on(&self, parent: &Self) -> Self
pub fn layered_on(&self, parent: &Self) -> Self
Replays this environment’s overlays on top of parent.
This preserves repeated same-typed overlay entries instead of collapsing them into a single visible value.
Trait Implementations§
Source§impl Clone for Environment
impl Clone for Environment
Source§fn clone(&self) -> Environment
fn clone(&self) -> Environment
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for Environment
impl Debug for Environment
Source§impl Default for Environment
impl Default for Environment
Source§impl Extractor for Environment
impl Extractor for Environment
Source§fn extract(env: &Environment) -> Result<Self, Error>
fn extract(env: &Environment) -> Result<Self, Error>
Extracts the Environment itself by creating a clone.
Source§fn extract_from_action(
env: &Environment,
state: &mut ExtractionState,
) -> Result<Self, Error>
fn extract_from_action( env: &Environment, state: &mut ExtractionState, ) -> Result<Self, Error>
Self from the given environment for
an action handler invocation. Read more