@@ -98,6 +98,10 @@ impl<T> RingBuf<T> {
9898 /// Returns true iff the buffer is at capacity
9999 #[ inline]
100100 fn is_full ( & self ) -> bool { self . cap - self . len ( ) == 1 }
101+
102+ /// Returns the index in the underlying buffer for a given logical element index.
103+ #[ inline]
104+ fn wrap_index ( & self , idx : uint ) -> uint { wrap_index ( idx, self . cap ) }
101105}
102106
103107impl < T > RingBuf < T > {
@@ -149,7 +153,7 @@ impl<T> RingBuf<T> {
149153 #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
150154 pub fn get ( & self , i : uint ) -> Option < & T > {
151155 if i < self . len ( ) {
152- let idx = wrap_index ( self . tail + i, self . cap ) ;
156+ let idx = self . wrap_index ( self . tail + i) ;
153157 unsafe { Some ( & * self . ptr . offset ( idx as int ) ) }
154158 } else {
155159 None
@@ -179,7 +183,7 @@ impl<T> RingBuf<T> {
179183 #[ unstable = "matches collection reform specification, waiting for dust to settle" ]
180184 pub fn get_mut ( & mut self , i : uint ) -> Option < & mut T > {
181185 if i < self . len ( ) {
182- let idx = wrap_index ( self . tail + i, self . cap ) ;
186+ let idx = self . wrap_index ( self . tail + i) ;
183187 unsafe { Some ( & mut * self . ptr . offset ( idx as int ) ) }
184188 } else {
185189 None
@@ -208,8 +212,8 @@ impl<T> RingBuf<T> {
208212 pub fn swap ( & mut self , i : uint , j : uint ) {
209213 assert ! ( i < self . len( ) ) ;
210214 assert ! ( j < self . len( ) ) ;
211- let ri = wrap_index ( self . tail + i, self . cap ) ;
212- let rj = wrap_index ( self . tail + j, self . cap ) ;
215+ let ri = self . wrap_index ( self . tail + i) ;
216+ let rj = self . wrap_index ( self . tail + j) ;
213217 unsafe {
214218 ptr:: swap ( self . ptr . offset ( ri as int ) , self . ptr . offset ( rj as int ) )
215219 }
@@ -334,6 +338,7 @@ impl<T> RingBuf<T> {
334338 }
335339 debug_assert ! ( self . head < self . cap) ;
336340 debug_assert ! ( self . tail < self . cap) ;
341+ debug_assert ! ( self . cap. count_ones( ) == 1 ) ;
337342 }
338343 }
339344
@@ -549,7 +554,7 @@ impl<T> RingBuf<T> {
549554 None
550555 } else {
551556 let tail = self . tail ;
552- self . tail = wrap_index ( self . tail + 1 , self . cap ) ;
557+ self . tail = self . wrap_index ( self . tail + 1 ) ;
553558 unsafe { Some ( self . buffer_read ( tail) ) }
554559 }
555560 }
@@ -573,7 +578,7 @@ impl<T> RingBuf<T> {
573578 debug_assert ! ( !self . is_full( ) ) ;
574579 }
575580
576- self . tail = wrap_index ( self . tail - 1 , self . cap ) ;
581+ self . tail = self . wrap_index ( self . tail - 1 ) ;
577582 let tail = self . tail ;
578583 unsafe { self . buffer_write ( tail, t) ; }
579584 }
@@ -604,7 +609,7 @@ impl<T> RingBuf<T> {
604609 }
605610
606611 let head = self . head ;
607- self . head = wrap_index ( self . head + 1 , self . cap ) ;
612+ self . head = self . wrap_index ( self . head + 1 ) ;
608613 unsafe { self . buffer_write ( head, t) }
609614 }
610615
@@ -633,7 +638,7 @@ impl<T> RingBuf<T> {
633638 if self . is_empty ( ) {
634639 None
635640 } else {
636- self . head = wrap_index ( self . head - 1 , self . cap ) ;
641+ self . head = self . wrap_index ( self . head - 1 ) ;
637642 let head = self . head ;
638643 unsafe { Some ( self . buffer_read ( head) ) }
639644 }
@@ -644,9 +649,7 @@ impl<T> RingBuf<T> {
644649#[ inline]
645650fn wrap_index ( index : uint , size : uint ) -> uint {
646651 // size is always a power of 2
647- let idx = index & ( size - 1 ) ;
648- debug_assert ! ( idx < size) ;
649- idx
652+ index & ( size - 1 )
650653}
651654
652655/// Calculate the number of elements left to be read in the buffer
0 commit comments