[][src]Struct bus::Bus

pub struct Bus<T> { /* fields omitted */ }

Bus is the main interconnect for broadcast messages. It can be used to send broadcast messages, or to connect additional consumers. When the Bus is dropped, receivers will continue receiving any outstanding broadcast messages they would have received if the bus were not dropped. After all those messages have been received, any subsequent receive call on a receiver will return a disconnected error.

Methods

impl<T> Bus<T>[src]

pub fn new(len: usize) -> Bus<T>[src]

Allocates a new Bus.

The provided length should be sufficient to absorb temporary peaks in the data flow, and is thus workflow-dependent. Bus performance degrades somewhat when the queue is full, so it is generally better to set this high than low unless you are pressed for memory.

pub fn try_broadcast(&mut self, val: T) -> Result<(), T>[src]

Attempt to broadcast the given value to all consumers, but does not block if full.

Note that, in contrast to regular channels, a bus is not considered closed if there are no consumers, and thus broadcasts will continue to succeed. Thus, a successful broadcast occurs as long as there is room on the internal bus to store the value, or some older value has been received by all consumers. Note that a return value of Err means that the data will never be received (by any consumer), but a return value of Ok does not mean that the data will be received by a given consumer. It is possible for a receiver to hang up immediately after this function returns Ok.

This method will never block the current thread.

use bus::Bus;
let mut tx = Bus::new(1);
let mut rx = tx.add_rx();
assert_eq!(tx.try_broadcast("Hello"), Ok(()));
assert_eq!(tx.try_broadcast("world"), Err("world"));

pub fn broadcast(&mut self, val: T)[src]

Broadcasts a value on the bus to all consumers.

This function will block until space in the internal buffer becomes available.

Note that a successful send does not guarantee that the receiver will ever see the data if there is a buffer on this channel. Items may be enqueued in the internal buffer for the receiver to receive at a later time. Furthermore, in contrast to regular channels, a bus is not considered closed if there are no consumers, and thus broadcasts will continue to succeed.

pub fn add_rx(&mut self) -> BusReader<T>[src]

Add a new consumer to this bus.

The new consumer will receive all future broadcasts on this bus.

Examples

use bus::Bus;
use std::sync::mpsc::TryRecvError;

let mut bus = Bus::new(10);
let mut rx1 = bus.add_rx();

bus.broadcast("Hello");

// consumer present during broadcast sees update
assert_eq!(rx1.recv(), Ok("Hello"));

// new consumer does *not* see broadcast
let mut rx2 = bus.add_rx();
assert_eq!(rx2.try_recv(), Err(TryRecvError::Empty));

// both consumers see new broadcast
bus.broadcast("world");
assert_eq!(rx1.recv(), Ok("world"));
assert_eq!(rx2.recv(), Ok("world"));

Trait Implementations

impl<T> Drop for Bus<T>[src]

Auto Trait Implementations

impl<T> Send for Bus<T> where
    T: Send

impl<T> Unpin for Bus<T>

impl<T> Sync for Bus<T> where
    T: Send

impl<T> !UnwindSafe for Bus<T>

impl<T> !RefUnwindSafe for Bus<T>

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]