1313
1414use crate :: protocol:: { PayloadSize , ResponseHead , SendError } ;
1515
16- use bytes:: { BufMut , Bytes , BytesMut } ;
16+ use bytes:: { BufMut , BytesMut } ;
1717
1818use http:: { HeaderValue , Version , header} ;
1919use 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.
0 commit comments