wire::tcp

Struct Receiver

Source
pub struct Receiver<T, E>
where T: Send, E: Send,
{ /* private fields */ }
Expand description

The receiving end of the channel.

Implementations§

Source§

impl<T, E> Receiver<T, E>
where T: Send + 'static, E: Send + 'static,

Source

pub fn from_old(v: Receiver<CommMsg<T, E>>) -> Receiver<T, E>

Converts an old-style receiver to a bchannel receiver.

Source

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.

Source

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.
Source

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, None is returned.
  • If the channel is closed with an error, None is returned.
Source

pub fn has_error(&self) -> bool

Returns true if the channel was closed with an error.

Source

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.

Source

pub fn is_closed(&self) -> bool

Returns true if the channel is closed.

Source

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.

Source

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.

Source

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.

Source

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?
examples/unreliable_message.rs (line 27)
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
Hide additional examples
examples/fib_client.rs (line 27)
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);
    }
}
examples/fib_server.rs (line 24)
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();
            }
        });
    }
}

Trait Implementations§

Source§

impl<T, E> Send for Receiver<T, E>
where T: Send, E: Send,

Auto Trait Implementations§

§

impl<T, E> !Freeze for Receiver<T, E>

§

impl<T, E> !RefUnwindSafe for Receiver<T, E>

§

impl<T, E> !Sync for Receiver<T, E>

§

impl<T, E> Unpin for Receiver<T, E>
where E: Unpin,

§

impl<T, E> UnwindSafe for Receiver<T, E>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.