Crates.io | mqueue |
lib.rs | mqueue |
version | 0.1.0 |
source | src |
created_at | 2022-12-07 03:34:27.367489 |
updated_at | 2022-12-07 03:34:27.367489 |
description | A mutable FIFO queue. |
homepage | https://github.com/jacksonzamorano/mqueue |
repository | https://github.com/jacksonzamorano/mqueue |
max_upload_size | |
id | 731575 |
size | 16,391 |
This is the simplest way to use a MQueue.
let mut queue = WorkQueue::<i32>::create();
let mut sum = 0;
queue.add(1);
queue.add(2);
queue.add(3);
while let Some(data) = queue.next_value() {
sum += data;
}
assert_eq!(sum, 6);
The loop will continue to give items until it reaches the end. Note that the alternative function next_ref
may be used instead to obtain a reference to the object, instead of a clone
of it.
As you can see, items may be enqueued while in the loop. No matter whether you use next_ref
or next_value
, the queue may be modified while in a loop.
let mut queue = WorkQueue::<i32>::create();
let mut sum = 0;
queue.add(1);
queue.add(2);
queue.add(3);
while let Some(data) = queue.next_ref() {
sum += data;
if sum == 6 {
queue.add(4);
}
}
MQueue works by keeping a current index to your current item. That means that elements remain in memory until manually cleared with clean()
. Manually clean the queue when you're done with it or at any point where it makes sense to. This operation is O(n), while accessing current or next is O(1). You can get how many items that are consumed but not cleaned by calling dirty_count()
.
You can access the number of remaining items by calling queue.remaining()
. This returns an usize
.