From 4cae9add8c9471126aa405cacecac2623232083a Mon Sep 17 00:00:00 2001 From: Colin Sherratt Date: Mon, 10 Nov 2014 21:16:29 -0500 Subject: [PATCH] Added some extra debug_asserts to ring_buf. --- src/libcollections/ring_buf.rs | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index 79201b1db54..f08c5334000 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -209,7 +209,7 @@ impl RingBuf { assert!(i < self.len()); assert!(j < self.len()); let ri = wrap_index(self.tail + i, self.cap); - let rj = wrap_index(self.tail + j, self.cap); + let rj = wrap_index(self.tail + j, self.cap);; unsafe { ptr::swap(self.ptr.offset(ri as int), self.ptr.offset(rj as int)) } @@ -320,6 +320,7 @@ impl RingBuf { ); } self.head += oldcap; + debug_assert!(self.head > self.tail); } else { // C unsafe { ptr::copy_nonoverlapping_memory( @@ -329,7 +330,10 @@ impl RingBuf { ); } self.tail = count - (oldcap - self.tail); + debug_assert!(self.head < self.tail); } + debug_assert!(self.head < self.cap); + debug_assert!(self.tail < self.cap); } } @@ -564,7 +568,10 @@ impl RingBuf { /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn push_front(&mut self, t: T) { - if self.is_full() { self.reserve(1) } + if self.is_full() { + self.reserve(1); + debug_assert!(!self.is_full()); + } self.tail = wrap_index(self.tail - 1, self.cap); let tail = self.tail; @@ -591,7 +598,10 @@ impl RingBuf { /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn push_back(&mut self, t: T) { - if self.is_full() { self.reserve(1) } + if self.is_full() { + self.reserve(1); + debug_assert!(!self.is_full()); + } let head = self.head; self.head = wrap_index(self.head + 1, self.cap); @@ -634,7 +644,9 @@ impl RingBuf { #[inline] fn wrap_index(index: uint, size: uint) -> uint { // size is always a power of 2 - index & (size - 1) + let idx = index & (size - 1); + debug_assert!(idx < size); + idx } /// Calculate the number of elements left to be read in the buffer