Struct sai::System[][src]

pub struct System<T> where
    T: ComponentRegistry
{ pub entrypoint: Option<TypeId>, // some fields omitted }
Expand description

A system is a collection of components + the ability to control the lifecycle of components in a way meeting the dependency requirement of components, e.g. start/stop them.


// Assume a RootRegistry is defined here
use sai::{System, Component, component_registry};

#[derive(Component)]
struct Foo {
}

component_registry!(RootRegistry, [Foo]);

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut system : System<RootRegistry> = System::new();
    println!("System starting up...");
    system.start().await;
    println!("System started.");

    // Waiting for Ctrl-c
    // signal::ctrl_c().await?;

    println!("System shutting down...");
    system.stop().await;
    println!("System shutted down.");
    Ok(())
}

Fields

entrypoint: Option<TypeId>

If this is set, then the system will only start components that can be reached by this entrypoint.

Implementations

Create a new system with a Component Registry

Example

use sai::{component_registry, System};
component_registry!(RootRegistry, [ component1, component2 ]);

let system: System<RootRegistry> = System::new();

Similar to System::new() but allow you to specified an entrypoint for the system. If an entrypoint is specified, it will become the topology root of component tree.

Create & start all components in the registry in a topological order. The topological order is automatically derived by system from analysing #[injected] macro attributes in component definitons.

The entrypoints will be automatically detected unless specifically specified.

following the example above:

system.start().await;

Stop and drop all components in a topological order in reverse to startup. A typical example used with tokio signal:

use tokio::signal;

...

// Waiting for Ctrl-c
signal::ctrl_c().await?;
println!("System shutting down...");
system.stop().await;
println!("System shutted down.");

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

Performs the conversion.

Performs the conversion.

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.