Module traits

Module traits 

Source
Expand description

Execution context traits

This module defines the core traits for managing execution context in CLI/REPL applications. The execution context is shared state that persists across command executions.

§Design Philosophy

The context system uses Rust’s type system to provide:

  • Type safety: Contexts are strongly typed
  • Flexibility: Each application defines its own context type
  • Thread safety: Contexts must be Send + Sync
  • Ergonomic downcasting: Helper methods for type conversion

§Example

use dynamic_cli::context::{ExecutionContext, downcast_mut};
use std::any::Any;

// Define your application's context
#[derive(Default)]
struct MyContext {
    counter: u32,
    data: Vec<String>,
}

// Implement the ExecutionContext trait
impl ExecutionContext for MyContext {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

// Use the context with downcasting
fn use_context(ctx: &mut dyn ExecutionContext) {
    // Downcast to concrete type
    if let Some(my_ctx) = downcast_mut::<MyContext>(ctx) {
        my_ctx.counter += 1;
        my_ctx.data.push("Hello".to_string());
    }
}

Traits§

ExecutionContext
Execution context trait

Functions§

downcast_mut
Attempt to downcast a mutable context reference to a concrete type
downcast_ref
Attempt to downcast a context reference to a concrete type