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

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.

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

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.

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

Executes the destructor for this type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.