New synchronous Rust Maelstrom crate

I’ve been working through the challenges in Rust, and been writing my own Maelstrom crate as I went along. Now that I’ve got RPC and a KV proxy working, I think it’s ready for showing its face in public:

Features:

  • no async, only three external deps (all of which are Serde-related), builds in less than a second
  • almost no boilerplate (the complete echo client is in the readme, it’s like fifteen lines)
  • includes a KV proxy with read, write, and cas methods that return a Result<Option<serde_json::Value>, crate::ErrorCode>

I wanted it to be as simple as possible to use, and I think it nails it.

Echo:

use nebkor_maelstrom::{Body, Message, Node, Runner};

struct Echo;

impl Node for Echo {
    fn handle(&mut self, runner: &Runner, msg: Message) {
        let typ = &msg.body.typ;
        if typ.as_str() == "echo" {
            let body = Body::from_type("echo_ok").with_payload(msg.body.payload.clone());
            runner.reply(&msg, body);
        }
    }
}

fn main() {
    let node = Echo;

    let runner = Runner::new(node);

    runner.run(None);
}
1 Like

And here’s an example of passing an initialization function to the runner, from the g-counter client:

fn main() {
    let node = Counter;

    let runner = Runner::new(node);

    let on_init = |rnr: &Runner| {
        let kv = Kv::seq();
        let _ = kv.cas(rnr, KEY, 0i64.into(), 0i64.into(), true);
    };

    let on_init = Box::new(on_init);

    runner.run(Some(on_init));
}

This is now available at crates.io:

https://crates.io/crates/nebkor-maelstrom

cargo add nebkor-maelstrom

Now that I’ve finished all the challenges using it, I’ve released the 1.0 :slight_smile: