pub fn downcast_mut<T: ExecutionContext + 'static>(
context: &mut dyn ExecutionContext,
) -> Option<&mut T>Expand description
Attempt to downcast a mutable context reference to a concrete type
This is the mutable version of downcast_ref(). It allows command
handlers to modify the context state after downcasting.
§Type Parameters
T- The concrete type to downcast to. Must implementExecutionContextand have a'staticlifetime.
§Arguments
context- Mutable reference to the execution context trait object
§Returns
Some(&mut T)if the context is of typeTNoneif the context is a different type
§Example
use dynamic_cli::context::{ExecutionContext, downcast_mut};
use std::any::Any;
struct Counter {
value: u32,
}
impl ExecutionContext for Counter {
fn as_any(&self) -> &dyn Any { self }
fn as_any_mut(&mut self) -> &mut dyn Any { self }
}
fn increment(ctx: &mut dyn ExecutionContext) {
if let Some(counter) = downcast_mut::<Counter>(ctx) {
counter.value += 1;
}
}§Safety
This operation is safe because it uses Rust’s Any::downcast_mut,
which performs runtime type checking.