@@ -98,6 +98,10 @@ impl<T> RingBuf<T> {
98
98
/// Returns true iff the buffer is at capacity
99
99
#[ inline]
100
100
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 ) }
101
105
}
102
106
103
107
impl < T > RingBuf < T > {
@@ -149,7 +153,7 @@ impl<T> RingBuf<T> {
149
153
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
150
154
pub fn get ( & self , i : uint ) -> Option < & T > {
151
155
if i < self . len ( ) {
152
- let idx = wrap_index ( self . tail + i, self . cap ) ;
156
+ let idx = self . wrap_index ( self . tail + i) ;
153
157
unsafe { Some ( & * self . ptr . offset ( idx as int ) ) }
154
158
} else {
155
159
None
@@ -179,7 +183,7 @@ impl<T> RingBuf<T> {
179
183
#[ unstable = "matches collection reform specification, waiting for dust to settle" ]
180
184
pub fn get_mut ( & mut self , i : uint ) -> Option < & mut T > {
181
185
if i < self . len ( ) {
182
- let idx = wrap_index ( self . tail + i, self . cap ) ;
186
+ let idx = self . wrap_index ( self . tail + i) ;
183
187
unsafe { Some ( & mut * self . ptr . offset ( idx as int ) ) }
184
188
} else {
185
189
None
@@ -208,8 +212,8 @@ impl<T> RingBuf<T> {
208
212
pub fn swap ( & mut self , i : uint , j : uint ) {
209
213
assert ! ( i < self . len( ) ) ;
210
214
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) ;
213
217
unsafe {
214
218
ptr:: swap ( self . ptr . offset ( ri as int ) , self . ptr . offset ( rj as int ) )
215
219
}
@@ -334,6 +338,7 @@ impl<T> RingBuf<T> {
334
338
}
335
339
debug_assert ! ( self . head < self . cap) ;
336
340
debug_assert ! ( self . tail < self . cap) ;
341
+ debug_assert ! ( self . cap. count_ones( ) == 1 ) ;
337
342
}
338
343
}
339
344
@@ -549,7 +554,7 @@ impl<T> RingBuf<T> {
549
554
None
550
555
} else {
551
556
let tail = self . tail ;
552
- self . tail = wrap_index ( self . tail + 1 , self . cap ) ;
557
+ self . tail = self . wrap_index ( self . tail + 1 ) ;
553
558
unsafe { Some ( self . buffer_read ( tail) ) }
554
559
}
555
560
}
@@ -573,7 +578,7 @@ impl<T> RingBuf<T> {
573
578
debug_assert ! ( !self . is_full( ) ) ;
574
579
}
575
580
576
- self . tail = wrap_index ( self . tail - 1 , self . cap ) ;
581
+ self . tail = self . wrap_index ( self . tail - 1 ) ;
577
582
let tail = self . tail ;
578
583
unsafe { self . buffer_write ( tail, t) ; }
579
584
}
@@ -604,7 +609,7 @@ impl<T> RingBuf<T> {
604
609
}
605
610
606
611
let head = self . head ;
607
- self . head = wrap_index ( self . head + 1 , self . cap ) ;
612
+ self . head = self . wrap_index ( self . head + 1 ) ;
608
613
unsafe { self . buffer_write ( head, t) }
609
614
}
610
615
@@ -633,7 +638,7 @@ impl<T> RingBuf<T> {
633
638
if self . is_empty ( ) {
634
639
None
635
640
} else {
636
- self . head = wrap_index ( self . head - 1 , self . cap ) ;
641
+ self . head = self . wrap_index ( self . head - 1 ) ;
637
642
let head = self . head ;
638
643
unsafe { Some ( self . buffer_read ( head) ) }
639
644
}
@@ -644,9 +649,7 @@ impl<T> RingBuf<T> {
644
649
#[ inline]
645
650
fn wrap_index ( index : uint , size : uint ) -> uint {
646
651
// size is always a power of 2
647
- let idx = index & ( size - 1 ) ;
648
- debug_assert ! ( idx < size) ;
649
- idx
652
+ index & ( size - 1 )
650
653
}
651
654
652
655
/// Calculate the number of elements left to be read in the buffer
0 commit comments