Skip to content

Commit 9f071a6

Browse files
committed
chore: polishing
1 parent 6373089 commit 9f071a6

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

crates/http/src/codec/header/header_encoder.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
1414
use crate::protocol::{PayloadSize, ResponseHead, SendError};
1515

16-
use bytes::{BufMut, Bytes, BytesMut};
16+
use bytes::{BufMut, BytesMut};
1717

1818
use http::{HeaderValue, Version, header};
1919
use std::io;
@@ -74,25 +74,27 @@ impl Encoder<(ResponseHead, PayloadSize)> for HeaderEncoder {
7474
}
7575
},
7676

77-
PayloadSize::Chunked => match header.headers_mut().get_mut(header::TRANSFER_ENCODING) {
78-
Some(value) => *value = unsafe { HeaderValue::from_maybe_shared_unchecked(Bytes::from_static("chunked".as_bytes())) },
79-
None => {
80-
header.headers_mut().insert(header::TRANSFER_ENCODING, unsafe {
81-
HeaderValue::from_maybe_shared_unchecked(Bytes::from_static("chunked".as_bytes()))
82-
});
77+
PayloadSize::Chunked => {
78+
const CHUNKED: HeaderValue = HeaderValue::from_static("chunked");
79+
80+
match header.headers_mut().get_mut(header::TRANSFER_ENCODING) {
81+
Some(value) => *value = CHUNKED,
82+
None => {
83+
header.headers_mut().insert(header::TRANSFER_ENCODING, CHUNKED);
84+
}
8385
}
8486
},
85-
PayloadSize::Empty => match header.headers_mut().get_mut(header::CONTENT_LENGTH) {
86-
Some(value) => *value = 0.into(),
87-
None => {
88-
const ZERO_VALUE: HeaderValue = HeaderValue::from_static("0");
89-
header.headers_mut().insert(header::CONTENT_LENGTH, ZERO_VALUE);
90-
}
87+
88+
PayloadSize::Empty => if let Some(value) = header.headers_mut().get_mut(header::CONTENT_LENGTH) {
89+
*value = 0.into();
90+
} else {
91+
const ZERO_VALUE: HeaderValue = HeaderValue::from_static("0");
92+
header.headers_mut().insert(header::CONTENT_LENGTH, ZERO_VALUE);
9193
},
9294
}
9395

9496
// Write all headers
95-
for (header_name, header_value) in header.headers().iter() {
97+
for (header_name, header_value) in header.headers() {
9698
dst.put_slice(header_name.as_ref());
9799
dst.put_slice(b": ");
98100
dst.put_slice(header_value.as_ref());
@@ -103,7 +105,7 @@ impl Encoder<(ResponseHead, PayloadSize)> for HeaderEncoder {
103105
}
104106
}
105107

106-
/// Fast writer implementation for writing to BytesMut.
108+
/// Fast writer implementation for writing to [`BytesMut`].
107109
///
108110
/// This is an optimization to avoid unnecessary bounds checking when writing
109111
/// to the bytes buffer, since we've already reserved enough space.

crates/http/src/codec/response_encoder.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use std::io::ErrorKind;
3030
use tokio_util::codec::Encoder;
3131
use tracing::error;
3232

33-
/// A encoder for HTTP responses that handles both headers and payload
33+
/// An encoder for HTTP responses that handles both headers and payload
3434
///
3535
/// The encoder operates in two phases:
3636
/// 1. Header encoding: Encodes the response headers using [`HeaderEncoder`]
@@ -45,8 +45,9 @@ pub struct ResponseEncoder {
4545

4646
impl ResponseEncoder {
4747
/// Creates a new `ResponseEncoder` instance
48+
#[must_use]
4849
pub fn new() -> Self {
49-
Default::default()
50+
ResponseEncoder::default()
5051
}
5152
}
5253

@@ -88,9 +89,7 @@ impl<D: Buf> Encoder<Message<(ResponseHead, PayloadSize), D>> for ResponseEncode
8889

8990
Message::Payload(payload_item) => {
9091
// Get the payload encoder, return error if it doesn't exist
91-
let payload_encoder = if let Some(encoder) = &mut self.payload_encoder {
92-
encoder
93-
} else {
92+
let Some(payload_encoder) = &mut self.payload_encoder else {
9493
error!("expect response header but receive payload item");
9594
return Err(io::Error::from(ErrorKind::InvalidInput).into());
9695
};

0 commit comments

Comments
 (0)