Struct multiqueue::SingleReceiver [] [src]

pub struct SingleReceiver<T: Clone> { /* fields omitted */ }

This class is similar to the receiver, except it ensures that there is only one consumer for the stream it owns. This means that one can safely view the data in-place with the recv_view method family and avoid the cost of copying it. If there's only one receiver on a stream, it can be converted into a SingleReceiver

Example:

use multiqueue::multiqueue;

let (w, r) = multiqueue(10);
w.try_send(1).unwrap();
let r2 = r.clone();
// Fails since there's two receivers on the stream
assert!(r2.into_single().is_err());
let single_r = r.into_single().unwrap();
let val = match single_r.try_recv_view(|x| 2 * *x) {
    Ok(val) => val,
    Err(_) => panic!("Queue should have an element"),
};
assert_eq!(2, val);

Methods

impl<T: Clone + Sync> SingleReceiver<T>
[src]

Identical to Receiver::try_recv()

Applies the passed function to the value in the queue without copying it out If there is no data in the queue or the writers have disconnected, returns an Err((F, TryRecvError))

Example

use multiqueue::multiqueue;

let (w, r) = multiqueue(10);
let single_r = r.into_single().unwrap();
for i in 0..5 {
    w.try_send(i).unwrap();
}

for i in 0..5 {
    let val = match single_r.recv_view(|x| 1 + *x) {
        Ok(val) => val,
        Err(_) => panic!("Queue shouldn't be disconncted or empty"),
    };
    assert_eq!(i + 1, val);
}
drop(w);
assert!(single_r.recv_view(|x| *x).is_err());

Applies the passed function to the value in the queue without copying it out If there is no data in the queue, blocks until an item is pushed into the queue or all writers disconnect

Example

use multiqueue::multiqueue;

let (w, r) = multiqueue(10);
let single_r = r.into_single().unwrap();
for i in 0..5 {
    w.try_send(i).unwrap();
}

for i in 0..5 {
    let val = match single_r.recv_view(|x| 1 + *x) {
        Ok(val) => val,
        Err(_) => panic!("Queue shouldn't be disconncted or empty"),
    };
    assert_eq!(i + 1, val);
}
drop(w);
assert!(single_r.recv_view(|x| *x).is_err());

This adds a new stream with a Single Receiver, otherwise it is identical to Receiver::add_stream()

Returns an iterator acting over the results of the SingleReceiver with the given function called on them. Essentially the loop match self.recv() {...} in iterator form

Notes: The original reader an be obtained fom the iterator with the function get_reader

Examples

use multiqueue::multiqueue;

let (w, r) = multiqueue(10);
for i in 0..5 {
    w.try_send(i).unwrap();
}
drop(w);
for val in r.into_single().unwrap().iter_with(|x| *x + 1).zip(1..6) {
    assert_eq!(val.0, val.1);
}

Returns an iterator acting over the results of the SingleReceiver with the given function called on them in a similar fashion to iter_with, except this iterator exits once the queue is empty and does not take ownership of the receiver

Notes: The original reader an be obtained fom the iterator with the function get_reader

Examples

use multiqueue::multiqueue;

let (w, r) = multiqueue(10);
let single_r = r.into_single().unwrap();
for _ in 0..2 {
    for i in 0..5 {
        w.try_send(i).unwrap();
    }
    for val in single_r.partial_iter_with(|x| *x + 1).zip(1..6) {
        assert_eq!(val.0, val.1);
    }
}

Transforms the SingleReceiver into a Receiver

Examples

use multiqueue::multiqueue;
let (_, mreader) = multiqueue::<usize>(6);
let sreader = mreader.into_single().unwrap();
let _mreader2 = sreader.into_multi();
// Can't use sreader anymore!
// mreader.try_recv_view(|x| x+1) doesn't work since multireader can't do view methods

See Receiver::unsubscribe()

Trait Implementations

impl<T: Send + Clone> Send for SingleReceiver<T>
[src]