pub struct Context<'a> { /* private fields */ }
Expand description
Stores type information about an entry inside of a TraitMap
Must be cast to a TypedContext using the .downcast()
or .try_downcast()
methods for adding or removing traits from the map.
Implementations§
source§impl<'a> Context<'a>
impl<'a> Context<'a>
sourcepub fn downcast<T>(self) -> TypedContext<'a, T>where
T: 'static,
pub fn downcast<T>(self) -> TypedContext<'a, T>where
T: 'static,
Downcast into the concrete TypedContext.
Panics
This method panics if the type parameter T
does not match the concrete type.
Examples
impl TraitMapEntry for MyStruct {
fn on_create<'a>(&mut self, context: Context<'a>) {
context
.downcast::<Self>()
.add_trait::<dyn ExampleTrait>()
.add_trait::<dyn ExampleTraitTwo>();
}
}
sourcepub fn try_downcast<T>(self) -> Result<TypedContext<'a, T>, Self>where
T: 'static,
pub fn try_downcast<T>(self) -> Result<TypedContext<'a, T>, Self>where
T: 'static,
Try to downcast into a concrete TypedContext.
Errors
Returns None
if the type parameter T
does not match the concrete type.
Examples
impl TraitMapEntry for MyStruct {
fn on_create<'a>(&mut self, context: Context<'a>) {
if let Some(context) = context.try_downcast::<Self>() {
context
.add_trait::<dyn ExampleTrait>()
.add_trait::<dyn ExampleTraitTwo>();
}
}
}