flume-overwrite
A Rust library that provides bounded channels with overwrite capability, built on top of the flume crate. When the channel reaches capacity, new messages will automatically overwrite the oldest unread messages, ensuring that sends never block due to a full channel.
Features
- Overwrite semantics: Messages sent to a full channel replace the oldest messages
- Non-blocking sends: Never blocks when sending, even at capacity
- Async support: Both blocking and async send operations
- Drain tracking: Returns information about which messages were overwritten
- Thread-safe: Built on flume's proven concurrency primitives
- Zero-copy: Minimal overhead over standard flume channels
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Usage Examples
Basic Overwrite Behavior
use bounded;
let = bounded;
// Fill the channel
assert_eq!;
assert_eq!;
// This overwrites "first"
let overwritten = sender.send_overwrite.unwrap;
assert_eq!;
// Only "second" and "third" remain
assert_eq!;
assert_eq!;
Async Operations
use bounded;
use block_on;
let = bounded;
block_on;
Producer-Consumer with Overwrite
Perfect for scenarios where you want to ensure the consumer always gets the latest data without blocking the producer:
use bounded;
use thread;
use Duration;
let = bounded;
// Producer thread - never blocks
let producer = spawn;
// Consumer thread - processes at its own pace
let consumer = spawn;
producer.join.unwrap;
// Consumer will process the latest available messages
Integration with Standard Flume Operations
The OverwriteSender implements Deref<Target = Sender<T>>, so you can use all standard flume sender methods:
use bounded;
let = bounded;
// Use standard flume methods
sender.send.unwrap;
println!;
println!;
// Or use overwrite methods
sender.send_overwrite.unwrap;
Use Cases
This library is particularly useful for:
- Real-time data streams: Sensor data, market feeds, telemetry where latest data is most important
- Event logging: Keeping recent events while discarding old ones automatically
- Producer-consumer scenarios: Where producers shouldn't be blocked by slow consumers
- Buffering latest state: GUI updates, game state, configuration changes
- Rate limiting: Dropping excess messages while preserving recent ones
Requirements
- Rust 1.75.0 or later (edition 2024)
- Compatible with
no_stdenvironments (through flume)
License
This project is licensed under the MIT License - see the LICENSE file for details.