File tree Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Expand file tree Collapse file tree 1 file changed +9
-2
lines changed Original file line number Diff line number Diff line change @@ -122,7 +122,10 @@ where
122
122
/// Writes an element to the buffer, overwriting the oldest value.
123
123
pub fn write ( & mut self , t : T ) {
124
124
self . data [ self . write_at ] = t;
125
- self . write_at = ( self . write_at + 1 ) % self . len ( ) ;
125
+ self . write_at = self . write_at + 1 ;
126
+ if self . write_at == self . len ( ) {
127
+ self . write_at = 0 ;
128
+ }
126
129
}
127
130
128
131
/// Clones and writes all elements in a slice to the buffer.
@@ -152,7 +155,11 @@ where
152
155
/// assert_eq!(x.recent(), &10);
153
156
/// ```
154
157
pub fn recent ( & self ) -> & T {
155
- & self . data [ ( self . write_at + self . len ( ) - 1 ) % self . len ( ) ]
158
+ if self . write_at == 0 {
159
+ & self . data [ ( self . len ( ) - 1 ) ]
160
+ } else {
161
+ & self . data [ ( self . write_at - 1 ) ]
162
+ }
156
163
}
157
164
158
165
/// Returns the array slice backing the buffer, without keeping track
You can’t perform that action at this time.
0 commit comments