@@ -141,6 +141,12 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
141
141
N
142
142
}
143
143
144
+ /// Returns whether the buffer is full
145
+ #[ inline]
146
+ pub fn is_full ( & self ) -> bool {
147
+ self . filled
148
+ }
149
+
144
150
/// Writes an element to the buffer, overwriting the oldest value.
145
151
pub fn write ( & mut self , t : T ) {
146
152
if self . filled {
@@ -182,14 +188,70 @@ impl<T, const N: usize> HistoryBuffer<T, N> {
182
188
/// assert_eq!(x.recent(), Some(&10));
183
189
/// ```
184
190
pub fn recent ( & self ) -> Option < & T > {
191
+ self . recent_index ( )
192
+ . map ( |i| unsafe { & * self . data [ i] . as_ptr ( ) } )
193
+ }
194
+
195
+ /// Returns index of the most recently written value in the underlying slice.
196
+ ///
197
+ /// # Examples
198
+ ///
199
+ /// ```
200
+ /// use heapless::HistoryBuffer;
201
+ ///
202
+ /// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
203
+ /// x.write(4);
204
+ /// x.write(10);
205
+ /// assert_eq!(x.recent_index(), Some(1));
206
+ /// ```
207
+ pub fn recent_index ( & self ) -> Option < usize > {
185
208
if self . write_at == 0 {
186
209
if self . filled {
187
- Some ( unsafe { & * self . data [ self . capacity ( ) - 1 ] . as_ptr ( ) } )
210
+ Some ( self . capacity ( ) - 1 )
188
211
} else {
189
212
None
190
213
}
191
214
} else {
192
- Some ( unsafe { & * self . data [ self . write_at - 1 ] . as_ptr ( ) } )
215
+ Some ( self . write_at - 1 )
216
+ }
217
+ }
218
+
219
+ /// Returns a reference to the oldest value in the buffer.
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(), Some(&4));
230
+ /// ```
231
+ pub fn oldest ( & self ) -> Option < & T > {
232
+ self . oldest_index ( )
233
+ . map ( |i| unsafe { & * self . data [ i] . as_ptr ( ) } )
234
+ }
235
+
236
+ /// Returns index of the oldest value in the underlying slice.
237
+ ///
238
+ /// # Examples
239
+ ///
240
+ /// ```
241
+ /// use heapless::HistoryBuffer;
242
+ ///
243
+ /// let mut x: HistoryBuffer<u8, 16> = HistoryBuffer::new();
244
+ /// x.write(4);
245
+ /// x.write(10);
246
+ /// assert_eq!(x.oldest_index(), Some(0));
247
+ /// ```
248
+ pub fn oldest_index ( & self ) -> Option < usize > {
249
+ if self . filled {
250
+ Some ( self . write_at )
251
+ } else if self . write_at == 0 {
252
+ None
253
+ } else {
254
+ Some ( 0 )
193
255
}
194
256
}
195
257
@@ -381,9 +443,11 @@ mod tests {
381
443
assert_eq ! ( x. len( ) , 4 ) ;
382
444
assert_eq ! ( x. as_slice( ) , [ 1 ; 4 ] ) ;
383
445
assert_eq ! ( * x, [ 1 ; 4 ] ) ;
446
+ assert ! ( x. is_full( ) ) ;
384
447
385
448
let x: HistoryBuffer < u8 , 4 > = HistoryBuffer :: new ( ) ;
386
449
assert_eq ! ( x. as_slice( ) , [ ] ) ;
450
+ assert ! ( !x. is_full( ) ) ;
387
451
}
388
452
389
453
#[ test]
@@ -457,18 +521,39 @@ mod tests {
457
521
#[ test]
458
522
fn recent ( ) {
459
523
let mut x: HistoryBuffer < u8 , 4 > = HistoryBuffer :: new ( ) ;
524
+ assert_eq ! ( x. recent_index( ) , None ) ;
460
525
assert_eq ! ( x. recent( ) , None ) ;
461
526
462
527
x. write ( 1 ) ;
463
528
x. write ( 4 ) ;
529
+ assert_eq ! ( x. recent_index( ) , Some ( 1 ) ) ;
464
530
assert_eq ! ( x. recent( ) , Some ( & 4 ) ) ;
465
531
466
532
x. write ( 5 ) ;
467
533
x. write ( 6 ) ;
468
534
x. write ( 10 ) ;
535
+ assert_eq ! ( x. recent_index( ) , Some ( 0 ) ) ;
469
536
assert_eq ! ( x. recent( ) , Some ( & 10 ) ) ;
470
537
}
471
538
539
+ #[ test]
540
+ fn oldest ( ) {
541
+ let mut x: HistoryBuffer < u8 , 4 > = HistoryBuffer :: new ( ) ;
542
+ assert_eq ! ( x. oldest_index( ) , None ) ;
543
+ assert_eq ! ( x. oldest( ) , None ) ;
544
+
545
+ x. write ( 1 ) ;
546
+ x. write ( 4 ) ;
547
+ assert_eq ! ( x. oldest_index( ) , Some ( 0 ) ) ;
548
+ assert_eq ! ( x. oldest( ) , Some ( & 1 ) ) ;
549
+
550
+ x. write ( 5 ) ;
551
+ x. write ( 6 ) ;
552
+ x. write ( 10 ) ;
553
+ assert_eq ! ( x. oldest_index( ) , Some ( 1 ) ) ;
554
+ assert_eq ! ( x. oldest( ) , Some ( & 4 ) ) ;
555
+ }
556
+
472
557
#[ test]
473
558
fn as_slice ( ) {
474
559
let mut x: HistoryBuffer < u8 , 4 > = HistoryBuffer :: new ( ) ;
0 commit comments