Skip to content

Commit b363f00

Browse files
authored
feat: add Client::notify (#52)
This adds `Client::notify`, which was the one method we were still missing for the different kind of channels: The case where there is neither a sender nor a receiver, i.e. where the server cannot reply, only a single message is sent from client to server.
1 parent 283d457 commit b363f00

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/lib.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
#![cfg_attr(quicrpc_docsrs, feature(doc_cfg))]
7979
use std::{fmt::Debug, future::Future, io, marker::PhantomData, ops::Deref, result};
8080

81-
use channel::{mpsc, oneshot};
81+
use channel::{mpsc, none::NoSender, oneshot};
8282
/// Processes an RPC request enum and generates trait implementations for use with `irpc`.
8383
///
8484
/// This attribute macro may be applied to an enum where each variant represents
@@ -829,6 +829,25 @@ where
829829
}
830830
}
831831

832+
/// Tuple conversion from inner message to a WithChannels struct without channels
833+
impl<I, S> From<(I,)> for WithChannels<I, S>
834+
where
835+
I: Channels<S, Rx = NoReceiver, Tx = NoSender>,
836+
S: Service,
837+
{
838+
fn from(inner: (I,)) -> Self {
839+
let (inner,) = inner;
840+
Self {
841+
inner,
842+
tx: NoSender,
843+
rx: NoReceiver,
844+
#[cfg(feature = "spans")]
845+
#[cfg_attr(quicrpc_docsrs, doc(cfg(feature = "spans")))]
846+
span: tracing::Span::current(),
847+
}
848+
}
849+
}
850+
832851
/// Deref so you can access the inner fields directly.
833852
///
834853
/// If the inner message has fields named `tx`, `rx` or `span`, you need to use the
@@ -1082,6 +1101,32 @@ impl<S: Service> Client<S> {
10821101
Ok((update_tx, res_rx))
10831102
}
10841103
}
1104+
1105+
/// Performs a request for which the server returns nothing.
1106+
///
1107+
/// The returned future completes once the message is sent.
1108+
pub fn notify<Req>(&self, msg: Req) -> impl Future<Output = Result<()>> + Send + 'static
1109+
where
1110+
S: From<Req>,
1111+
S::Message: From<WithChannels<Req, S>>,
1112+
Req: Channels<S, Tx = NoSender, Rx = NoReceiver>,
1113+
{
1114+
let request = self.request();
1115+
async move {
1116+
match request.await? {
1117+
Request::Local(request) => {
1118+
request.send((msg,)).await?;
1119+
}
1120+
#[cfg(not(feature = "rpc"))]
1121+
Request::Remote(_request) => unreachable!(),
1122+
#[cfg(feature = "rpc")]
1123+
Request::Remote(request) => {
1124+
let (_tx, _rx) = request.write(msg).await?;
1125+
}
1126+
};
1127+
Ok(())
1128+
}
1129+
}
10851130
}
10861131

10871132
#[derive(Debug)]

0 commit comments

Comments
 (0)