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].
2
3
// [RFC 1940]: https://github.com/rust-lang/rust/issues/43302
3
4
4
5
use core:: cmp;
@@ -202,7 +203,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
202
203
///
203
204
/// This function may return a slice smaller than the given size
204
205
/// if the free space in the buffer is not contiguous.
205
- // #[must_use]
206
+ #[ must_use]
206
207
pub fn enqueue_many ( & mut self , size : usize ) -> & mut [ T ] {
207
208
self . enqueue_many_with ( |buf| {
208
209
let size = cmp:: min ( size, buf. len ( ) ) ;
@@ -213,7 +214,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
213
214
214
215
/// Enqueue as many elements from the given slice into the buffer as possible,
215
216
/// and return the amount of elements that could fit.
216
- // #[must_use]
217
+ #[ must_use]
217
218
pub fn enqueue_slice ( & mut self , data : & [ T ] ) -> usize
218
219
where
219
220
T : Copy ,
@@ -259,7 +260,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
259
260
///
260
261
/// This function may return a slice smaller than the given size
261
262
/// if the allocated space in the buffer is not contiguous.
262
- // #[must_use]
263
+ #[ must_use]
263
264
pub fn dequeue_many ( & mut self , size : usize ) -> & mut [ T ] {
264
265
self . dequeue_many_with ( |buf| {
265
266
let size = cmp:: min ( size, buf. len ( ) ) ;
@@ -270,7 +271,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
270
271
271
272
/// Dequeue as many elements from the buffer into the given slice as possible,
272
273
/// and return the amount of elements that could fit.
273
- // #[must_use]
274
+ #[ must_use]
274
275
pub fn dequeue_slice ( & mut self , data : & mut [ T ] ) -> usize
275
276
where
276
277
T : Copy ,
@@ -294,7 +295,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
294
295
impl < ' a , T : ' a > RingBuffer < ' a , T > {
295
296
/// Return the largest contiguous slice of unallocated buffer elements starting
296
297
/// at the given offset past the last allocated element, and up to the given size.
297
- // #[must_use]
298
+ #[ must_use]
298
299
pub fn get_unallocated ( & mut self , offset : usize , mut size : usize ) -> & mut [ T ] {
299
300
let start_at = self . get_idx ( self . length + offset) ;
300
301
// We can't access past the end of unallocated data.
@@ -318,7 +319,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
318
319
/// Write as many elements from the given slice into unallocated buffer elements
319
320
/// starting at the given offset past the last allocated element, and return
320
321
/// the amount written.
321
- // #[must_use]
322
+ #[ must_use]
322
323
pub fn write_unallocated ( & mut self , offset : usize , data : & [ T ] ) -> usize
323
324
where
324
325
T : Copy ,
@@ -349,7 +350,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
349
350
350
351
/// Return the largest contiguous slice of allocated buffer elements starting
351
352
/// at the given offset past the first allocated element, and up to the given size.
352
- // #[must_use]
353
+ #[ must_use]
353
354
pub fn get_allocated ( & self , offset : usize , mut size : usize ) -> & [ T ] {
354
355
let start_at = self . get_idx ( offset) ;
355
356
// We can't read past the end of the allocated data.
@@ -373,7 +374,7 @@ impl<'a, T: 'a> RingBuffer<'a, T> {
373
374
/// Read as many elements from allocated buffer elements into the given slice
374
375
/// starting at the given offset past the first allocated element, and return
375
376
/// the amount read.
376
- // #[must_use]
377
+ #[ must_use]
377
378
pub fn read_allocated ( & mut self , offset : usize , data : & mut [ T ] ) -> usize
378
379
where
379
380
T : Copy ,
@@ -686,7 +687,8 @@ mod test {
686
687
}
687
688
assert_eq ! ( & ring. storage[ ..] , b"abcd........" ) ;
688
689
689
- ring. enqueue_many ( 4 ) ;
690
+ let buf_enqueued = ring. enqueue_many ( 4 ) ;
691
+ assert_eq ! ( buf_enqueued. len( ) , 4 ) ;
690
692
assert_eq ! ( ring. len( ) , 4 ) ;
691
693
692
694
{
@@ -730,15 +732,18 @@ mod test {
730
732
assert_eq ! ( ring. get_allocated( 16 , 4 ) , b"" ) ;
731
733
assert_eq ! ( ring. get_allocated( 0 , 4 ) , b"" ) ;
732
734
733
- ring. enqueue_slice ( b"abcd" ) ;
735
+ let len_enqueued = ring. enqueue_slice ( b"abcd" ) ;
734
736
assert_eq ! ( ring. get_allocated( 0 , 8 ) , b"abcd" ) ;
737
+ assert_eq ! ( len_enqueued, 4 ) ;
735
738
736
- ring. enqueue_slice ( b"efghijkl" ) ;
739
+ let len_enqueued = ring. enqueue_slice ( b"efghijkl" ) ;
737
740
ring. dequeue_many ( 4 ) . copy_from_slice ( b"...." ) ;
738
741
assert_eq ! ( ring. get_allocated( 4 , 8 ) , b"ijkl" ) ;
742
+ assert_eq ! ( len_enqueued, 8 ) ;
739
743
740
- ring. enqueue_slice ( b"abcd" ) ;
744
+ let len_enqueued = ring. enqueue_slice ( b"abcd" ) ;
741
745
assert_eq ! ( ring. get_allocated( 4 , 8 ) , b"ijkl" ) ;
746
+ assert_eq ! ( len_enqueued, 4 ) ;
742
747
}
743
748
744
749
#[ test]
@@ -782,10 +787,11 @@ mod test {
782
787
#[ test]
783
788
fn test_buffer_write_wholly ( ) {
784
789
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 " ) ;
787
792
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" ) ;
789
795
assert_eq ! ( ring. len( ) , 0 ) ;
790
796
791
797
let large = ring. enqueue_many ( 8 ) ;
0 commit comments