rename 'encoded_message' to 'decoded_message'

This commit is contained in:
Joe Ardent 2024-11-10 12:11:58 -08:00
parent e5150af666
commit 0c4496ae07
4 changed files with 20 additions and 19 deletions

View file

@ -1,7 +1,7 @@
[package]
name = "nebkor-maelstrom"
edition = "2021"
version = "1.0.0"
version = "1.0.1"
license-file = "LICENSE.md"
readme = "README.md"
description = "An easy-to-use and synchronous client for creating Maelstrom distributed clients."

View file

@ -1,6 +1,4 @@
use crate::Value;
use crate::{check_err, mk_payload, Body, RpcResult, Runner};
use crate::{check_err, mk_payload, Body, RpcResult, Runner, Value};
/// A convenient way to do RPC with Maelstrom's KV services.
#[derive(Debug, Default, Clone)]

View file

@ -49,9 +49,9 @@ impl Runner {
}
}
/// Start processing messages from stdin and sending them to your node. The `on_init` argument
/// is an optional callback that will be called with `&self` after the `init` message from
/// Maelstrom has been processed.
/// Start processing messages from stdin and sending them to your node. The
/// `on_init` argument is an optional callback that will be called with
/// `&self` after the `init` message from Maelstrom has been processed.
///
/// # Examples
///
@ -88,9 +88,10 @@ impl Runner {
self.process_input(on_init);
}
/// Get a Sender that will send Messages to the node as input. Useful for triggering periodic
/// behavior from a separate thread, or for sending a Message to the node from `on_init`. See
/// the `broadcast` example for a use of it.
/// Get a Sender that will send Messages to the node as input. Useful for
/// triggering periodic behavior from a separate thread, or for sending
/// a Message to the node from `on_init`. See the `broadcast` example
/// for a use of it.
pub fn get_backdoor(&self) -> Sender<Message> {
self.backdoor.get().unwrap().clone()
}
@ -123,7 +124,8 @@ impl Runner {
self.outbound_tx.get().unwrap().send(msg).unwrap();
}
/// Returns a Receiver<Message> that will receive the reply from the request.
/// Returns a Receiver<Message> that will receive the reply from the
/// request.
pub fn rpc(&self, dest: &str, body: Body) -> RpcPromise {
let msg = self.mk_msg(dest, body);
let (tx, rx) = channel();
@ -164,8 +166,8 @@ impl Runner {
fn process_input(&self, on_init: Option<OnInit>) {
// for sending Messages to the node's inputs
let (encoded_input_tx, encoded_input_rx) = channel();
let _ = self.backdoor.get_or_init(|| encoded_input_tx.clone());
let (decoded_input_tx, decoded_input_rx) = channel();
let _ = self.backdoor.get_or_init(|| decoded_input_tx.clone());
// decouple stdin from processing
let proms = self.promises.clone();
@ -179,13 +181,13 @@ impl Runner {
promise.send(msg).unwrap();
} else {
// just let the node's `handle()` method handle it
encoded_input_tx.send(msg).unwrap();
decoded_input_tx.send(msg).unwrap();
}
}
});
// first Message is always `init`:
let msg = encoded_input_rx.recv().unwrap();
let msg = decoded_input_rx.recv().unwrap();
{
self.init(&msg);
let body = Body::from_type("init_ok");
@ -197,7 +199,7 @@ impl Runner {
// every other message is for the node's handle() method
let mut node = self.node.lock().unwrap();
for msg in encoded_input_rx {
for msg in decoded_input_rx {
node.handle(self, msg);
}
}

View file

@ -52,9 +52,10 @@ impl Message {
m
}
/// The Maelstrom type of a Body (and hence a Message) is just a string. This is for the sake of
/// ease of use for doing the Gossip Glomers challenges; this crate is not meant to be a real
/// network client framework.
/// The Maelstrom type of a Body (and hence a Message) is just a string.
/// This is for the sake of ease of use for doing the Gossip Glomers
/// challenges; this crate is not meant to be a real network client
/// framework.
pub fn typ(&self) -> &str {
self.body.typ.as_str()
}