11//! A "history buffer", similar to a write-only ring buffer of fixed length.
22//!
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
44//! is overwritten. Thus, the buffer is useful to keep a history of values with
55//! some desired depth, and for example calculate a rolling average.
66//!
1414//! // Starts with no data
1515//! assert_eq!(buf.recent(), None);
1616//!
17- //! buf.write (3);
18- //! buf.write (5);
17+ //! buf.push (3);
18+ //! buf.push (5);
1919//! buf.extend(&[4, 4]);
2020//!
2121//! // The most recent written element is a four.
@@ -157,7 +157,7 @@ pub struct HistoryBufInner<T, S: HistoryBufStorage<T> + ?Sized> {
157157
158158/// A "history buffer", similar to a write-only ring buffer of fixed length.
159159///
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
161161/// is overwritten. Thus, the buffer is useful to keep a history of values with
162162/// some desired depth, and for example calculate a rolling average.
163163///
@@ -171,8 +171,8 @@ pub struct HistoryBufInner<T, S: HistoryBufStorage<T> + ?Sized> {
171171/// // Starts with no data
172172/// assert_eq!(buf.recent(), None);
173173///
174- /// buf.write (3);
175- /// buf.write (5);
174+ /// buf.push (3);
175+ /// buf.push (5);
176176/// buf.extend(&[4, 4]);
177177///
178178/// // The most recent written element is a four.
@@ -204,8 +204,8 @@ pub type HistoryBuf<T, const N: usize> = HistoryBufInner<T, OwnedHistoryBufStora
204204/// // Starts with no data
205205/// assert_eq!(buf.recent(), None);
206206///
207- /// buf.write (3);
208- /// buf.write (5);
207+ /// buf.push (3);
208+ /// buf.push (5);
209209/// buf.extend(&[4, 4]);
210210///
211211/// // The most recent written element is a four.
@@ -369,7 +369,13 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
369369 }
370370
371371 /// Writes an element to the buffer, overwriting the oldest value.
372+ #[ deprecated( note = "Superseeded by push for harmonizing naming with other containers." ) ]
372373 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 ) {
373379 if self . filled {
374380 // Drop the old before we overwrite it.
375381 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> {
392398 T : Clone ,
393399 {
394400 for item in other {
395- self . write ( item. clone ( ) ) ;
401+ self . push ( item. clone ( ) ) ;
396402 }
397403 }
398404
@@ -404,8 +410,8 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
404410 /// use heapless::HistoryBuf;
405411 ///
406412 /// let mut x: HistoryBuf<u8, 16> = HistoryBuf::new();
407- /// x.write (4);
408- /// x.write (10);
413+ /// x.push (4);
414+ /// x.push (10);
409415 /// assert_eq!(x.recent(), Some(&10));
410416 /// ```
411417 pub fn recent ( & self ) -> Option < & T > {
@@ -421,8 +427,8 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
421427 /// use heapless::HistoryBuf;
422428 ///
423429 /// let mut x: HistoryBuf<u8, 16> = HistoryBuf::new();
424- /// x.write (4);
425- /// x.write (10);
430+ /// x.push (4);
431+ /// x.push (10);
426432 /// assert_eq!(x.recent_index(), Some(1));
427433 /// ```
428434 pub fn recent_index ( & self ) -> Option < usize > {
@@ -445,8 +451,8 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
445451 /// use heapless::HistoryBuf;
446452 ///
447453 /// let mut x: HistoryBuf<u8, 16> = HistoryBuf::new();
448- /// x.write (4);
449- /// x.write (10);
454+ /// x.push (4);
455+ /// x.push (10);
450456 /// assert_eq!(x.oldest(), Some(&4));
451457 /// ```
452458 pub fn oldest ( & self ) -> Option < & T > {
@@ -462,8 +468,8 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> HistoryBufInner<T, S> {
462468 /// use heapless::HistoryBuf;
463469 ///
464470 /// let mut x: HistoryBuf<u8, 16> = HistoryBuf::new();
465- /// x.write (4);
466- /// x.write (10);
471+ /// x.push (4);
472+ /// x.push (10);
467473 /// assert_eq!(x.oldest_index(), Some(0));
468474 /// ```
469475 pub fn oldest_index ( & self ) -> Option < usize > {
@@ -542,7 +548,7 @@ impl<T, S: HistoryBufStorage<T> + ?Sized> Extend<T> for HistoryBufInner<T, S> {
542548 I : IntoIterator < Item = T > ,
543549 {
544550 for item in iter. into_iter ( ) {
545- self . write ( item) ;
551+ self . push ( item) ;
546552 }
547553 }
548554}
@@ -672,16 +678,41 @@ mod tests {
672678 assert ! ( !x. is_full( ) ) ;
673679 }
674680
681+ #[ allow( deprecated) ]
675682 #[ test]
676683 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 ( ) {
677708 let mut x: HistoryBuf < u8 , 4 > = HistoryBuf :: new ( ) ;
678- x. write ( 1 ) ;
679- x. write ( 4 ) ;
709+ x. push ( 1 ) ;
710+ x. push ( 4 ) ;
680711 assert_eq ! ( x. as_unordered_slice( ) , [ 1 , 4 ] ) ;
681712
682- x. write ( 5 ) ;
683- x. write ( 6 ) ;
684- x. write ( 10 ) ;
713+ x. push ( 5 ) ;
714+ x. push ( 6 ) ;
715+ x. push ( 10 ) ;
685716 assert_eq ! ( x. as_unordered_slice( ) , [ 10 , 4 , 5 , 6 ] ) ;
686717
687718 x. extend ( [ 11 , 12 ] . iter ( ) ) ;
@@ -704,7 +735,7 @@ mod tests {
704735 let mut x: HistoryBuf < u8 , 3 > = HistoryBuf :: new ( ) ;
705736 for i in 0 ..10 {
706737 assert_eq ! ( x. as_unordered_slice( ) , x. clone( ) . as_unordered_slice( ) ) ;
707- x. write ( i) ;
738+ x. push ( i) ;
708739 }
709740
710741 // Records number of clones locally and globally.
@@ -722,11 +753,11 @@ mod tests {
722753 let mut y: HistoryBuf < InstrumentedClone , 2 > = HistoryBuf :: new ( ) ;
723754 let _ = y. clone ( ) ;
724755 assert_eq ! ( GLOBAL . load( Ordering :: Relaxed ) , 0 ) ;
725- y. write ( InstrumentedClone ( 0 ) ) ;
756+ y. push ( InstrumentedClone ( 0 ) ) ;
726757 assert_eq ! ( GLOBAL . load( Ordering :: Relaxed ) , 0 ) ;
727758 assert_eq ! ( y. clone( ) . as_unordered_slice( ) , [ InstrumentedClone ( 1 ) ] ) ;
728759 assert_eq ! ( GLOBAL . load( Ordering :: Relaxed ) , 1 ) ;
729- y. write ( InstrumentedClone ( 0 ) ) ;
760+ y. push ( InstrumentedClone ( 0 ) ) ;
730761 assert_eq ! ( GLOBAL . load( Ordering :: Relaxed ) , 1 ) ;
731762 assert_eq ! (
732763 y. clone( ) . as_unordered_slice( ) ,
@@ -746,14 +777,14 @@ mod tests {
746777 assert_eq ! ( x. recent_index( ) , None ) ;
747778 assert_eq ! ( x. recent( ) , None ) ;
748779
749- x. write ( 1 ) ;
750- x. write ( 4 ) ;
780+ x. push ( 1 ) ;
781+ x. push ( 4 ) ;
751782 assert_eq ! ( x. recent_index( ) , Some ( 1 ) ) ;
752783 assert_eq ! ( x. recent( ) , Some ( & 4 ) ) ;
753784
754- x. write ( 5 ) ;
755- x. write ( 6 ) ;
756- x. write ( 10 ) ;
785+ x. push ( 5 ) ;
786+ x. push ( 6 ) ;
787+ x. push ( 10 ) ;
757788 assert_eq ! ( x. recent_index( ) , Some ( 0 ) ) ;
758789 assert_eq ! ( x. recent( ) , Some ( & 10 ) ) ;
759790 }
@@ -764,14 +795,14 @@ mod tests {
764795 assert_eq ! ( x. oldest_index( ) , None ) ;
765796 assert_eq ! ( x. oldest( ) , None ) ;
766797
767- x. write ( 1 ) ;
768- x. write ( 4 ) ;
798+ x. push ( 1 ) ;
799+ x. push ( 4 ) ;
769800 assert_eq ! ( x. oldest_index( ) , Some ( 0 ) ) ;
770801 assert_eq ! ( x. oldest( ) , Some ( & 1 ) ) ;
771802
772- x. write ( 5 ) ;
773- x. write ( 6 ) ;
774- x. write ( 10 ) ;
803+ x. push ( 5 ) ;
804+ x. push ( 6 ) ;
805+ x. push ( 10 ) ;
775806 assert_eq ! ( x. oldest_index( ) , Some ( 1 ) ) ;
776807 assert_eq ! ( x. oldest( ) , Some ( & 4 ) ) ;
777808 }
@@ -821,7 +852,7 @@ mod tests {
821852 let mut buffer: HistoryBuf < u8 , 6 > = HistoryBuf :: new ( ) ;
822853
823854 for n in 0 ..20 {
824- buffer. write ( n) ;
855+ buffer. push ( n) ;
825856 let ( head, tail) = buffer. as_slices ( ) ;
826857 assert_eq_iter (
827858 [ head, tail] . iter ( ) . copied ( ) . flatten ( ) ,
@@ -911,16 +942,16 @@ mod tests {
911942 let mut x: HistoryBuf < u8 , 3 > = HistoryBuf :: new ( ) ;
912943 let mut y: HistoryBuf < u8 , 3 > = HistoryBuf :: new ( ) ;
913944 assert_eq ! ( x, y) ;
914- x. write ( 1 ) ;
945+ x. push ( 1 ) ;
915946 assert_ne ! ( x, y) ;
916- y. write ( 1 ) ;
947+ y. push ( 1 ) ;
917948 assert_eq ! ( x, y) ;
918949 for _ in 0 ..4 {
919- x. write ( 2 ) ;
950+ x. push ( 2 ) ;
920951 assert_ne ! ( x, y) ;
921952 for i in 0 ..5 {
922- x. write ( i) ;
923- y. write ( i) ;
953+ x. push ( i) ;
954+ y. push ( i) ;
924955 }
925956 assert_eq ! (
926957 x,
@@ -945,9 +976,9 @@ mod tests {
945976 }
946977
947978 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 { } ) ;
951982
952983 assert_eq ! ( DROP_COUNT . load( Ordering :: SeqCst ) , 0 ) ;
953984 x. clear ( ) ;
0 commit comments