Struct MPMCReceiver

Source
pub struct MPMCReceiver<T> { /* private fields */ }
Expand description

This is the receiving end of a standard mpmc view of the queue It functions similarly to the BroadcastReceiver execpt there is only ever one stream. As a result, the type doesn’t need to be clone

Implementations§

Source§

impl<T> MPMCReceiver<T>

Source

pub fn try_recv(&self) -> Result<T, TryRecvError>

Tries to receive a value from the queue without blocking.

§Examples:
use multiqueue2::mpmc_queue;
let (w, r) = mpmc_queue(10);
w.try_send(1).unwrap();
assert_eq!(1, r.try_recv().unwrap());
use multiqueue2::mpmc_queue;
use std::thread;

let (send, recv) = mpmc_queue(10);

let handle = thread::spawn(move || {
    for _ in 0..10 {
        loop {
            match recv.try_recv() {
                Ok(val) => {
                    println!("Got {}", val);
                    break;
                },
                Err(_) => (),
            }
        }
    }
    assert!(recv.try_recv().is_err()); // recv would block here
});

for i in 0..10 {
    send.try_send(i).unwrap();
}

// Drop the sender to close the queue
drop(send);

handle.join();
Source

pub fn recv(&self) -> Result<T, RecvError>

Receives a value from the queue, blocks until there is data.

§Examples:
use multiqueue2::mpmc_queue;
let (w, r) = mpmc_queue(10);
w.try_send(1).unwrap();
assert_eq!(1, r.recv().unwrap());
use multiqueue2::mpmc_queue;
use std::thread;

let (send, recv) = mpmc_queue(10);

let handle = thread::spawn(move || {
    // note the lack of dealing with failed reads.
    // unwrap 'ignores' the error where sender disconnects
    for _ in 0..10 {
        println!("Got {}", recv.recv().unwrap());
    }
    assert!(recv.try_recv().is_err());
});

for i in 0..10 {
    send.try_send(i).unwrap();
}

// Drop the sender to close the queue
drop(send);

handle.join();
Source

pub fn unsubscribe(self) -> bool

Removes the given reader from the queue subscription lib Returns true if this is the last reader in a given broadcast unit

§Examples
use multiqueue2::mpmc_queue;
let (writer, reader) = mpmc_queue(2);
writer.try_send(1).expect("This will succeed since queue is empty");
reader.try_recv().expect("This reader can read");
reader.unsubscribe();
// Fails since there's no readers left
assert!(writer.try_send(1).is_err());
Source

pub fn into_single(self) -> Result<MPMCUniReceiver<T>, MPMCReceiver<T>>

If there is only one MPMCReceiver on the stream, converts the Receiver into a MPMCUniReceiver otherwise returns the MPMCReceiver.

§Example:
use multiqueue2::mpmc_queue;

let (w, r) = mpmc_queue(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);
Source

pub fn try_iter(&self) -> MPMCRefIter<'_, T>

Returns a non-owning iterator that iterates over the queue until it fails to receive an item, either through being empty or begin disconnected. This iterator will never block.

§Examples:
use multiqueue2::mpmc_queue;
let (w, r) = mpmc_queue(2);
for _ in 0 .. 3 {
    w.try_send(1).unwrap();
    w.try_send(2).unwrap();
    for val in r.try_iter().zip(1..2) {
        assert_eq!(val.0, val.1);
    }
}

Trait Implementations§

Source§

impl<T> Clone for MPMCReceiver<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for MPMCReceiver<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a, T: 'a> IntoIterator for &'a MPMCReceiver<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = MPMCRefIter<'a, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> MPMCRefIter<'a, T>

Creates an iterator from a value. Read more
Source§

impl<T> IntoIterator for MPMCReceiver<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = MPMCIter<T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> MPMCIter<T>

Creates an iterator from a value. Read more
Source§

impl<T: Send> Send for MPMCReceiver<T>

Auto Trait Implementations§

§

impl<T> !Freeze for MPMCReceiver<T>

§

impl<T> !RefUnwindSafe for MPMCReceiver<T>

§

impl<T> !Sync for MPMCReceiver<T>

§

impl<T> Unpin for MPMCReceiver<T>

§

impl<T> !UnwindSafe for MPMCReceiver<T>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.