Trait maelstrom::Node

source ·
pub trait Node: Sync + Send {
    // Required method
    fn process<'life0, 'async_trait>(
        &'life0 self,
        runtime: Runtime,
        request: Message
    ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait;
}

Required Methods§

source

fn process<'life0, 'async_trait>( &'life0 self, runtime: Runtime, request: Message ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>where Self: 'async_trait, 'life0: 'async_trait,

Main handler function that processes incoming requests.

Example:

use async_trait::async_trait;
use maelstrom::protocol::Message;
use maelstrom::{Node, Result, Runtime, done};

struct Handler {}

#[async_trait]
impl Node for Handler {
    async fn process(&self, runtime: Runtime, req: Message) -> Result<()> {
        if req.get_type() == "echo" {
            let echo = req.body.clone().with_type("echo_ok");
            return runtime.reply(req, echo).await;
        }

        // all other types are unsupported
        done(runtime, req)
    }
}

Implementors§