1
1
//! A "history buffer", similar to a write-only ring buffer of fixed length.
2
2
//!
3
- //! This buffer keeps a fixed number of elements. On write , the oldest element
3
+ //! This buffer keeps a fixed number of elements. On push , the oldest element
4
4
//! is overwritten. Thus, the buffer is useful to keep a history of values with
5
5
//! some desired depth, and for example calculate a rolling average.
6
6
//!
14
14
//! // Starts with no data
15
15
//! assert_eq!(buf.recent(), None);
16
16
//!
17
- //! buf.write (3);
18
- //! buf.write (5);
17
+ //! buf.push (3);
18
+ //! buf.push (5);
19
19
//! buf.extend(&[4, 4]);
20
20
//!
21
21
//! // The most recent written element is a four.
@@ -157,7 +157,7 @@ pub struct HistoryBufInner<T, S: HistoryBufStorage<T> + ?Sized> {
157
157
158
158
/// A "history buffer", similar to a write-only ring buffer of fixed length.
159
159
///
160
- /// This buffer keeps a fixed number of elements. On write , the oldest element
160
+ /// This buffer keeps a fixed number of elements. On push , the oldest element
161
161
/// is overwritten. Thus, the buffer is useful to keep a history of values with
162
162
/// some desired depth, and for example calculate a rolling average.
163
163
///
@@ -171,8 +171,8 @@ pub struct HistoryBufInner<T, S: HistoryBufStorage<T> + ?Sized> {
171
171
/// // Starts with no data
172
172
/// assert_eq!(buf.recent(), None);
173
173
///
174
- /// buf.write (3);
175
- /// buf.write (5);
174
+ /// buf.push (3);
175
+ /// buf.push (5);
176
176
/// buf.extend(&[4, 4]);
177
177
///
178
178
/// // The most recent written element is a four.
@@ -204,8 +204,8 @@ pub type HistoryBuf<T, const N: usize> = HistoryBufInner<T, OwnedHistoryBufStora
204
204
/// // Starts with no data
205
205
/// assert_eq!(buf.recent(), None);
206
206
///
207
- /// buf.write (3);
208
- /// buf.write (5);
207
+ /// buf.push (3);
208
+ /// buf.push (5);
209
209
/// buf.extend(&[4, 4]);
210
210
///
211
211
/// // The most recent written element is a four.
@@ -369,7 +369,13 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
369
369
}
370
370
371
371
/// Writes an element to the buffer, overwriting the oldest value.
372
+ #[ deprecated( note = "Superseeded by push for harmonizing naming with other containers." ) ]
372
373
pub fn write ( & mut self , t : T ) {
374
+ self . push ( t) ;
375
+ }
376
+
377
+ /// Pushes an element to the buffer, overwriting the oldest value.
378
+ pub fn push ( & mut self , t : T ) {
373
379
if self . filled {
374
380
// Drop the old before we overwrite it.
375
381
unsafe { ptr:: drop_in_place ( self . data . borrow_mut ( ) [ self . write_at ] . as_mut_ptr ( ) ) }
@@ -392,7 +398,7 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
392
398
T : Clone ,
393
399
{
394
400
for item in other {
395
- self . write ( item. clone ( ) ) ;
401
+ self . push ( item. clone ( ) ) ;
396
402
}
397
403
}
398
404
@@ -404,8 +410,8 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
404
410
/// use heapless::HistoryBuf;
405
411
///
406
412
/// let mut x: HistoryBuf<u8, 16> = HistoryBuf::new();
407
- /// x.write (4);
408
- /// x.write (10);
413
+ /// x.push (4);
414
+ /// x.push (10);
409
415
/// assert_eq!(x.recent(), Some(&10));
410
416
/// ```
411
417
pub fn recent ( & self ) -> Option < & T > {
@@ -421,8 +427,8 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
421
427
/// use heapless::HistoryBuf;
422
428
///
423
429
/// let mut x: HistoryBuf<u8, 16> = HistoryBuf::new();
424
- /// x.write (4);
425
- /// x.write (10);
430
+ /// x.push (4);
431
+ /// x.push (10);
426
432
/// assert_eq!(x.recent_index(), Some(1));
427
433
/// ```
428
434
pub fn recent_index ( & self ) -> Option < usize > {
@@ -445,8 +451,8 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
445
451
/// use heapless::HistoryBuf;
446
452
///
447
453
/// let mut x: HistoryBuf<u8, 16> = HistoryBuf::new();
448
- /// x.write (4);
449
- /// x.write (10);
454
+ /// x.push (4);
455
+ /// x.push (10);
450
456
/// assert_eq!(x.oldest(), Some(&4));
451
457
/// ```
452
458
pub fn oldest ( & self ) -> Option < & T > {
@@ -462,8 +468,8 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
462
468
/// use heapless::HistoryBuf;
463
469
///
464
470
/// let mut x: HistoryBuf<u8, 16> = HistoryBuf::new();
465
- /// x.write (4);
466
- /// x.write (10);
471
+ /// x.push (4);
472
+ /// x.push (10);
467
473
/// assert_eq!(x.oldest_index(), Some(0));
468
474
/// ```
469
475
pub fn oldest_index ( & self ) -> Option < usize > {
@@ -542,7 +548,7 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> Extend<T> for HistoryBufInner<T, S> {
542
548
I : IntoIterator < Item = T > ,
543
549
{
544
550
for item in iter. into_iter ( ) {
545
- self . write ( item) ;
551
+ self . push ( item) ;
546
552
}
547
553
}
548
554
}
@@ -672,16 +678,41 @@ mod tests {
672
678
assert ! ( !x. is_full( ) ) ;
673
679
}
674
680
681
+ #[ allow( deprecated) ]
675
682
#[ test]
676
683
fn write ( ) {
684
+ let mut write: HistoryBuf < u8 , 4 > = HistoryBuf :: new ( ) ;
685
+ let mut push: HistoryBuf < u8 , 4 > = HistoryBuf :: new ( ) ;
686
+
687
+ write. write ( 1 ) ;
688
+ write. write ( 4 ) ;
689
+ push. push ( 1 ) ;
690
+ push. push ( 4 ) ;
691
+ assert_eq ! ( write, push) ;
692
+
693
+ write. push ( 5 ) ;
694
+ write. push ( 6 ) ;
695
+ write. push ( 10 ) ;
696
+ push. push ( 5 ) ;
697
+ push. push ( 6 ) ;
698
+ push. push ( 10 ) ;
699
+ assert_eq ! ( write, push) ;
700
+
701
+ write. extend ( [ 11 , 12 ] . iter ( ) ) ;
702
+ push. extend ( [ 11 , 12 ] . iter ( ) ) ;
703
+ assert_eq ! ( write, push) ;
704
+ }
705
+
706
+ #[ test]
707
+ fn push ( ) {
677
708
let mut x: HistoryBuf < u8 , 4 > = HistoryBuf :: new ( ) ;
678
- x. write ( 1 ) ;
679
- x. write ( 4 ) ;
709
+ x. push ( 1 ) ;
710
+ x. push ( 4 ) ;
680
711
assert_eq ! ( x. as_unordered_slice( ) , [ 1 , 4 ] ) ;
681
712
682
- x. write ( 5 ) ;
683
- x. write ( 6 ) ;
684
- x. write ( 10 ) ;
713
+ x. push ( 5 ) ;
714
+ x. push ( 6 ) ;
715
+ x. push ( 10 ) ;
685
716
assert_eq ! ( x. as_unordered_slice( ) , [ 10 , 4 , 5 , 6 ] ) ;
686
717
687
718
x. extend ( [ 11 , 12 ] . iter ( ) ) ;
@@ -704,7 +735,7 @@ mod tests {
704
735
let mut x: HistoryBuf < u8 , 3 > = HistoryBuf :: new ( ) ;
705
736
for i in 0 ..10 {
706
737
assert_eq ! ( x. as_unordered_slice( ) , x. clone( ) . as_unordered_slice( ) ) ;
707
- x. write ( i) ;
738
+ x. push ( i) ;
708
739
}
709
740
710
741
// Records number of clones locally and globally.
@@ -722,11 +753,11 @@ mod tests {
722
753
let mut y: HistoryBuf < InstrumentedClone , 2 > = HistoryBuf :: new ( ) ;
723
754
let _ = y. clone ( ) ;
724
755
assert_eq ! ( GLOBAL . load( Ordering :: Relaxed ) , 0 ) ;
725
- y. write ( InstrumentedClone ( 0 ) ) ;
756
+ y. push ( InstrumentedClone ( 0 ) ) ;
726
757
assert_eq ! ( GLOBAL . load( Ordering :: Relaxed ) , 0 ) ;
727
758
assert_eq ! ( y. clone( ) . as_unordered_slice( ) , [ InstrumentedClone ( 1 ) ] ) ;
728
759
assert_eq ! ( GLOBAL . load( Ordering :: Relaxed ) , 1 ) ;
729
- y. write ( InstrumentedClone ( 0 ) ) ;
760
+ y. push ( InstrumentedClone ( 0 ) ) ;
730
761
assert_eq ! ( GLOBAL . load( Ordering :: Relaxed ) , 1 ) ;
731
762
assert_eq ! (
732
763
y. clone( ) . as_unordered_slice( ) ,
@@ -746,14 +777,14 @@ mod tests {
746
777
assert_eq ! ( x. recent_index( ) , None ) ;
747
778
assert_eq ! ( x. recent( ) , None ) ;
748
779
749
- x. write ( 1 ) ;
750
- x. write ( 4 ) ;
780
+ x. push ( 1 ) ;
781
+ x. push ( 4 ) ;
751
782
assert_eq ! ( x. recent_index( ) , Some ( 1 ) ) ;
752
783
assert_eq ! ( x. recent( ) , Some ( & 4 ) ) ;
753
784
754
- x. write ( 5 ) ;
755
- x. write ( 6 ) ;
756
- x. write ( 10 ) ;
785
+ x. push ( 5 ) ;
786
+ x. push ( 6 ) ;
787
+ x. push ( 10 ) ;
757
788
assert_eq ! ( x. recent_index( ) , Some ( 0 ) ) ;
758
789
assert_eq ! ( x. recent( ) , Some ( & 10 ) ) ;
759
790
}
@@ -764,14 +795,14 @@ mod tests {
764
795
assert_eq ! ( x. oldest_index( ) , None ) ;
765
796
assert_eq ! ( x. oldest( ) , None ) ;
766
797
767
- x. write ( 1 ) ;
768
- x. write ( 4 ) ;
798
+ x. push ( 1 ) ;
799
+ x. push ( 4 ) ;
769
800
assert_eq ! ( x. oldest_index( ) , Some ( 0 ) ) ;
770
801
assert_eq ! ( x. oldest( ) , Some ( & 1 ) ) ;
771
802
772
- x. write ( 5 ) ;
773
- x. write ( 6 ) ;
774
- x. write ( 10 ) ;
803
+ x. push ( 5 ) ;
804
+ x. push ( 6 ) ;
805
+ x. push ( 10 ) ;
775
806
assert_eq ! ( x. oldest_index( ) , Some ( 1 ) ) ;
776
807
assert_eq ! ( x. oldest( ) , Some ( & 4 ) ) ;
777
808
}
@@ -821,7 +852,7 @@ mod tests {
821
852
let mut buffer: HistoryBuf < u8 , 6 > = HistoryBuf :: new ( ) ;
822
853
823
854
for n in 0 ..20 {
824
- buffer. write ( n) ;
855
+ buffer. push ( n) ;
825
856
let ( head, tail) = buffer. as_slices ( ) ;
826
857
assert_eq_iter (
827
858
[ head, tail] . iter ( ) . copied ( ) . flatten ( ) ,
@@ -911,16 +942,16 @@ mod tests {
911
942
let mut x: HistoryBuf < u8 , 3 > = HistoryBuf :: new ( ) ;
912
943
let mut y: HistoryBuf < u8 , 3 > = HistoryBuf :: new ( ) ;
913
944
assert_eq ! ( x, y) ;
914
- x. write ( 1 ) ;
945
+ x. push ( 1 ) ;
915
946
assert_ne ! ( x, y) ;
916
- y. write ( 1 ) ;
947
+ y. push ( 1 ) ;
917
948
assert_eq ! ( x, y) ;
918
949
for _ in 0 ..4 {
919
- x. write ( 2 ) ;
950
+ x. push ( 2 ) ;
920
951
assert_ne ! ( x, y) ;
921
952
for i in 0 ..5 {
922
- x. write ( i) ;
923
- y. write ( i) ;
953
+ x. push ( i) ;
954
+ y. push ( i) ;
924
955
}
925
956
assert_eq ! (
926
957
x,
@@ -945,9 +976,9 @@ mod tests {
945
976
}
946
977
947
978
let mut x: HistoryBuf < DropCheck , 3 > = HistoryBuf :: new ( ) ;
948
- x. write ( DropCheck { } ) ;
949
- x. write ( DropCheck { } ) ;
950
- x. write ( DropCheck { } ) ;
979
+ x. push ( DropCheck { } ) ;
980
+ x. push ( DropCheck { } ) ;
981
+ x. push ( DropCheck { } ) ;
951
982
952
983
assert_eq ! ( DROP_COUNT . load( Ordering :: SeqCst ) , 0 ) ;
953
984
x. clear ( ) ;
0 commit comments