pub struct Receiver<T, E>{ /* private fields */ }Expand description
The receiving end of the channel.
Implementations§
Source§impl<T, E> Receiver<T, E>
impl<T, E> Receiver<T, E>
Sourcepub fn from_old(v: Receiver<CommMsg<T, E>>) -> Receiver<T, E>
pub fn from_old(v: Receiver<CommMsg<T, E>>) -> Receiver<T, E>
Converts an old-style receiver to a bchannel receiver.
Sourcepub fn into_inner(self) -> (Receiver<CommMsg<T, E>>, Option<E>)
pub fn into_inner(self) -> (Receiver<CommMsg<T, E>>, Option<E>)
Returns the old-style receiver along with the error. The error will be None unless this channel was closed by an error.
Sourcepub fn recv(&self) -> Option<T>
pub fn recv(&self) -> Option<T>
Returns the next message asyncrhonously.
- If there is a message in the channels queue, it is returned in
Some. - If there is no message ready, None is returned.
- If the channel is closed, None is returned.
- If the channel is closed with an error, None is returned.
Sourcepub fn recv_block(&self) -> Option<T>
pub fn recv_block(&self) -> Option<T>
Returns the next message in the channe. This method will block until either a message arrives or the channel is closed (either regularly) or by an error.
- If a message arrives, the message is returned inside of
Some. - If the channel is closed,
Noneis returned. - If the channel is closed with an error,
Noneis returned.
Sourcepub fn take_error(&self) -> Option<E>
pub fn take_error(&self) -> Option<E>
Returns the error if the channel was closed with an error. This method moves the error out of the Receiver, so subsequent calls will return None.
Returns None if the channel wasn’t closed with an error, or if
the error has already been taken.
Sourcepub fn iter(&self) -> ReceiverIterator<'_, T, E>
pub fn iter(&self) -> ReceiverIterator<'_, T, E>
Returns an iterator over the messages in this receiver. The iterator is non-blocking, and borrows this receiver.
Sourcepub fn blocking_iter(&self) -> ReceiverIterator<'_, T, E>
pub fn blocking_iter(&self) -> ReceiverIterator<'_, T, E>
Returns an iterator over the messages in this receiver. The iterator is blocking and borrows this receiver.
Sourcepub fn into_iter(self) -> ReceiverIterator<'static, T, E>
pub fn into_iter(self) -> ReceiverIterator<'static, T, E>
Returns an iterator over the messages in this receiver. The iterator is non-blocking and consumes this receiver.
Sourcepub fn into_blocking_iter(self) -> ReceiverIterator<'static, T, E>
pub fn into_blocking_iter(self) -> ReceiverIterator<'static, T, E>
Returns an iterator over the messages in this receiver. The iterator is blocking, and consumes this receiver.
Examples found in repository?
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
fn main() {
let (mine, theirs) = get_sockets();
let (sender, receiver) = udp::bind(mine).unwrap();
if let Some(theirs) = theirs {
let stdin = std::io::stdin();
for line in stdin.lock().lines().filter_map(|a| a.ok()) {
sender.send(&line, theirs);
}
} else {
for (from, m) in receiver.into_blocking_iter() {
let m: String = m;
println!("{}: {}", from, m);
}
}
}More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
fn main() {
// Only allow incomming messages of at max 16 bytes, and verify that all of
// our outgoing messages aren't over 8 bytes.
let (read_limit, write_limit) = (SizeLimit::Bounded(16),
SizeLimit::Bounded(8));
// Connect to our running fib-server.
// incoming: (u64, u64)
// outgoing: u64
let (i, mut o) = wire::connect_tcp(("localhost", 8080), read_limit, write_limit).unwrap();
// Send all the numbers from 0 to 10.
for x in 0u64 .. 10u64 {
o.send(&x).ok();
}
// Close our outgoing pipe. This is necessary because otherwise,
// the server will keep waiting for the client to send it data and
// we will deadlock.
o.close();
// Print everything that we get back.
for a in i.into_blocking_iter() {
let (x, fx): (u64, u64) = a;
println!("{} -> {}", x, fx);
}
}14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
fn main() {
// Make a listener on 0.0.0.0:8080
let (listener, _) = wire::listen_tcp(("0.0.0.0", 8080)).unwrap();
// Only allow incoming messages of at max 8 bytes, and verify that we aren't
// writing anything over 16 bytes.
let (read_limit, write_limit) = (SizeLimit::Bounded(8),
SizeLimit::Bounded(16));
// Turn the listener into an iterator of connections.
for (connection, _) in listener.into_blocking_iter() {
// Spawn a new thread for each connection that we get.
spawn(move || {
// Upgrade the connection to read `u64` and write `(u64, u64)`.
let (i, mut o) = wire::upgrade_tcp(connection, read_limit, write_limit).unwrap();
// For each `u64` that we read from the network...
for x in i.into_blocking_iter() {
// Send that number back with the computed value.
o.send(&(x, fib(x))).ok();
}
});
}
}