diff --git a/src/arrays.rs b/src/arrays.rs index b2e1d0d..a642dad 100644 --- a/src/arrays.rs +++ b/src/arrays.rs @@ -1,5 +1,4 @@ //! Provides functions for dynamic array manipulation. -use std::cmp::Ordering; /// Allocate an uninitialized array of a given size. /// @@ -31,22 +30,20 @@ pub trait WrappingSlice { impl WrappingSlice 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) } } } diff --git a/src/buffer.rs b/src/buffer.rs index 3194ec0..ed70897 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -83,12 +83,7 @@ impl Buffer { 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); @@ -110,6 +105,10 @@ impl Buffer { /// 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));