@@ -12,9 +12,10 @@ use crate::{
12
12
} ;
13
13
use core:: convert:: Infallible ;
14
14
15
- /// A split duplex that tries to write as much as possible to `storage`, while falling back to
16
- /// `duplex`.
17
- pub struct Split < ' a , S , D >
15
+ /// A wrapper around an underlying buffer (`duplex`) which will prefer to read/write from a
16
+ /// user-provided temporary buffer (`storage`). The underlying buffer (`duplex`)'s current
17
+ /// position and total length are updated if needed.
18
+ pub struct Interposer < ' a , S , D >
18
19
where
19
20
S : writer:: Storage + ?Sized ,
20
21
D : duplex:: Skip < Error = Infallible > + ?Sized ,
@@ -23,19 +24,24 @@ where
23
24
duplex : & ' a mut D ,
24
25
}
25
26
26
- impl < ' a , S , D > Split < ' a , S , D >
27
+ impl < ' a , S , D > Interposer < ' a , S , D >
27
28
where
28
29
S : writer:: Storage + ?Sized ,
29
30
D : duplex:: Skip < Error = Infallible > + ?Sized ,
30
31
{
31
32
#[ inline]
32
33
pub fn new ( storage : & ' a mut S , duplex : & ' a mut D ) -> Self {
34
+ debug_assert ! (
35
+ !storage. has_remaining_capacity( ) || duplex. buffer_is_empty( ) ,
36
+ "`duplex` should be drained into `storage` before constructing an Interposer"
37
+ ) ;
38
+
33
39
Self { storage, duplex }
34
40
}
35
41
}
36
42
37
43
/// Delegates to the inner Duplex
38
- impl < ' a , S , D > reader:: Storage for Split < ' a , S , D >
44
+ impl < ' a , S , D > reader:: Storage for Interposer < ' a , S , D >
39
45
where
40
46
S : writer:: Storage + ?Sized ,
41
47
D : duplex:: Skip < Error = Infallible > + ?Sized ,
78
84
}
79
85
80
86
/// Delegates to the inner Duplex
81
- impl < ' a , C , D > Reader for Split < ' a , C , D >
87
+ impl < ' a , C , D > Reader for Interposer < ' a , C , D >
82
88
where
83
89
C : writer:: Storage + ?Sized ,
84
90
D : duplex:: Skip < Error = Infallible > + ?Sized ,
@@ -104,7 +110,7 @@ where
104
110
}
105
111
}
106
112
107
- impl < ' a , C , D > Writer for Split < ' a , C , D >
113
+ impl < ' a , C , D > Writer for Interposer < ' a , C , D >
108
114
where
109
115
C : writer:: Storage + ?Sized ,
110
116
D : duplex:: Skip < Error = Infallible > + ?Sized ,
@@ -121,7 +127,7 @@ where
121
127
// receive buffer, since that's what it stores
122
128
let mut should_delegate = C :: SPECIALIZES_BYTES || C :: SPECIALIZES_BYTES_MUT ;
123
129
124
- // if the storage is empty then write into the duplex
130
+ // if the storage has no space left then write into the duplex
125
131
should_delegate |= !self . storage . has_remaining_capacity ( ) ;
126
132
127
133
// if this packet is non-contiguous, then delegate to the wrapped writer
@@ -192,9 +198,9 @@ mod tests {
192
198
// limit the storage capacity so we force writing into the duplex
193
199
let mut storage = storage. with_write_limit ( 1 ) ;
194
200
195
- let mut split = Split :: new ( & mut storage, & mut duplex) ;
201
+ let mut interposer = Interposer :: new ( & mut storage, & mut duplex) ;
196
202
197
- split . read_from ( & mut reader) . unwrap ( ) ;
203
+ interposer . read_from ( & mut reader) . unwrap ( ) ;
198
204
}
199
205
200
206
// the storage was too small so we delegated to duplex
@@ -220,15 +226,15 @@ mod tests {
220
226
221
227
let mut storage: Vec < u8 > = vec ! [ ] ;
222
228
223
- let mut split = Split :: new ( & mut storage, & mut duplex) ;
229
+ let mut interposer = Interposer :: new ( & mut storage, & mut duplex) ;
224
230
225
- split . read_from ( & mut reader) . unwrap ( ) ;
231
+ interposer . read_from ( & mut reader) . unwrap ( ) ;
226
232
227
233
// make sure we consumed the reader
228
234
assert_eq ! ( reader. current_offset( ) . as_u64( ) , 10 ) ;
229
235
230
- assert_eq ! ( split . current_offset( ) . as_u64( ) , 0 ) ;
231
- assert_eq ! ( split . buffered_len( ) , 0 ) ;
236
+ assert_eq ! ( interposer . current_offset( ) . as_u64( ) , 0 ) ;
237
+ assert_eq ! ( interposer . buffered_len( ) , 0 ) ;
232
238
233
239
// make sure we didn't write to the storage, even if we had capacity, since the
234
240
// current_offset doesn't match
@@ -241,15 +247,15 @@ mod tests {
241
247
242
248
let mut storage: Vec < u8 > = vec ! [ ] ;
243
249
244
- let mut split = Split :: new ( & mut storage, & mut duplex) ;
250
+ let mut interposer = Interposer :: new ( & mut storage, & mut duplex) ;
245
251
246
- split . read_from ( & mut reader) . unwrap ( ) ;
252
+ interposer . read_from ( & mut reader) . unwrap ( ) ;
247
253
248
254
// make sure we consumed the reader
249
255
assert_eq ! ( reader. current_offset( ) . as_u64( ) , 10 ) ;
250
256
251
- assert_eq ! ( split . current_offset( ) . as_u64( ) , 10 ) ;
252
- assert_eq ! ( split . buffered_len( ) , 0 ) ;
257
+ assert_eq ! ( interposer . current_offset( ) . as_u64( ) , 10 ) ;
258
+ assert_eq ! ( interposer . buffered_len( ) , 0 ) ;
253
259
254
260
// make sure we copied the entire reader
255
261
assert_eq ! ( storage. len( ) , 10 ) ;
@@ -265,9 +271,9 @@ mod tests {
265
271
266
272
let mut storage: Vec < u8 > = vec ! [ ] ;
267
273
268
- let mut split = Split :: new ( & mut storage, & mut duplex) ;
274
+ let mut interposer = Interposer :: new ( & mut storage, & mut duplex) ;
269
275
270
- split . read_from ( & mut reader) . unwrap ( ) ;
276
+ interposer . read_from ( & mut reader) . unwrap ( ) ;
271
277
272
278
assert_eq ! ( storage. len( ) , 10 ) ;
273
279
assert_eq ! ( duplex. current_offset( ) . as_u64( ) , 10 ) ;
@@ -283,19 +289,19 @@ mod tests {
283
289
284
290
let mut storage = writer:: storage:: Empty ;
285
291
286
- let mut split = Split :: new ( & mut storage, & mut duplex) ;
292
+ let mut interposer = Interposer :: new ( & mut storage, & mut duplex) ;
287
293
288
- split . read_from ( & mut reader) . unwrap ( ) ;
294
+ interposer . read_from ( & mut reader) . unwrap ( ) ;
289
295
290
- assert_eq ! ( split . current_offset( ) . as_u64( ) , 0 ) ;
291
- assert_eq ! ( split . buffered_len( ) , 10 ) ;
296
+ assert_eq ! ( interposer . current_offset( ) . as_u64( ) , 0 ) ;
297
+ assert_eq ! ( interposer . buffered_len( ) , 10 ) ;
292
298
293
- checker. read_from ( & mut split ) . unwrap ( ) ;
299
+ checker. read_from ( & mut interposer ) . unwrap ( ) ;
294
300
295
- assert_eq ! ( split . current_offset( ) . as_u64( ) , 10 ) ;
296
- assert ! ( split . buffer_is_empty( ) ) ;
297
- assert_eq ! ( split . buffered_len( ) , 0 ) ;
298
- assert ! ( split . is_consumed( ) ) ;
301
+ assert_eq ! ( interposer . current_offset( ) . as_u64( ) , 10 ) ;
302
+ assert ! ( interposer . buffer_is_empty( ) ) ;
303
+ assert_eq ! ( interposer . buffered_len( ) , 0 ) ;
304
+ assert ! ( interposer . is_consumed( ) ) ;
299
305
}
300
306
301
307
#[ test]
@@ -308,9 +314,9 @@ mod tests {
308
314
{
309
315
let mut storage = storage. with_write_limit ( 9 ) ;
310
316
311
- let mut split = Split :: new ( & mut storage, & mut duplex) ;
317
+ let mut interposer = Interposer :: new ( & mut storage, & mut duplex) ;
312
318
313
- split . read_from ( & mut reader) . unwrap ( ) ;
319
+ interposer . read_from ( & mut reader) . unwrap ( ) ;
314
320
}
315
321
316
322
// the storage was at least half the reader
0 commit comments