Optionally clone block behind work-package.
This commit is contained in:
@@ -27,6 +27,14 @@ pub struct UsingQueue<T> where T: Clone {
|
||||
max_size: usize,
|
||||
}
|
||||
|
||||
/// Take an item or just clone it?
|
||||
pub enum GetAction {
|
||||
/// Remove the item, faster but you can't get it back.
|
||||
Take,
|
||||
/// Clone the item, slower but you can get it again.
|
||||
Clone,
|
||||
}
|
||||
|
||||
impl<T> UsingQueue<T> where T: Clone {
|
||||
/// Create a new struct with a maximum size of `max_size`.
|
||||
pub fn new(max_size: usize) -> UsingQueue<T> {
|
||||
@@ -80,6 +88,14 @@ impl<T> UsingQueue<T> where T: Clone {
|
||||
self.in_use.iter().find(|r| predicate(r)).cloned()
|
||||
}
|
||||
|
||||
/// Fork-function for `take_used_if` and `clone_used_if`.
|
||||
pub fn get_used_if<P>(&mut self, action: GetAction, predicate: P) -> Option<T> where P: Fn(&T) -> bool {
|
||||
match action {
|
||||
GetAction::Take => self.take_used_if(predicate),
|
||||
GetAction::Clone => self.clone_used_if(predicate),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the most recently pushed block if `f` returns `true` with a reference to it as
|
||||
/// a parameter, otherwise `None`.
|
||||
/// Will not destroy a block if a reference to it has previously been returned by `use_last_ref`,
|
||||
@@ -121,6 +137,20 @@ fn should_find_when_pushed_and_used() {
|
||||
assert!(q.take_used_if(|i| i == &1).unwrap() == 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_have_same_semantics_for_get_take_clone() {
|
||||
let mut q = UsingQueue::new(2);
|
||||
q.push(1);
|
||||
assert!(q.get_used_if(GetAction::Clone, |i| i == &1).is_none());
|
||||
assert!(q.get_used_if(GetAction::Take, |i| i == &1).is_none());
|
||||
q.use_last_ref();
|
||||
assert!(q.get_used_if(GetAction::Clone, |i| i == &1).unwrap() == 1);
|
||||
assert!(q.get_used_if(GetAction::Clone, |i| i == &1).unwrap() == 1);
|
||||
assert!(q.get_used_if(GetAction::Take, |i| i == &1).unwrap() == 1);
|
||||
assert!(q.get_used_if(GetAction::Clone, |i| i == &1).is_none());
|
||||
assert!(q.get_used_if(GetAction::Take, |i| i == &1).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn should_find_when_pushed_and_used_with_clone() {
|
||||
let mut q = UsingQueue::new(2);
|
||||
|
||||
Reference in New Issue
Block a user