rename 'promises' to 'futures' for rpc returns

This commit is contained in:
Joe Ardent 2025-07-16 14:25:36 -07:00
parent 0c4496ae07
commit ad1b8ccb31
4 changed files with 11 additions and 10 deletions

View file

@ -1,4 +1,4 @@
imports_granularity = "Crate" imports_granularity = "Crate"
group_imports = "StdExternalCrate" group_imports = "StdExternalCrate"
wrap_comments = true wrap_comments = true
edition = "2021" edition = "2024"

View file

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

View file

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

View file

@ -2,9 +2,9 @@ use std::{
collections::HashMap, collections::HashMap,
io::{BufRead, Write}, io::{BufRead, Write},
sync::{ sync::{
atomic::{AtomicU64, Ordering},
mpsc::{channel, Receiver, Sender},
Arc, Mutex, OnceLock, Arc, Mutex, OnceLock,
atomic::{AtomicU64, Ordering},
mpsc::{Receiver, Sender, channel},
}, },
thread::{self}, thread::{self},
}; };
@ -18,7 +18,7 @@ pub mod kv;
pub type NodeyNodeFace = Arc<Mutex<dyn Node>>; pub type NodeyNodeFace = Arc<Mutex<dyn Node>>;
pub type OnInit = Box<dyn Fn(&Runner)>; pub type OnInit = Box<dyn Fn(&Runner)>;
pub type RpcPromise = Receiver<Message>; pub type RpcFuture = Receiver<Message>;
pub type RpcResult = std::result::Result<Option<Value>, ErrorCode>; pub type RpcResult = std::result::Result<Option<Value>, ErrorCode>;
pub trait Node { pub trait Node {
@ -57,6 +57,7 @@ impl Runner {
/// ///
/// ```no_run /// ```no_run
/// use nebkor_maelstrom::{Body, Message, Node, Runner}; /// use nebkor_maelstrom::{Body, Message, Node, Runner};
///
/// struct Foo; /// struct Foo;
/// impl Node for Foo {fn handle(&mut self, _runner: &Runner, _msg: Message) { /* empty impl */ }} /// impl Node for Foo {fn handle(&mut self, _runner: &Runner, _msg: Message) { /* empty impl */ }}
/// ///
@ -126,13 +127,13 @@ impl Runner {
/// Returns a Receiver<Message> that will receive the reply from the /// Returns a Receiver<Message> that will receive the reply from the
/// request. /// request.
pub fn rpc(&self, dest: &str, body: Body) -> RpcPromise { pub fn rpc(&self, dest: &str, body: Body) -> RpcFuture {
let msg = self.mk_msg(dest, body); let msg = self.mk_msg(dest, body);
let (tx, rx) = channel(); let (tx, rx) = channel();
{ {
let msg_id = msg.body.msg_id; let msg_id = msg.body.msg_id;
let mut g = self.promises.lock().unwrap(); let mut promises = self.promises.lock().unwrap();
g.insert(msg_id, tx); promises.insert(msg_id, tx);
} }
self.outbound_tx.get().unwrap().send(msg).unwrap(); self.outbound_tx.get().unwrap().send(msg).unwrap();
rx rx