From 4d66d5d4382aa5ba4cca17460a68d290bb98a21b Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Sat, 2 Nov 2024 15:34:14 +0100 Subject: [PATCH] style: replace `split_to` and `split_off` with better alternatives This removes `let _ = ` from in front of `split_to` and `split_off` and mostly follows the suggestions from the `#[must_use]` impls. One of the uses of `split_to` is instead replaced with `take`. --- src/codec/framed_read.rs | 7 +++---- src/frame/headers.rs | 12 ++++++------ src/frame/util.rs | 6 +++--- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/codec/framed_read.rs b/src/codec/framed_read.rs index 9270a8635..edb8d9548 100644 --- a/src/codec/framed_read.rs +++ b/src/codec/framed_read.rs @@ -8,7 +8,7 @@ use crate::hpack; use futures_core::Stream; -use bytes::BytesMut; +use bytes::{Buf, BytesMut}; use std::io; @@ -146,8 +146,7 @@ fn decode_frame( macro_rules! header_block { ($frame:ident, $head:ident, $bytes:ident) => ({ // Drop the frame header - // TODO: Change to drain: carllerche/bytes#130 - let _ = $bytes.split_to(frame::HEADER_LEN); + $bytes.advance(frame::HEADER_LEN); // Parse the header frame w/o parsing the payload let (mut frame, mut payload) = match frame::$frame::load($head, $bytes) { @@ -227,7 +226,7 @@ fn decode_frame( .into() } Kind::Data => { - let _ = bytes.split_to(frame::HEADER_LEN); + bytes.advance(frame::HEADER_LEN); let res = frame::Data::load(head, bytes.freeze()); // TODO: Should this always be connection level? Probably not... diff --git a/src/frame/headers.rs b/src/frame/headers.rs index 0c756325f..8411a3aa9 100644 --- a/src/frame/headers.rs +++ b/src/frame/headers.rs @@ -6,7 +6,7 @@ use crate::hpack::{self, BytesStr}; use http::header::{self, HeaderName, HeaderValue}; use http::{uri, HeaderMap, Method, Request, StatusCode, Uri}; -use bytes::{BufMut, Bytes, BytesMut}; +use bytes::{Buf, BufMut, Bytes, BytesMut}; use std::fmt; use std::io::Cursor; @@ -166,7 +166,7 @@ impl Headers { pad = src[0] as usize; // Drop the padding - let _ = src.split_to(1); + src.advance(1); } // Read the stream dependency @@ -181,7 +181,7 @@ impl Headers { } // Drop the next 5 bytes - let _ = src.split_to(5); + src.advance(5); Some(stream_dep) } else { @@ -425,7 +425,7 @@ impl PushPromise { pad = src[0] as usize; // Drop the padding - let _ = src.split_to(1); + src.advance(1); } if src.len() < 5 { @@ -434,7 +434,7 @@ impl PushPromise { let (promised_id, _) = StreamId::parse(&src[..4]); // Drop promised_id bytes - let _ = src.split_to(4); + src.advance(4); if pad > 0 { if pad > src.len() { @@ -657,7 +657,7 @@ impl EncodingHeaderBlock { // Now, encode the header payload let continuation = if self.hpack.len() > dst.remaining_mut() { - dst.put_slice(&self.hpack.split_to(dst.remaining_mut())); + dst.put((&mut self.hpack).take(dst.remaining_mut())); Some(Continuation { stream_id: head.stream_id(), diff --git a/src/frame/util.rs b/src/frame/util.rs index 6bee7bd9b..3861aa2d3 100644 --- a/src/frame/util.rs +++ b/src/frame/util.rs @@ -1,7 +1,7 @@ use std::fmt; use super::Error; -use bytes::Bytes; +use bytes::{Buf, Bytes}; /// Strip padding from the given payload. /// @@ -32,8 +32,8 @@ pub fn strip_padding(payload: &mut Bytes) -> Result { return Err(Error::TooMuchPadding); } - let _ = payload.split_to(1); - let _ = payload.split_off(payload_len - pad_len - 1); + payload.advance(1); + payload.truncate(payload_len - pad_len - 1); Ok(pad_len as u8) }