Crate bndpresbufch

Source
Expand description

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));

Structs§

Builder
Builder for a bounds-preserving buffer channel.
MustHandle
Wrapper around elements that must be handled by the application.
Receiver
Receiving end-point used to receive bounds-preserved buffers from a Sender end-point.
Sender
Transmitting end-point used to transfer bounds-preserved buffers to a Receiver end-point.

Enums§

Error
Crate-specific errors.