Engine Module API Reference
The engine module defines the core interfaces for how OSUI applications run, render, and execute commands. It provides abstractions for different rendering backends and includes a default console implementation.
Engine Trait
pub trait Engine<Output = ()> {
fn run<C: ComponentImpl + 'static>(&self, component: C) -> crate::Result<Output>;
fn init<C: ComponentImpl + 'static>(&self, component: C) -> Arc<Context>;
fn render(&self, cx: &Arc<Context>);
fn render_delay(&self);
fn render_view(&self, area: &Area, view: &View) -> DrawContext;
fn draw_context(&self, ctx: &DrawContext);
fn executor(&self) -> Arc<dyn CommandExecutor>;
}
The Engine trait is the primary interface for running an OSUI application. It abstracts away the specifics of how components are initialized, rendered, and how the application loop is managed.
Associated Types / Generics:
Output: A generic type that allowsrunto return different results. By default, it's(), but for specialized engines (likeBenchmark), it can return custom data.
Methods:
fn run<C: ComponentImpl + 'static>(&self, component: C) -> crate::Result<Output>- The main entry point to start the OSUI application loop. It takes the root component
C, initializes it, and continuously renders until a stop command is issued. ReturnsOk(())by default, or a customOutputfor specialized engines.
- The main entry point to start the OSUI application loop. It takes the root component
fn init<C: ComponentImpl + 'static>(&self, component: C) -> Arc<Context>- Initializes the rendering environment and creates the root
Contextfor the application's top-level component. This is typically called byrun.
- Initializes the rendering environment and creates the root
fn render(&self, cx: &Arc<Context>)- Performs a full render cycle for the given
Context. This usually involves clearing the screen, callingrender_viewfor the root, and thendraw_context.
- Performs a full render cycle for the given
fn render_delay(&self)- A hook for introducing a delay between render frames. The default implementation calls
crate::sleep(16)for approximately 60 frames per second.
- A hook for introducing a delay between render frames. The default implementation calls
fn render_view(&self, area: &Area, view: &View) -> DrawContext- Takes a
Viewand theAreait should render within, executing theView's closure to produce aDrawContextfilled withDrawInstructions.
- Takes a
fn draw_context(&self, ctx: &DrawContext)- Executes the drawing instructions contained within a
DrawContextto actually draw content to the rendering target (e.g., the terminal).
- Executes the drawing instructions contained within a
fn executor(&self) -> Arc<dyn CommandExecutor>- Returns the
CommandExecutorinstance used by this engine.
- Returns the
Command Trait
pub trait Command {
fn as_any(&self) -> &dyn Any;
}
The Command trait is implemented by types that represent actions or instructions that can be executed by the CommandExecutor. This trait enables a type-safe way to send commands between components and the engine.
fn as_any(&self) -> &dyn Any: Allows downcasting the command to its concrete type for pattern matching and execution.
CommandExecutor Trait
pub trait CommandExecutor: Send + Sync {
fn execute_command(&self, command: &Arc<dyn Command>) -> crate::Result<()>;
}
The CommandExecutor trait defines how commands are processed within the OSUI application. Engines provide their own implementations of this trait to handle system-level operations.
fn execute_command(&self, command: &Arc<dyn Command>) -> crate::Result<()>: Takes anArcwrappedCommandand executes it. Implementations typically usecommand.as_any().downcast_ref()to identify and process specific commands.
console Module: Console Engine and ConsoleExecutor
The console module provides OSUI's default, crossterm-based rendering engine.
Console Struct
pub struct Console {
threads: Mutex<Vec<Arc<dyn Fn(Arc<Context>) + Send + Sync>>>,
executor: Arc<ConsoleExecutor>,
}
The Console struct implements the Engine trait, specifically designed to render to a terminal using crossterm.
Methods:
fn new() -> Self: Creates a newConsoleengine.fn thread<F: Fn(Arc<Context>) + Send + Sync + 'static>(&self, run: F): Registers a closure to be run in a separate thread when the engine initializes. This is useful for background tasks or input polling that needs access to the mainContext.
Engine Trait Implementation:
The Console implements all methods of the Engine trait, handling terminal setup (raw mode, cursor hiding), screen clearing, crossterm cursor movements, and text output.
ConsoleExecutor Struct
pub struct ConsoleExecutor {
running: Mutex<bool>,
}
The ConsoleExecutor implements the CommandExecutor trait for the Console engine. It manages the running state of the application.
Methods:
fn is_running(self: &Arc<ConsoleExecutor>) -> bool: Checks if the application is currently running.fn stop(&self) -> crate::Result<()>: Sets the internalrunningflag tofalse, signaling theConsoleengine to terminate itsrunloop.
CommandExecutor Trait Implementation:
The ConsoleExecutor currently supports handling the commands::Stop command.
commands Module: Built-in Commands
The commands module defines simple, built-in commands for the engine.
Stop Command
pub struct Stop;
impl Command for Stop {
fn as_any(&self) -> &dyn std::any::Any;
}
A basic command used to signal the Engine to stop its main loop and exit the application.
benchmark Module: Benchmark Engine
The benchmark module provides a wrapper engine for performance testing.
BenchmarkResult Struct
pub struct BenchmarkResult {
pub average: u128,
pub min: u128,
pub max: u128,
pub total_render: u128,
pub total: u128,
}
Holds the statistical results of a benchmark run, including average, minimum, maximum, total render time, and total overall time in microseconds.
Benchmark<T: Engine> Struct
pub struct Benchmark<T: Engine>(T);
A wrapper around any other Engine that measures its rendering performance.
Methods:
fn new(engine: T) -> Self: Creates a newBenchmarkwrapper around an existingEngineinstance.
Engine<BenchmarkResult> Trait Implementation:
The Benchmark engine implements the Engine trait, but its run method performs multiple render cycles (e.g., 40 times), measures the duration of each, and returns a BenchmarkResult instead of (). It delegates all other Engine methods to the wrapped engine.
This comprehensive set of traits and implementations allows OSUI to be flexible regarding its rendering backend and extensible with custom command handling.
Next: Explore the Frontend API.