Struct wolf_engine::Context
source · [−]pub struct Context { /* private fields */ }Expand description
Provides a dynamic storage container for global Engine state.
The context object essentially provides a dynamic container for Subcontext objects. Subcontexts store data used by the engine, engine modules, or the game. Specific Subcontexts can be dynamically added, and retrieved by type, at runtime, allowing for greatly improved flexibility, as any type implementing the Subcontext trait can be used. An AnyMap is used to achieve this behavior.
Examples
To create the default context, use Context::default();
let context = Context::default();Adding a Subcontext is done using the Context::add() method.
context.add(my_subcontext);The Subcontext can be accessed again using Context::get() or Context::get_mut().
// If you want an immutable reference:
if let Some(my_subcontext) = context.get::<MySubcontext>() {
// Do something with the Subcontext.
}
// If you want a mutable reference:
if let Some(my_subcontext_mut) = context.get_mut::<MySubcontext>() {
// Do something with the Subcontext.
}Implementations
sourceimpl Context
impl Context
sourcepub fn new() -> Self
pub fn new() -> Self
Create an instance of the default context.
The default context starts off with a few common Subcontexts. If this is not desirable, use Context::empty().
The default Subcontexts:
sourcepub fn empty() -> Self
pub fn empty() -> Self
Create an empty context with no Subcontexts.
sourcepub fn add<T: Subcontext>(
&mut self,
subcontext: T
) -> Result<(), ContextAlreadyExistsError>
pub fn add<T: Subcontext>(
&mut self,
subcontext: T
) -> Result<(), ContextAlreadyExistsError>
Add a Subcontext object.
This function ensures that only a single instance of each Subcontext type may
be added. For example: If you add an instance of SubcontextA, then later
attempt to add another instance of SubcontextA, this will result in an error.
A result is returned to indicate if the Subcontext was successfully added. An Ok indicates the context was added, and an Err indicates there is already an instance of the type added.
sourcepub fn get<T: Subcontext>(&self) -> Option<&T>
pub fn get<T: Subcontext>(&self) -> Option<&T>
Access a specific type of Subcontext immutably.
sourcepub fn get_mut<T: Subcontext>(&mut self) -> Option<&mut T>
pub fn get_mut<T: Subcontext>(&mut self) -> Option<&mut T>
Access a specific type of Subcontext mutably.
sourcepub fn remove<T: Subcontext>(&mut self)
pub fn remove<T: Subcontext>(&mut self)
Remove a specific type of Subcontext.
You should avoid removing a Subcontext unless you’re 100% sure no other parts of the code are depending on it. Removing a Subcontext will likely cause any code depending on it to panic or otherwise fail. As a general rule, avoid removing anything you didn’t add yourself.
Trait Implementations
Auto Trait Implementations
impl !RefUnwindSafe for Context
impl !Send for Context
impl !Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more