rename 'promises' to 'futures' for rpc returns
This commit is contained in:
parent
0c4496ae07
commit
ad1b8ccb31
4 changed files with 11 additions and 10 deletions
|
@ -1,4 +1,4 @@
|
|||
imports_granularity = "Crate"
|
||||
group_imports = "StdExternalCrate"
|
||||
wrap_comments = true
|
||||
edition = "2021"
|
||||
edition = "2024"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "nebkor-maelstrom"
|
||||
edition = "2021"
|
||||
version = "1.0.1"
|
||||
edition = "2024"
|
||||
version = "1.1.0"
|
||||
license-file = "LICENSE.md"
|
||||
readme = "README.md"
|
||||
description = "An easy-to-use and synchronous client for creating Maelstrom distributed clients."
|
||||
|
|
|
@ -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.
|
||||
#[derive(Debug, Default, Clone)]
|
||||
|
|
13
src/lib.rs
13
src/lib.rs
|
@ -2,9 +2,9 @@ use std::{
|
|||
collections::HashMap,
|
||||
io::{BufRead, Write},
|
||||
sync::{
|
||||
atomic::{AtomicU64, Ordering},
|
||||
mpsc::{channel, Receiver, Sender},
|
||||
Arc, Mutex, OnceLock,
|
||||
atomic::{AtomicU64, Ordering},
|
||||
mpsc::{Receiver, Sender, channel},
|
||||
},
|
||||
thread::{self},
|
||||
};
|
||||
|
@ -18,7 +18,7 @@ pub mod kv;
|
|||
|
||||
pub type NodeyNodeFace = Arc<Mutex<dyn Node>>;
|
||||
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 trait Node {
|
||||
|
@ -57,6 +57,7 @@ impl Runner {
|
|||
///
|
||||
/// ```no_run
|
||||
/// use nebkor_maelstrom::{Body, Message, Node, Runner};
|
||||
///
|
||||
/// struct Foo;
|
||||
/// 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
|
||||
/// 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 (tx, rx) = channel();
|
||||
{
|
||||
let msg_id = msg.body.msg_id;
|
||||
let mut g = self.promises.lock().unwrap();
|
||||
g.insert(msg_id, tx);
|
||||
let mut promises = self.promises.lock().unwrap();
|
||||
promises.insert(msg_id, tx);
|
||||
}
|
||||
self.outbound_tx.get().unwrap().send(msg).unwrap();
|
||||
rx
|
||||
|
|
Loading…
Reference in a new issue