#channel #buffer #queue #data #builder #error #bounds-preserving

bndpresbufch

Bounds-preserving channel for passing buffers

2 releases

0.1.2 Oct 5, 2024
0.1.1 Oct 5, 2024
0.1.0 Sep 13, 2024

#1038 in Concurrency

Download history 143/week @ 2024-09-07 35/week @ 2024-09-14 6/week @ 2024-09-21 19/week @ 2024-09-28 370/week @ 2024-10-05 33/week @ 2024-10-12 2/week @ 2024-10-19 21/week @ 2024-11-02

65 downloads per month

0BSD license

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