1- // Uncomment the #[must_use]s here once [RFC 1940] hits stable.
1+ // Some of the functions in ring buffer is marked as #[must_use]. It notes that
2+ // these functions may have side effects, and it's implemented by [RFC 1940].
23// [RFC 1940]: https://github.com/rust-lang/rust/issues/43302
34
45use core:: cmp;
@@ -202,7 +203,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
202203 ///
203204 /// This function may return a slice smaller than the given size
204205 /// if the free space in the buffer is not contiguous.
205- // #[must_use]
206+ #[ must_use]
206207 pub fn enqueue_many ( & mut self , size : usize ) -> & mut [ T ] {
207208 self . enqueue_many_with ( |buf| {
208209 let size = cmp:: min ( size, buf. len ( ) ) ;
@@ -213,7 +214,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
213214
214215 /// Enqueue as many elements from the given slice into the buffer as possible,
215216 /// and return the amount of elements that could fit.
216- // #[must_use]
217+ #[ must_use]
217218 pub fn enqueue_slice ( & mut self , data : & [ T ] ) -> usize
218219 where
219220 T : Copy ,
@@ -259,7 +260,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
259260 ///
260261 /// This function may return a slice smaller than the given size
261262 /// if the allocated space in the buffer is not contiguous.
262- // #[must_use]
263+ #[ must_use]
263264 pub fn dequeue_many ( & mut self , size : usize ) -> & mut [ T ] {
264265 self . dequeue_many_with ( |buf| {
265266 let size = cmp:: min ( size, buf. len ( ) ) ;
@@ -270,7 +271,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
270271
271272 /// Dequeue as many elements from the buffer into the given slice as possible,
272273 /// and return the amount of elements that could fit.
273- // #[must_use]
274+ #[ must_use]
274275 pub fn dequeue_slice ( & mut self , data : & mut [ T ] ) -> usize
275276 where
276277 T : Copy ,
@@ -294,7 +295,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
294295impl < ' a , T : ' a > RingBuffer < ' a , T > {
295296 /// Return the largest contiguous slice of unallocated buffer elements starting
296297 /// at the given offset past the last allocated element, and up to the given size.
297- // #[must_use]
298+ #[ must_use]
298299 pub fn get_unallocated ( & mut self , offset : usize , mut size : usize ) -> & mut [ T ] {
299300 let start_at = self . get_idx ( self . length + offset) ;
300301 // We can't access past the end of unallocated data.
@@ -318,7 +319,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
318319 /// Write as many elements from the given slice into unallocated buffer elements
319320 /// starting at the given offset past the last allocated element, and return
320321 /// the amount written.
321- // #[must_use]
322+ #[ must_use]
322323 pub fn write_unallocated ( & mut self , offset : usize , data : & [ T ] ) -> usize
323324 where
324325 T : Copy ,
@@ -349,7 +350,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
349350
350351 /// Return the largest contiguous slice of allocated buffer elements starting
351352 /// at the given offset past the first allocated element, and up to the given size.
352- // #[must_use]
353+ #[ must_use]
353354 pub fn get_allocated ( & self , offset : usize , mut size : usize ) -> & [ T ] {
354355 let start_at = self . get_idx ( offset) ;
355356 // We can't read past the end of the allocated data.
@@ -373,7 +374,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
373374 /// Read as many elements from allocated buffer elements into the given slice
374375 /// starting at the given offset past the first allocated element, and return
375376 /// the amount read.
376- // #[must_use]
377+ #[ must_use]
377378 pub fn read_allocated ( & mut self , offset : usize , data : & mut [ T ] ) -> usize
378379 where
379380 T : Copy ,
@@ -686,7 +687,8 @@ mod test {
686687 }
687688 assert_eq ! ( & ring. storage[ ..] , b"abcd........" ) ;
688689
689- ring. enqueue_many ( 4 ) ;
690+ let buf_enqueued = ring. enqueue_many ( 4 ) ;
691+ assert_eq ! ( buf_enqueued. len( ) , 4 ) ;
690692 assert_eq ! ( ring. len( ) , 4 ) ;
691693
692694 {
@@ -730,15 +732,18 @@ mod test {
730732 assert_eq ! ( ring. get_allocated( 16 , 4 ) , b"" ) ;
731733 assert_eq ! ( ring. get_allocated( 0 , 4 ) , b"" ) ;
732734
733- ring. enqueue_slice ( b"abcd" ) ;
735+ let len_enqueued = ring. enqueue_slice ( b"abcd" ) ;
734736 assert_eq ! ( ring. get_allocated( 0 , 8 ) , b"abcd" ) ;
737+ assert_eq ! ( len_enqueued, 4 ) ;
735738
736- ring. enqueue_slice ( b"efghijkl" ) ;
739+ let len_enqueued = ring. enqueue_slice ( b"efghijkl" ) ;
737740 ring. dequeue_many ( 4 ) . copy_from_slice ( b"...." ) ;
738741 assert_eq ! ( ring. get_allocated( 4 , 8 ) , b"ijkl" ) ;
742+ assert_eq ! ( len_enqueued, 8 ) ;
739743
740- ring. enqueue_slice ( b"abcd" ) ;
744+ let len_enqueued = ring. enqueue_slice ( b"abcd" ) ;
741745 assert_eq ! ( ring. get_allocated( 4 , 8 ) , b"ijkl" ) ;
746+ assert_eq ! ( len_enqueued, 4 ) ;
742747 }
743748
744749 #[ test]
@@ -782,10 +787,11 @@ mod test {
782787 #[ test]
783788 fn test_buffer_write_wholly ( ) {
784789 let mut ring = RingBuffer :: new ( vec ! [ b'.' ; 8 ] ) ;
785- ring. enqueue_many ( 2 ) . copy_from_slice ( b"xx " ) ;
786- ring. enqueue_many ( 2 ) . copy_from_slice ( b"xx " ) ;
790+ ring. enqueue_many ( 2 ) . copy_from_slice ( b"ab " ) ;
791+ ring. enqueue_many ( 2 ) . copy_from_slice ( b"cd " ) ;
787792 assert_eq ! ( ring. len( ) , 4 ) ;
788- ring. dequeue_many ( 4 ) ;
793+ let buf_dequeued = ring. dequeue_many ( 4 ) ;
794+ assert_eq ! ( buf_dequeued, b"abcd" ) ;
789795 assert_eq ! ( ring. len( ) , 0 ) ;
790796
791797 let large = ring. enqueue_many ( 8 ) ;
0 commit comments