Struct Funnel

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

Funnel maintains a collection of Receivers which it collects messages from to output to a single source.

Implementations§

Source§

impl<T> Funnel<T>

Source

pub fn new() -> Funnel<T>

Creates a new Funnel that received messages of type T with no input sources.

Source

pub fn push(&mut self, receiver: Receiver<T>)

Adds a new input source to the Funnel, which it takes ownership over.

Source

pub fn len(&self) -> usize

Obtains the number of input sources that are being read from.

Source

pub fn remove(&mut self, index: usize) -> Receiver<T>

Removes a specific input source at a given index.

This method is useful in situations where the owner of the Funnel is keeping track of the indexes of Receivers. It is also useful for removing Receivers that can no longer be read from, indicated by any errors returned by a call to recv().

Source

pub fn add_receiver(&mut self) -> Sender<T>

Creates a new channel, whose Receiver will be managed by the funnel.

Source

pub fn recv(&mut self) -> (Option<T>, Vec<(FunnelError, usize)>)

Attempts to wait for a value on the oldest Receiver not already received from.

Successive calls to recv() result in calls to the recv() method of successive Receivers managed by the funnel. In doing so, channels are read from in an even distribution. As soon as a value is successfully received, it will be returned as the first element of the tuple returned. recv() will accumulate a Vec containing any errors that may have occurred trying to read from Receivers, as well as the index of those Receivers, allowing users to remove() them if desired. Note that the returned error type is FunnelError, which may take on the NoSourcesError variant. In such cases, the index accompanying the error will be 0, however this error is returned to signify that the length of the sources container is 0 and thus remove()ing index 0 would cause an error.

§Examples
use self::funnel::Funnel;
use std::thread;

let mut fun = Funnel::new();
let writer1 = fun.add_receiver();
let writer2 = fun.add_receiver();
thread::spawn(move || {
    let _ = writer1.send(32).unwrap();
});
thread::spawn(move || {
    let _ = writer2.send(64).unwrap();
});
assert!(match fun.recv() {
    (Some(read_value), errors) => read_value == 32 && errors.len() == 0,
    _ => false,
});
assert!(match fun.recv() {
    (Some(read_value), errors) => read_value == 64 && errors.len() == 0,
    _ => false,
});

Trait Implementations§

Source§

impl<T: Debug> Debug for Funnel<T>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Funnel<T>

§

impl<T> RefUnwindSafe for Funnel<T>

§

impl<T> Send for Funnel<T>
where T: Send,

§

impl<T> !Sync for Funnel<T>

§

impl<T> Unpin for Funnel<T>

§

impl<T> UnwindSafe for Funnel<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> 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.