Struct bus::Bus

source ·
pub struct Bus<T> { /* private fields */ }
Expand description

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.

Implementations§

source§

impl<T> Bus<T>

source

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

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.

source

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

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"));
source

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

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.

source

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

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"));
source

pub fn rx_count(&self) -> usize

Returns the number of active consumers currently attached to this bus.

It is not guaranteed that a sent message will reach this number of consumers, as active consumers may never call recv or try_recv again before dropping.

Examples
use bus::Bus;

let mut bus = Bus::<u8>::new(10);
assert_eq!(bus.rx_count(), 0);

let rx1 = bus.add_rx();
assert_eq!(bus.rx_count(), 1);

drop(rx1);
assert_eq!(bus.rx_count(), 0);

Trait Implementations§

source§

impl<T> Debug for Bus<T>

source§

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

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

impl<T> Drop for Bus<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for Bus<T>

§

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

§

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

§

impl<T> Unpin for Bus<T>

§

impl<T> !UnwindSafe for Bus<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.