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

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:

Create an empty context with no Subcontexts.

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.

Access a specific type of Subcontext immutably.

Access a specific type of Subcontext mutably.

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

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.