1
+ use std:: cmp;
1
2
use std:: collections:: vec_deque;
2
3
use std:: collections:: VecDeque ;
3
4
use std:: io:: IoSlice ;
@@ -7,6 +8,7 @@ use std::ops::DerefMut;
7
8
8
9
use bytes:: Buf ;
9
10
use bytes:: Bytes ;
11
+ use bytes:: BytesMut ;
10
12
11
13
use crate :: bytes_ext:: buf_get_bytes:: BufGetBytes ;
12
14
@@ -117,8 +119,8 @@ impl<B: Buf> Buf for BufVecDeque<B> {
117
119
fn copy_to_bytes ( & mut self , cnt : usize ) -> Bytes {
118
120
assert ! ( cnt <= self . remaining( ) ) ;
119
121
120
- match self . deque . front_mut ( ) {
121
- Some ( front ) if front. remaining ( ) >= cnt => {
122
+ if let Some ( front ) = self . deque . front_mut ( ) {
123
+ if front. remaining ( ) >= cnt {
122
124
let r = if front. remaining ( ) == cnt {
123
125
let mut front = self . deque . pop_front ( ) . unwrap ( ) ;
124
126
front. copy_to_bytes ( cnt)
@@ -127,33 +129,31 @@ impl<B: Buf> Buf for BufVecDeque<B> {
127
129
} ;
128
130
129
131
self . len -= cnt;
130
- r
132
+ return r ;
131
133
}
132
- Some ( _) => self . take ( cnt) . copy_to_bytes ( cnt) ,
133
- None => Bytes :: new ( ) ,
134
134
}
135
+
136
+ let mut bytes = BytesMut :: with_capacity ( cnt) ;
137
+ while bytes. len ( ) < cnt {
138
+ let need = cnt - bytes. len ( ) ;
139
+ let front = self . deque . front_mut ( ) . unwrap ( ) ;
140
+ let copy = cmp:: min ( front. chunk ( ) . len ( ) , need) ;
141
+ bytes. extend_from_slice ( & front. chunk ( ) [ ..copy] ) ;
142
+ front. advance ( copy) ;
143
+ if !front. has_remaining ( ) {
144
+ let front = self . deque . pop_front ( ) . unwrap ( ) ;
145
+ assert ! ( !front. has_remaining( ) ) ;
146
+ }
147
+ self . len -= copy;
148
+ }
149
+ assert_eq ! ( bytes. len( ) , cnt) ;
150
+ bytes. freeze ( )
135
151
}
136
152
}
137
153
138
154
impl < B : BufGetBytes > BufGetBytes for BufVecDeque < B > {
139
155
fn get_bytes ( & mut self , cnt : usize ) -> Bytes {
140
- assert ! ( cnt <= self . remaining( ) ) ;
141
-
142
- match self . deque . front_mut ( ) {
143
- Some ( front) if front. remaining ( ) >= cnt => {
144
- let r = if front. remaining ( ) == cnt {
145
- let mut front = self . deque . pop_front ( ) . unwrap ( ) ;
146
- front. get_bytes ( cnt)
147
- } else {
148
- front. get_bytes ( cnt)
149
- } ;
150
-
151
- self . len -= cnt;
152
- r
153
- }
154
- Some ( _) => self . take ( cnt) . copy_to_bytes ( cnt) ,
155
- None => Bytes :: new ( ) ,
156
- }
156
+ self . copy_to_bytes ( cnt)
157
157
}
158
158
}
159
159
0 commit comments