Module alloc

Source
Expand description

A channel implemented with heap allocated buffers (require alloc feature).

This module implements a broadcast channel with a fixed-size buffer, allocated in the heap. This means that multiple senders can be used without the need to use the same reference.

The channel overwrites the oldest message if the buffer is full, prioritizing the latest data.

Key Features:

  • Broadcast: Multiple senders can send messages to multiple receivers simultaneously.
  • Heap-allocated Buffers: Ensures data storage flexibility when the application requires it.
  • Fixed-size Buffer: Provides bounded memory usage with predictable performance.
  • Overwriting Behavior: Prioritizes the latest data in scenarios where the buffer becomes full.
  • Cloneable: Both Sender and Receiver are cloneable, enabling flexible message distribution patterns.

Usage Considerations:

  • Well-suited for scenarios where multiple components need to broadcast messages and the latest data takes priority.
  • Ideal when heap allocation is necessary or desirable.
  • Receivers must be fast enough to keep up with the senders and avoid losing messages due to overwriting.

§Examples

use blinkcast::alloc::channel;
let (sender, mut receiver) = channel::<i32>(4);
sender.send(1);
assert_eq!(receiver.recv(), Some(1));

sender.send(2);
sender.send(3);

assert_eq!(receiver.recv(), Some(2));

// clone the receiver
let mut receiver2 = receiver.clone();
assert_eq!(receiver.recv(), Some(3));
assert_eq!(receiver2.recv(), Some(3));
assert_eq!(receiver.recv(), None);
assert_eq!(receiver2.recv(), None);

Structs§

Receiver
The receiver of the channel.
Sender
The sender of the channel.

Functions§

channel
Creates a new channel, returning the Sender and Receiver for it.