Skip to content

Commit 4cae9ad

Browse files
committed
Added some extra debug_asserts to ring_buf.
1 parent ba24e33 commit 4cae9ad

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/libcollections/ring_buf.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl<T> RingBuf<T> {
209209
assert!(i < self.len());
210210
assert!(j < self.len());
211211
let ri = wrap_index(self.tail + i, self.cap);
212-
let rj = wrap_index(self.tail + j, self.cap);
212+
let rj = wrap_index(self.tail + j, self.cap);;
213213
unsafe {
214214
ptr::swap(self.ptr.offset(ri as int), self.ptr.offset(rj as int))
215215
}
@@ -320,6 +320,7 @@ impl<T> RingBuf<T> {
320320
);
321321
}
322322
self.head += oldcap;
323+
debug_assert!(self.head > self.tail);
323324
} else { // C
324325
unsafe {
325326
ptr::copy_nonoverlapping_memory(
@@ -329,7 +330,10 @@ impl<T> RingBuf<T> {
329330
);
330331
}
331332
self.tail = count - (oldcap - self.tail);
333+
debug_assert!(self.head < self.tail);
332334
}
335+
debug_assert!(self.head < self.cap);
336+
debug_assert!(self.tail < self.cap);
333337
}
334338
}
335339

@@ -564,7 +568,10 @@ impl<T> RingBuf<T> {
564568
/// ```
565569
#[unstable = "matches collection reform specification, waiting for dust to settle"]
566570
pub fn push_front(&mut self, t: T) {
567-
if self.is_full() { self.reserve(1) }
571+
if self.is_full() {
572+
self.reserve(1);
573+
debug_assert!(!self.is_full());
574+
}
568575

569576
self.tail = wrap_index(self.tail - 1, self.cap);
570577
let tail = self.tail;
@@ -591,7 +598,10 @@ impl<T> RingBuf<T> {
591598
/// ```
592599
#[unstable = "matches collection reform specification, waiting for dust to settle"]
593600
pub fn push_back(&mut self, t: T) {
594-
if self.is_full() { self.reserve(1) }
601+
if self.is_full() {
602+
self.reserve(1);
603+
debug_assert!(!self.is_full());
604+
}
595605

596606
let head = self.head;
597607
self.head = wrap_index(self.head + 1, self.cap);
@@ -634,7 +644,9 @@ impl<T> RingBuf<T> {
634644
#[inline]
635645
fn wrap_index(index: uint, size: uint) -> uint {
636646
// size is always a power of 2
637-
index & (size - 1)
647+
let idx = index & (size - 1);
648+
debug_assert!(idx < size);
649+
idx
638650
}
639651

640652
/// Calculate the number of elements left to be read in the buffer

0 commit comments

Comments
 (0)