Struct BroadcastUniReceiver

Source
pub struct BroadcastUniReceiver<T: Clone + Sync> { /* private fields */ }
Expand description

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 BroadcastUniInnerRecv

§Example:

use multiqueue2::broadcast_queue;

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

Implementations§

Source§

impl<T: Clone + Sync> BroadcastUniReceiver<T>

Source

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

Identical to BroadcastReceiver::try_recv

Source

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

Identical to BroadcastReceiver::recv

Source

pub fn try_recv_view<R, F: FnOnce(&T) -> R>( &self, op: F, ) -> Result<R, (F, TryRecvError)>

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 multiqueue2::broadcast_queue;

let (w, r) = broadcast_queue(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.try_recv_view(|x| 1 + *x) {
        Ok(val) => val,
        Err(_) => panic!("Queue shouldn't be disconncted or empty"),
    };
    assert_eq!(i + 1, val);
}
assert!(single_r.try_recv_view(|x| *x).is_err());
drop(w);
assert!(single_r.try_recv_view(|x| *x).is_err());
Source

pub fn recv_view<R, F: FnOnce(&T) -> R>( &self, op: F, ) -> Result<R, (F, RecvError)>

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 multiqueue2::broadcast_queue;

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

pub fn unsubscribe(self)

Almost identical to BroadcastReceiver::unsubscribe, except it doesn’t return a boolean of whether this was the last receiver on the stream because a receiver of this type must be the last one on the stream

Source

pub fn into_multi(self) -> BroadcastReceiver<T>

Transforms the BroadcastUniReceiver into a BroadcastReceiver

§Example
use multiqueue2::broadcast_queue;

let (w, r) = broadcast_queue(10);
w.try_send(1).unwrap();
let single_r = r.into_single().unwrap();
let normal_r = single_r.into_multi();
normal_r.clone();
Source

pub fn iter_with<R, F: FnMut(&T) -> R>(self, op: F) -> BroadcastUniIter<R, F, 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::broadcast_queue;
let (w, r) = broadcast_queue(2);
let sr = r.into_single().unwrap();
w.try_send(1).unwrap();
w.try_send(2).unwrap();
w.unsubscribe();
for val in sr.iter_with(|x| 2 * *x).zip(1..2) {
    assert_eq!(val.0, val.1 * 2);
}
Source

pub fn try_iter_with<R, F: FnMut(&T) -> R>( &self, op: F, ) -> BroadcastUniRefIter<'_, R, F, 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::broadcast_queue;
let (w, r) = broadcast_queue(2);
let sr = r.into_single().unwrap();
for _ in 0 .. 3 {
    w.try_send(1).unwrap();
    w.try_send(2).unwrap();
    for val in sr.try_iter_with(|x| 2 * *x).zip(1..2) {
        assert_eq!(val.0, val.1*2);
    }
}

Trait Implementations§

Source§

impl<'a, T: Clone + Sync + 'a> IntoIterator for &'a BroadcastUniReceiver<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = BroadcastSCRefIter<'a, T>

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

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

Creates an iterator from a value. Read more
Source§

impl<T: Clone + Sync> IntoIterator for BroadcastUniReceiver<T>

Source§

type Item = T

The type of the elements being iterated over.
Source§

type IntoIter = BroadcastSCIter<T>

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

fn into_iter(self) -> BroadcastSCIter<T>

Creates an iterator from a value. Read more
Source§

impl<T: Send + Sync + Clone> Send for BroadcastUniReceiver<T>

Auto Trait Implementations§

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.