@@ -124,6 +124,12 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
124
124
N
125
125
}
126
126
127
+ /// Returns whether the buffer is filled
128
+ #[ inline]
129
+ pub fn is_filled ( & self ) -> bool {
130
+ self . filled
131
+ }
132
+
127
133
/// Writes an element to the buffer, overwriting the oldest value.
128
134
pub fn write ( & mut self , t : T ) {
129
135
if self . filled {
@@ -165,14 +171,72 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
165
171
/// assert_eq!(x.recent(), Some(&10));
166
172
/// ```
167
173
pub fn recent ( & self ) -> Option < & T > {
174
+ self . recent_index ( )
175
+ . map ( |i| unsafe { & * self . data [ i] . as_ptr ( ) } )
176
+ }
177
+
178
+ /// Returns index of the most recently written value in the underlying slice.
179
+ ///
180
+ /// # Examples
181
+ ///
182
+ /// ```
183
+ /// use heapless::HistoryBuffer;
184
+ ///
185
+ /// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
186
+ /// x.write(4);
187
+ /// x.write(10);
188
+ /// assert_eq!(x.recent_index(), Some(1));
189
+ /// ```
190
+ pub fn recent_index ( & self ) -> Option < usize > {
168
191
if self . write_at == 0 {
169
192
if self . filled {
170
- Some ( unsafe { & * self . data [ self . capacity ( ) - 1 ] . as_ptr ( ) } )
193
+ Some ( self . capacity ( ) - 1 )
171
194
} else {
172
195
None
173
196
}
174
197
} else {
175
- Some ( unsafe { & * self . data [ self . write_at - 1 ] . as_ptr ( ) } )
198
+ Some ( self . write_at - 1 )
199
+ }
200
+ }
201
+
202
+ /// Returns a reference to the oldest value in the buffer.
203
+ ///
204
+ /// # Examples
205
+ ///
206
+ /// ```
207
+ /// use heapless::HistoryBuffer;
208
+ ///
209
+ /// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
210
+ /// x.write(4);
211
+ /// x.write(10);
212
+ /// assert_eq!(x.oldest(), Some(&4));
213
+ /// ```
214
+ pub fn oldest ( & self ) -> Option < & T > {
215
+ self . oldest_index ( )
216
+ . map ( |i| unsafe { & * self . data [ i] . as_ptr ( ) } )
217
+ }
218
+
219
+ /// Returns index of the oldest value in the underlying slice.
220
+ ///
221
+ /// # Examples
222
+ ///
223
+ /// ```
224
+ /// use heapless::HistoryBuffer;
225
+ ///
226
+ /// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
227
+ /// x.write(4);
228
+ /// x.write(10);
229
+ /// assert_eq!(x.oldest_index(), Some(0));
230
+ /// ```
231
+ pub fn oldest_index ( & self ) -> Option < usize > {
232
+ if self . filled {
233
+ Some ( self . write_at )
234
+ } else {
235
+ if self . write_at == 0 {
236
+ None
237
+ } else {
238
+ Some ( 0 )
239
+ }
176
240
}
177
241
}
178
242
@@ -365,9 +429,11 @@ mod tests {
365
429
assert_eq ! ( x. len( ) , 4 ) ;
366
430
assert_eq ! ( x. as_slice( ) , [ 1 ; 4 ] ) ;
367
431
assert_eq ! ( * x, [ 1 ; 4 ] ) ;
432
+ assert ! ( x. is_filled( ) ) ;
368
433
369
434
let x: HistoryBuffer < u8 , 4 > = HistoryBuffer :: new ( ) ;
370
435
assert_eq ! ( x. as_slice( ) , [ ] ) ;
436
+ assert ! ( !x. is_filled( ) ) ;
371
437
}
372
438
373
439
#[ test]
@@ -441,18 +507,39 @@ mod tests {
441
507
#[ test]
442
508
fn recent ( ) {
443
509
let mut x: HistoryBuffer < u8 , 4 > = HistoryBuffer :: new ( ) ;
510
+ assert_eq ! ( x. recent_index( ) , None ) ;
444
511
assert_eq ! ( x. recent( ) , None ) ;
445
512
446
513
x. write ( 1 ) ;
447
514
x. write ( 4 ) ;
515
+ assert_eq ! ( x. recent_index( ) , Some ( 1 ) ) ;
448
516
assert_eq ! ( x. recent( ) , Some ( & 4 ) ) ;
449
517
450
518
x. write ( 5 ) ;
451
519
x. write ( 6 ) ;
452
520
x. write ( 10 ) ;
521
+ assert_eq ! ( x. recent_index( ) , Some ( 0 ) ) ;
453
522
assert_eq ! ( x. recent( ) , Some ( & 10 ) ) ;
454
523
}
455
524
525
+ #[ test]
526
+ fn oldest ( ) {
527
+ let mut x: HistoryBuffer < u8 , 4 > = HistoryBuffer :: new ( ) ;
528
+ assert_eq ! ( x. oldest_index( ) , None ) ;
529
+ assert_eq ! ( x. oldest( ) , None ) ;
530
+
531
+ x. write ( 1 ) ;
532
+ x. write ( 4 ) ;
533
+ assert_eq ! ( x. oldest_index( ) , Some ( 0 ) ) ;
534
+ assert_eq ! ( x. oldest( ) , Some ( & 1 ) ) ;
535
+
536
+ x. write ( 5 ) ;
537
+ x. write ( 6 ) ;
538
+ x. write ( 10 ) ;
539
+ assert_eq ! ( x. oldest_index( ) , Some ( 1 ) ) ;
540
+ assert_eq ! ( x. oldest( ) , Some ( & 4 ) ) ;
541
+ }
542
+
456
543
#[ test]
457
544
fn as_slice ( ) {
458
545
let mut x: HistoryBuffer < u8 , 4 > = HistoryBuffer :: new ( ) ;
0 commit comments