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 const fn new() -> Environment
pub const fn new() -> Environment
Creates a new empty environment.
Sourcepub fn store<K, V>(self, value: V) -> Environmentwhere
K: 'static,
V: 'static,
pub fn store<K, V>(self, value: V) -> Environmentwhere
K: 'static,
V: 'static,
Sourcepub fn query<K, V>(&self) -> Option<&V>where
K: 'static,
V: 'static,
pub fn query<K, V>(&self) -> Option<&V>where
K: 'static,
V: 'static,
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 Environment
pub fn install(&mut self, plugin: impl Plugin) -> &mut Environment
Installs a plugin into the environment.
Plugins can register values or modifiers that will be available to all views.
Sourcepub fn insert<T>(&mut self, value: T)where
T: 'static,
pub fn insert<T>(&mut self, value: T)where
T: 'static,
Inserts a value into the environment.
If a value of the same type already exists, it will be replaced.
Sourcepub fn insert_hook<T, V>(
&mut self,
hook: impl Fn(&Environment, T) -> V + 'static,
)where
T: ViewConfiguration,
V: View,
pub fn insert_hook<T, V>(
&mut self,
hook: impl Fn(&Environment, T) -> V + 'static,
)where
T: ViewConfiguration,
V: View,
Inserts a view configuration hook into the environment.
Hooks allow you to intercept and modify view configurations globally.
Sourcepub fn with<T>(&mut self, value: T) -> &mut Environmentwhere
T: 'static,
pub fn with<T>(&mut self, value: T) -> &mut Environmentwhere
T: 'static,
Adds a value to the environment and returns the modified environment.
This is a fluent interface for chaining multiple additions.
Sourcepub fn get<T>(&self) -> Option<&T>where
T: 'static,
pub fn get<T>(&self) -> Option<&T>where
T: 'static,
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§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§fn default() -> Environment
fn default() -> Environment
Source§impl Extractor for Environment
impl Extractor for Environment
Source§fn extract(env: &Environment) -> Result<Environment, Error>
fn extract(env: &Environment) -> Result<Environment, Error>
Extracts the Environment itself by creating a clone.
impl MetadataKey for Environment
Auto Trait Implementations§
impl Freeze for Environment
impl !RefUnwindSafe for Environment
impl !Send for Environment
impl !Sync for Environment
impl Unpin for Environment
impl !UnwindSafe for Environment
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)