pub struct Queue<K, V, S = HashMap<K, Item>> { /* private fields */ }Expand description
Queue.
This is a specialization of Store, more specifically Ordered, that
maintains insertion order and allows to assign specific deadlines to items.
Values themselves don’t need to implement Ord, since the ordering is
completely independent and induced by the queue.
When an item is inserted, it is annotated with Instant::now, which on
the one hand implements insertion order, and on the other hand allows to
change the ordering of an item through Queue::set_deadline. This allows
to remove items from visibility until a certain point in time.
Additionally, Queue is not a queue in the traditional sense, since it
adds queueing to a Store, an immutable collection of key-value pairs.
The queue is self-organizing, and iterating over it will always yield the
correct order of items at that specific point in time.
§Examples
use zrx_store::{Queue, StoreIterable, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("a", 4);
queue.insert("b", 2);
queue.insert("c", 3);
queue.insert("d", 1);
// Create iterator over the queue
for (key, value) in &queue {
println!("{key}: {value}");
}Implementations§
Source§impl<K, V, S> Queue<K, V, S>
impl<K, V, S> Queue<K, V, S>
Sourcepub fn new() -> Selfwhere
S: Default,
pub fn new() -> Selfwhere
S: Default,
Creates a queue.
§Examples
use std::collections::HashMap;
use zrx_store::{Queue, StoreMut};
// Create queue
let mut queue = Queue::<_, _, HashMap<_, _>>::new();
queue.insert("key", 42);Sourcepub fn get_deadline(&self, key: &K) -> Option<Instant>
pub fn get_deadline(&self, key: &K) -> Option<Instant>
Returns the deadline of the item identified by the key.
§Examples
use std::time::Instant;
use zrx_store::{Queue, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Obtain deadline of item
let deadline = queue.get_deadline(&"key");
assert!(deadline < Some(Instant::now()));Source§impl<K, V, S> Queue<K, V, S>
impl<K, V, S> Queue<K, V, S>
Sourcepub fn set_deadline(&mut self, key: &K, deadline: Instant) -> Option<Instant>
pub fn set_deadline(&mut self, key: &K, deadline: Instant) -> Option<Instant>
Sets the deadline of the item identified by the key.
§Examples
use std::time::Instant;
use zrx_store::{Queue, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Update deadline of item
queue.set_deadline(&"key", Instant::now());Source§impl<K, V, S> Queue<K, V, S>
impl<K, V, S> Queue<K, V, S>
Sourcepub fn deadline(&self) -> Option<Instant>
pub fn deadline(&self) -> Option<Instant>
Returns the minimum deadline of all items.
§Examples
use std::time::Instant;
use zrx_store::{Queue, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Obtain minimum deadline of all items
let deadline = queue.deadline();
assert!(deadline < Some(Instant::now()));Sourcepub fn take(&mut self) -> Option<(K, V)>
pub fn take(&mut self) -> Option<(K, V)>
Takes ownership of the next item that is due.
Items are considered to be due if Instant::now has passed the value
stored in Item::deadline, which allows to defer processing.
§Examples
use zrx_store::{Queue, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("a", 4);
queue.insert("b", 2);
queue.insert("c", 3);
queue.insert("d", 1);
// Obtain items from queue
while let Some((key, value)) = queue.take() {
println!("{key}: {value}");
}Trait Implementations§
Source§impl<K, V> Default for Queue<K, V>where
K: Key,
impl<K, V> Default for Queue<K, V>where
K: Key,
Source§fn default() -> Self
fn default() -> Self
Creates a queue with HashMap::default as a store.
Note that this method does not allow to customize the BuildHasher,
but uses ahash by default, which is the fastest known hasher.
§Examples
use zrx_store::{Queue, StoreMut};
// Create queue
let mut queue = Queue::default();
// Insert value
queue.insert("key", 42);Source§impl<'a, K, V, S> IntoIterator for &'a Queue<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a Queue<K, V, S>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates an iterator over the queue.
§Examples
use zrx_store::{Queue, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Create iterator over the queue
for (key, value) in &queue {
println!("{key}: {value}");
}Source§impl<'a, K, V, S> IntoIterator for &'a mut Queue<K, V, S>
impl<'a, K, V, S> IntoIterator for &'a mut Queue<K, V, S>
Source§fn into_iter(self) -> Self::IntoIter
fn into_iter(self) -> Self::IntoIter
Creates a mutable iterator over the queue.
§Examples
use zrx_store::{Queue, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Create iterator over the queue
for (key, value) in &mut queue {
println!("{key}: {value}");
}Source§impl<K, V, S> Store<K, V> for Queue<K, V, S>
impl<K, V, S> Store<K, V> for Queue<K, V, S>
Source§fn get<Q>(&self, key: &Q) -> Option<&V>
fn get<Q>(&self, key: &Q) -> Option<&V>
Returns a reference to the value identified by the key.
§Examples
use zrx_store::{Queue, Store, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Obtain reference to value
let value = queue.get(&"key");
assert_eq!(value, Some(&42));Source§fn contains_key<Q>(&self, key: &Q) -> bool
fn contains_key<Q>(&self, key: &Q) -> bool
Returns whether the queue contains the key.
§Examples
use zrx_store::{Queue, Store, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Ensure presence of key
let check = queue.contains_key(&"key");
assert_eq!(check, true);Source§impl<K, V, S> StoreIterable<K, V> for Queue<K, V, S>
impl<K, V, S> StoreIterable<K, V> for Queue<K, V, S>
Source§fn iter(&self) -> Self::Iter<'_>
fn iter(&self) -> Self::Iter<'_>
Creates an iterator over the items of the queue.
§Examples
use zrx_store::{Queue, StoreIterable, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Create iterator over the queue
for (key, value) in queue.iter() {
println!("{key}: {value}");
}type Iter<'a> = Iter<'a, K, V> where Self: 'a
Source§impl<K, V, S> StoreIterableMut<K, V> for Queue<K, V, S>
impl<K, V, S> StoreIterableMut<K, V> for Queue<K, V, S>
Source§fn iter_mut(&mut self) -> Self::IterMut<'_>
fn iter_mut(&mut self) -> Self::IterMut<'_>
Creates a mutable iterator over the items of the queue.
§Examples
use zrx_store::{Queue, StoreIterableMut, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Create iterator over the queue
for (key, value) in queue.iter_mut() {
println!("{key}: {value}");
}type IterMut<'a> = IterMut<'a, K, V> where Self: 'a
Source§impl<K, V, S> StoreKeys<K, V> for Queue<K, V, S>
impl<K, V, S> StoreKeys<K, V> for Queue<K, V, S>
Source§fn keys(&self) -> Self::Keys<'_>
fn keys(&self) -> Self::Keys<'_>
Creates an iterator over the keys of the queue.
§Examples
use zrx_store::{Queue, StoreKeys, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Create iterator over the queue
for key in queue.keys() {
println!("{key}");
}type Keys<'a> = Keys<'a, K> where Self: 'a
Source§impl<K, V, S> StoreMut<K, V> for Queue<K, V, S>
impl<K, V, S> StoreMut<K, V> for Queue<K, V, S>
Source§fn insert(&mut self, key: K, value: V) -> Option<V>
fn insert(&mut self, key: K, value: V) -> Option<V>
Inserts the value identified by the key.
This method only updates the data of the Item, but does not change
the values of Item::deadline in case the item already exists.
§Examples
use zrx_store::{Queue, StoreMut};
// Create queue
let mut queue = Queue::default();
// Insert value
let value = queue.insert("key", 42);
assert_eq!(value, None);Source§fn remove<Q>(&mut self, key: &Q) -> Option<V>
fn remove<Q>(&mut self, key: &Q) -> Option<V>
Removes the value identified by the key.
§Examples
use zrx_store::{Queue, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Remove and return value
let value = queue.remove(&"key");
assert_eq!(value, Some(42));Source§fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
fn remove_entry<Q>(&mut self, key: &Q) -> Option<(K, V)>
Removes the value identified by the key and returns both.
§Examples
use zrx_store::{Queue, StoreMut};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Remove and return entry
let entry = queue.remove_entry(&"key");
assert_eq!(entry, Some(("key", 42)));Source§impl<K, V, S> StoreMutRef<K, V> for Queue<K, V, S>
impl<K, V, S> StoreMutRef<K, V> for Queue<K, V, S>
Source§fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
Returns a mutable reference to the value identified by the key.
§Examples
use zrx_store::{Queue, StoreMut, StoreMutRef};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Obtain mutable reference to value
let mut value = queue.get_mut(&"key");
assert_eq!(value, Some(&mut 42));Source§fn get_or_insert_default(&mut self, key: &K) -> &mut Vwhere
V: Default,
fn get_or_insert_default(&mut self, key: &K) -> &mut Vwhere
V: Default,
Returns a mutable reference to the value or creates the default.
§Examples
use zrx_store::{Queue, StoreMutRef};
// Create queue
let mut queue = Queue::<_, i32>::default();
// Obtain mutable reference to value
let value = queue.get_or_insert_default(&"key");
assert_eq!(value, &mut 0);Source§impl<K, V, S> StoreValues<K, V> for Queue<K, V, S>
impl<K, V, S> StoreValues<K, V> for Queue<K, V, S>
Source§fn values(&self) -> Self::Values<'_>
fn values(&self) -> Self::Values<'_>
Creates an iterator over the values of the queue.
§Examples
use zrx_store::{Queue, StoreMut, StoreValues};
// Create queue and initial state
let mut queue = Queue::default();
queue.insert("key", 42);
// Create iterator over the queue
for value in queue.values() {
println!("{value}");
}type Values<'a> = Values<'a, K, V> where Self: 'a
Auto Trait Implementations§
impl<K, V, S> Freeze for Queue<K, V, S>where
S: Freeze,
impl<K, V, S> RefUnwindSafe for Queue<K, V, S>
impl<K, V, S> Send for Queue<K, V, S>
impl<K, V, S> Sync for Queue<K, V, S>
impl<K, V, S> Unpin for Queue<K, V, S>
impl<K, V, S> UnsafeUnpin for Queue<K, V, S>where
S: UnsafeUnpin,
impl<K, V, S> UnwindSafe for Queue<K, V, S>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<K, V, S> Collection<K, V> for Swhere
K: Key,
V: Value,
S: Any + Debug + Store<K, V> + StoreMut<K, V> + StoreIterable<K, V> + StoreKeys<K, V> + StoreValues<K, V>,
impl<K, V, S> Collection<K, V> for Swhere
K: Key,
V: Value,
S: Any + Debug + Store<K, V> + StoreMut<K, V> + StoreIterable<K, V> + StoreKeys<K, V> + StoreValues<K, V>,
Source§fn contains_key(&self, id: &K) -> bool
fn contains_key(&self, id: &K) -> bool
Returns whether the collection contains the key.
Source§fn remove_entry(&mut self, key: &K) -> Option<(K, V)>
fn remove_entry(&mut self, key: &K) -> Option<(K, V)>
Removes the value identified by the key and returns both.
Source§fn iter(&self) -> Box<dyn Iterator<Item = (&K, &V)> + '_>
fn iter(&self) -> Box<dyn Iterator<Item = (&K, &V)> + '_>
Creates an iterator over the items of the collection.