Crate update_channel

Source
Expand description

Single value multiple producer multiple consumer update channel. Both the receiver and channel can be created with the update_channel and update_channel_with functions.

The channels are mostly lock free, multiple readers can access the shared value at the same time. The only way to really block is by calling the borrow_locked method and holding onto the RwLockReadGuard or by having a lot of writes happen at the same time.

The update channel can practically be seen as a single value which can be updated by an updater and then a receiver can update its own internal value with the receive_update method by cloning the new value in its internal buffer. If only a single receiver is used the take_update method is a more efficient alternative to the receive_update method

§Example

use update_channel::channel_with;
use std::thread::spawn;
 
let (mut receiver, updater) = channel_with(0);
assert_eq!(*receiver.borrow(), 0);
 
spawn(move || {
    updater.update(2).unwrap(); // shared value is 2
    updater.update(12).unwrap(); // shared value is 12
})
.join().unwrap();
 
// Shared value is 2 but internal value is 0
assert_eq!(*receiver.borrow(), 0);
// Update the latest value
receiver.recv_update().unwrap();
// Shared value is 12 and internal value 12
assert_eq!(*receiver.borrow(), 12);

Structs§

ReceiveError
An error that might occur while receiving a value
Receiver
The receiving half of an update channel
Updater
The updater half of the update channel.

Enums§

UpdateError
An error occurred while updating the update channel

Functions§

channel
Creates a channel with None as start value
channel_default
Starts the channel with the default value of T
channel_with
Create a channel where the receiver starts with value as internal value