2 releases
0.1.2 | Oct 5, 2024 |
---|---|
0.1.1 |
|
0.1.0 | Sep 13, 2024 |
#792 in Concurrency
425 downloads per month
23KB
473 lines
bndpresbufch
Special-purpose bounds-preserving, optionally size/count limited, channel used to pass buffers between threads/tasks.
lib.rs
:
Bounds-preserving, optionally explicitly limited/lossy, buffer channel.
use bndpresbufch::{Builder, Error};
// Create a buffer queue that can hold at most two buffers, with a total of
// 4 bytes of data.
let (tx, rx) = Builder::new()
.max_len(2)
.max_size(4)
.build();
// Fail to push a single buffer that is larger than maximum allowed total
// queue size.
assert_eq!(tx.force_push(vec![1, 2, 3, 4, 5]),
Err(Error::WontFit(vec![1, 2, 3, 4, 5])));
// Fill up queue
tx.try_push(vec![1, 2]).unwrap();
tx.force_push(vec![3, 4]).unwrap();
// Fail to add more data
assert_eq!(tx.try_push(vec![5]), Err(Error::WontFit(vec![5])));
// Force push data to the queue, ejecting the oldest buffer
tx.force_push(vec![6]).unwrap();
// Pull off a buffer that must be handled.
// Then drop the managed node before marking it has handled, which should
// put it back onto the channel.
let n = rx.pop_managed().unwrap();
assert_eq!(*n, [3, 4]);
drop(n);
assert_eq!(rx.pop(), Some(vec![3, 4]));
assert_eq!(rx.try_pop(), Ok(Some(vec![6])));
assert_eq!(rx.try_pop(), Ok(None));
Dependencies
~0.4–5MB
~12K SLoC