Skip to content

Commit 6c74d27

Browse files
authored
Implement basic combinators map, filter and filter_map for irpc channels. (#68)
I find these useful quite a few times. They already exist on Sink and Stream of course, but often you don't want to convert your senders and receivers into Sink / Stream and lose the extra fns. Also Sink API is just weird! In the fns that filter (with_filter, with_filter_map, filter, filter_map) the item is just dropped if it does not match the filter. I think for these channels this is uncontroversial, besides SinkExt does the same. It does not produce an error if you use e.g. [with_flat_map](https://docs.rs/futures/latest/futures/sink/trait.SinkExt.html#method.with_flat_map) to map to an empty stream. An additional change is that the bounds for crate::channel::mpsc::Sender/Receiver are reduced so you can now use a crate::channel::mpsc::Sender/Receiver as the channel to an actor even for messages that are not serializable.
1 parent 99df1dd commit 6c74d27

File tree

3 files changed

+296
-89
lines changed

3 files changed

+296
-89
lines changed

examples/compute.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ struct Multiply {
5555

5656
// The actor that processes requests
5757
struct ComputeActor {
58-
recv: tokio::sync::mpsc::Receiver<ComputeMessage>,
58+
recv: irpc::channel::mpsc::Receiver<ComputeMessage>,
5959
}
6060

6161
impl ComputeActor {
6262
pub fn local() -> ComputeApi {
63-
let (tx, rx) = tokio::sync::mpsc::channel(128);
63+
let (tx, rx) = irpc::channel::mpsc::channel(128);
6464
let actor = Self { recv: rx };
6565
n0_future::task::spawn(actor.run());
6666
ComputeApi {
@@ -69,7 +69,7 @@ impl ComputeActor {
6969
}
7070

7171
async fn run(mut self) {
72-
while let Some(msg) = self.recv.recv().await {
72+
while let Ok(Some(msg)) = self.recv.recv().await {
7373
n0_future::task::spawn(async move {
7474
if let Err(cause) = Self::handle(msg).await {
7575
eprintln!("Error: {cause}");

0 commit comments

Comments
 (0)