NodeImpl

Trait NodeImpl 

Source
pub trait NodeImpl:
    Send
    + Sync
    + Debug
    + 'static {
    type Config: DeserializeOwned + Send + Sync + Debug;

    // Required methods
    fn init<'async_trait>(
        config: Self::Config,
    ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>
       where Self: Sized + 'async_trait;
    fn run<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        ctx: &'life1 Context,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait,
             'life1: 'async_trait;

    // Provided methods
    fn shutdown<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn health_check<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = HealthStatus> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

Core trait that all Mecha10 nodes must implement.

This trait defines the lifecycle of a node:

  1. init() - Initialize the node with configuration
  2. run() - Main execution loop
  3. shutdown() - Graceful shutdown
  4. health_check() - Report health status

§Example

use mecha10::prelude::*;

#[derive(Debug)]
struct MyNode {
    config: MyConfig,
}

#[derive(Debug, Deserialize)]
struct MyConfig {
    rate: f32,
}

#[async_trait]
impl NodeImpl for MyNode {
    type Config = MyConfig;

    async fn init(config: Self::Config) -> Result<Self> {
        info!("Initializing node with rate: {}", config.rate);
        Ok(Self { config })
    }

    async fn run(&mut self, ctx: &Context) -> Result<()> {
        let mut images = ctx.subscribe(sensor::CAMERA_RGB).await?;

        while let Some(image) = images.recv().await {
            // Process image
            info!("Received image: {}x{}", image.width, image.height);
        }

        Ok(())
    }
}

Required Associated Types§

Source

type Config: DeserializeOwned + Send + Sync + Debug

Configuration type for this node

Required Methods§

Source

fn init<'async_trait>( config: Self::Config, ) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>
where Self: Sized + 'async_trait,

Initialize the node with the given configuration.

This is called once when the node starts. Use this to:

  • Load configuration
  • Initialize resources (models, connections, etc.)
  • Validate prerequisites
§Errors

Return an error if initialization fails. The node will not start.

Source

fn run<'life0, 'life1, 'async_trait>( &'life0 mut self, ctx: &'life1 Context, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait,

Main execution loop for the node.

This is where your node’s business logic lives. Typical patterns:

  • Subscribe to topics and process messages
  • Run periodic tasks
  • Publish results

This method should run indefinitely until:

  • The context is cancelled (shutdown requested)
  • An unrecoverable error occurs
§Errors

Return an error if the node encounters an unrecoverable error. The node will transition to Error state and shutdown.

Provided Methods§

Source

fn shutdown<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Graceful shutdown hook.

Called when the node is shutting down. Use this to:

  • Close connections
  • Flush buffers
  • Save state
  • Release resources

The default implementation does nothing.

§Errors

Errors during shutdown are logged but don’t prevent shutdown.

Source

fn health_check<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = HealthStatus> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Health check for monitoring.

Called periodically to check node health. Use this to:

  • Report current state
  • Check resource usage
  • Validate connections

The default implementation returns healthy status.

Implementors§