How to block until one of the two receivers has data available?

I am new to Rust and trying to set up a unidirectional node graph using Senderand Receiverfrom std::sync::mpsc. Right now it works for me when each node has exactly one parent and one child, for example:

fn run(mut self) {
    let parent = self.parent.unwrap();
    loop {
        match parent.recv() {
            Ok(input) => {
                self.value = (self.next_value)(input);
                match self.kid {
                    Some(ref k) => { k.send(self.value).unwrap(); },
                    None => {}
                }
            },
            Err(_) => {}
        }
    }

}

But what I really want to do is have two parent nodes where the function is evaluated next_value, and the child nodes are notified when either the parent node sends a different value. I can't just use the lock method recv()twice, and using a poll and a non-blocking call try_recv()will probably work, but it seems really inefficient.

Is there a better way to do this?

+4
source share
1

select!()

fn run(mut self) {
    // obtain parents
    loop {
        select! {
           resp = parent1.recv() => match resp {...}
           resp = parent2.recv() => match resp {...}
        }
    }

}

, mpsc::Select

+6

All Articles