Skip to content

Commit

Permalink
fix unable to pull when len == capacity
Browse files Browse the repository at this point in the history
  • Loading branch information
sagebind committed Feb 24, 2018
1 parent 129ada1 commit 0bd250c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
23 changes: 10 additions & 13 deletions src/arrays.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//! Provides functions for dynamic array manipulation.
use std::cmp::Ordering;
/// Allocate an uninitialized array of a given size.
///
Expand Down Expand Up @@ -31,22 +30,20 @@ pub trait WrappingSlice<T> {

impl<T> WrappingSlice<T> for [T] {
fn wrapping_range(&self, from: usize, to: usize) -> (&[T], &[T]) {
match from.cmp(&to) {
Ordering::Equal => (&[], &[]),
Ordering::Less => (&self[from..to], &[]),
Ordering::Greater => (&self[from..], &self[..to]),
if from < to {
(&self[from..to], &[])
} else {
(&self[from..], &self[..to])
}
}

fn wrapping_range_mut(&mut self, from: usize, to: usize) -> (&mut [T], &mut [T]) {
match from.cmp(&to) {
Ordering::Equal => (&mut [], &mut []),
Ordering::Less => (&mut self[from..to], &mut []),
Ordering::Greater => {
let (mid, right) = self.split_at_mut(from);
let left = mid.split_at_mut(to).0;
(left, right)
},
if from < to {
(&mut self[from..to], &mut [])
} else {
let (mid, right) = self.split_at_mut(from);
let left = mid.split_at_mut(to).0;
(right, left)
}
}
}
11 changes: 5 additions & 6 deletions src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,7 @@ impl<T: Copy> Buffer<T> {
let head_index = self.mask(self.head);
let tail_index = self.mask(self.tail);

let slices = if head_index == tail_index {
let slices = self.array.split_at_mut(head_index);
(slices.1, slices.0)
} else {
self.array.wrapping_range_mut(tail_index, head_index)
};
let slices = self.array.wrapping_range_mut(tail_index, head_index);

let mut pushed = arrays::copy(src, slices.0);
pushed += arrays::copy(&src[pushed..], slices.1);
Expand All @@ -110,6 +105,10 @@ impl<T: Copy> Buffer<T> {
/// Returns the number of elements copied. If there are less elements in the buffer than the length of `dest`, then
/// only part of `dest` will be written to.
pub fn copy_to(&self, dest: &mut [T]) -> usize {
if self.is_empty() {
return 0;
}

let slices = self.array
.wrapping_range(self.mask(self.head), self.mask(self.tail));

Expand Down

0 comments on commit 0bd250c

Please sign in to comment.