#queue #buffer #bounded #bounds-preserving

bndpresbufq

Bounds-preserving, optionally limited, buffer queue

1 unstable release

0.1.0 Apr 9, 2024

#1525 in Data structures

Download history 33/week @ 2024-04-03 117/week @ 2024-04-10

150 downloads per month

0BSD license

9KB
158 lines

Bounds-preserving buffer queue

BndPresLimBufQ is a bounds-preserving, optionally limited, buffer queue.


lib.rs:

BndPresLimBufQ is a bounds-preserving, optionally limited, buffer queue.

Terminology

length is used to refer the number of elements in the queue. size is used to refer to the total amount of bytes in the queue.

Example

use bndpresbufq::BndPresLimBufQ;

// Construct a queue with a maximum 2 element length limit and 4 bytes size
// limit
let mut q = BndPresLimBufQ::new(Some(2), Some(4));

// Add elements to fill up to queue
q.try_push(vec![1, 2]).unwrap();
q.force_push(vec![3, 4]);

// Fail to add new node
assert_eq!(q.try_push(vec![5]), Err(vec![5]));

// Forcibly add node; expelling the oldest node
q.force_push([6].into());

assert_eq!(q.pop(), Some(vec![3, 4]));
assert_eq!(q.pop(), Some(vec![6]));
assert_eq!(q.pop(), None);

Dependencies

~9KB