Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ impl<T> DoubleBuffer<T> {
}

/// Get an immutable reference to the current-state buffer.
pub fn current(&self) -> Ref<T> {
pub fn current(&self) -> Ref<'_, T> {
match self.switched {
false => self.first.borrow(),
true => self.second.borrow(),
}
}

/// Get a mutable reference to the next-state buffer.
pub fn next(&self) -> RefMut<T> {
pub fn next(&self) -> RefMut<'_, T> {
match self.switched {
false => self.second.borrow_mut(),
true => self.first.borrow_mut(),
}
}

/// Get an immutable reference to the next-state buffer.
pub fn next_immut(&self) -> Ref<T> {
pub fn next_immut(&self) -> Ref<'_, T> {
match self.switched {
false => self.second.borrow(),
true => self.first.borrow(),
Expand All @@ -57,9 +57,17 @@ impl<T> DoubleBuffer<T> {
pub fn switch(&mut self) {
self.switched = !self.switched;
}

}

impl<T: Clone> Clone for DoubleBuffer<T> {
fn clone(&self) -> Self {
Self {
first: self.first.clone(),
second: self.second.clone(),
switched: self.switched,
}
}
}

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -96,4 +104,34 @@ mod tests {
}


#[test]
fn cloning_instance() {
let my_double_buf: DoubleBuffer<Vec<i32>> = DoubleBuffer::new(vec![2, 4, 6], Vec::new());
for number in my_double_buf.current().iter() {
my_double_buf.next().push(*number + 1);
}

let mut my_clone_buf = my_double_buf.clone();
my_clone_buf.switch();
assert_eq!(*my_clone_buf.current(), vec!(3, 5, 7));
*my_clone_buf.next() = Vec::new();
for number in my_clone_buf.current().iter() {
my_clone_buf.next().push(*number + 1);
}

assert_eq!(*my_double_buf.current(), vec!(2, 4, 6));
}


#[test]
fn clone_from_elem() {
let my_many_double_buf = vec![DoubleBuffer::new(vec![2, 4, 6], Vec::new()); 2];
for mut my_double_buf in my_many_double_buf {
for number in my_double_buf.current().iter() {
my_double_buf.next().push(*number + 1);
}
my_double_buf.switch();
assert_eq!(*my_double_buf.current(), vec!(3, 5, 7));
}
}
}