Skip to content

Commit 24fa299

Browse files
bors[bot]luojia65
andauthored
Merge #561
561: Add lint `#[must_use]` for ring buffer functions r=Dirbaio a=luojia65 This pull request adds `#[must_use]` flag on several ring buffer functions that have side-effects. This lint is in stable Rust now: rust-lang/rust#43302, these changes are noted according to code comment from `@whitequark:` f65bc8a. Some test cases and functions are altered due to changes of `#[must_use]`. Co-authored-by: luojia65 <[email protected]>
2 parents d5af245 + 7a0df7d commit 24fa299

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

src/socket/tcp.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1875,8 +1875,10 @@ impl<'a> TcpSocket<'a> {
18751875
payload_len,
18761876
payload_offset
18771877
);
1878-
self.rx_buffer
1878+
let len_written = self
1879+
.rx_buffer
18791880
.write_unallocated(payload_offset, repr.payload);
1881+
debug_assert!(len_written == payload_len);
18801882
}
18811883
Err(_) => {
18821884
net_debug!(

src/storage/packet_buffer.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ impl<'a, H> PacketBuffer<'a, H> {
102102
// Add padding to the end of the ring buffer so that the
103103
// contiguous window is at the beginning of the ring buffer.
104104
*self.metadata_ring.enqueue_one()? = PacketMetadata::padding(contig_window);
105-
self.payload_ring.enqueue_many(contig_window);
105+
// note(discard): function does not write to the result
106+
// enqueued padding buffer location
107+
let _buf_enqueued = self.payload_ring.enqueue_many(contig_window);
106108
}
107109
}
108110

@@ -121,7 +123,8 @@ impl<'a, H> PacketBuffer<'a, H> {
121123

122124
let _ = metadata_ring.dequeue_one_with(|metadata| {
123125
if metadata.is_padding() {
124-
payload_ring.dequeue_many(metadata.size);
126+
// note(discard): function does not use value of dequeued padding bytes
127+
let _buf_dequeued = payload_ring.dequeue_many(metadata.size);
125128
Ok(()) // dequeue metadata
126129
} else {
127130
Err(Error::Exhausted) // don't dequeue metadata

src/storage/ring_buffer.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
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

45
use 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> {
294295
impl<'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

Comments
 (0)