@@ -25,14 +25,9 @@ impl Assembler {
25
25
Self :: default ( )
26
26
}
27
27
28
- // Get the the next ordered chunk
29
- pub ( crate ) fn read (
30
- & mut self ,
31
- max_length : usize ,
32
- ordered : bool ,
33
- ) -> Result < Option < Chunk > , AssembleError > {
28
+ pub ( crate ) fn ensure_ordering ( & mut self , ordered : bool ) -> Result < ( ) , IllegalOrderedRead > {
34
29
if ordered && !self . state . is_ordered ( ) {
35
- return Err ( AssembleError :: IllegalOrderedRead ) ;
30
+ return Err ( IllegalOrderedRead ) ;
36
31
} else if !ordered && self . state . is_ordered ( ) {
37
32
// Enter unordered mode
38
33
let mut recvd = RangeSet :: new ( ) ;
@@ -42,17 +37,21 @@ impl Assembler {
42
37
}
43
38
self . state = State :: Unordered { recvd } ;
44
39
}
40
+ Ok ( ( ) )
41
+ }
45
42
43
+ /// Get the the next chunk
44
+ pub ( crate ) fn read ( & mut self , max_length : usize , ordered : bool ) -> Option < Chunk > {
46
45
loop {
47
46
let mut chunk = match self . data . peek_mut ( ) {
48
47
Some ( chunk) => chunk,
49
- None => return Ok ( None ) ,
48
+ None => return None ,
50
49
} ;
51
50
52
51
if ordered {
53
52
if chunk. offset > self . bytes_read {
54
53
// Next chunk is after current read index
55
- return Ok ( None ) ;
54
+ return None ;
56
55
} else if ( chunk. offset + chunk. bytes . len ( ) as u64 ) <= self . bytes_read {
57
56
// Next chunk is useless as the read index is beyond its end
58
57
PeekMut :: pop ( chunk) ;
@@ -68,7 +67,7 @@ impl Assembler {
68
67
}
69
68
}
70
69
71
- return Ok ( Some ( if max_length < chunk. bytes . len ( ) {
70
+ return Some ( if max_length < chunk. bytes . len ( ) {
72
71
self . bytes_read += max_length as u64 ;
73
72
let offset = chunk. offset ;
74
73
chunk. offset += max_length as u64 ;
@@ -78,7 +77,7 @@ impl Assembler {
78
77
self . defragmented = self . defragmented . saturating_sub ( 1 ) ;
79
78
let chunk = PeekMut :: pop ( chunk) ;
80
79
Chunk :: new ( chunk. offset , chunk. bytes )
81
- } ) ) ;
80
+ } ) ;
82
81
}
83
82
}
84
83
@@ -242,11 +241,8 @@ impl Default for State {
242
241
}
243
242
244
243
/// Error indicating that an ordered read was performed on a stream after an unordered read
245
- #[ derive( Debug , Copy , Clone ) ]
246
- pub enum AssembleError {
247
- IllegalOrderedRead ,
248
- UnknownStream ,
249
- }
244
+ #[ derive( Debug ) ]
245
+ pub struct IllegalOrderedRead ;
250
246
251
247
#[ cfg( test) ]
252
248
mod test {
@@ -272,6 +268,7 @@ mod test {
272
268
#[ test]
273
269
fn assemble_unordered ( ) {
274
270
let mut x = Assembler :: new ( ) ;
271
+ x. ensure_ordering ( false ) . unwrap ( ) ;
275
272
x. insert ( 3 , Bytes :: from_static ( b"456" ) ) ;
276
273
assert_matches ! ( next( & mut x, 32 ) , None ) ;
277
274
x. insert ( 0 , Bytes :: from_static ( b"123" ) ) ;
@@ -426,42 +423,44 @@ mod test {
426
423
x. insert ( 7 , Bytes :: from_static ( b"hij" ) ) ;
427
424
x. insert ( 11 , Bytes :: from_static ( b"lmn" ) ) ;
428
425
x. defragment ( ) ;
429
- assert_matches ! ( x. read( usize :: MAX , true ) , Ok ( Some ( ref y) ) if & y. bytes[ ..] == b"abcdef" ) ;
426
+ assert_matches ! ( x. read( usize :: MAX , true ) , Some ( ref y) if & y. bytes[ ..] == b"abcdef" ) ;
430
427
x. insert ( 5 , Bytes :: from_static ( b"fghijklmn" ) ) ;
431
- assert_matches ! ( x. read( usize :: MAX , true ) , Ok ( Some ( ref y) ) if & y. bytes[ ..] == b"ghijklmn" ) ;
428
+ assert_matches ! ( x. read( usize :: MAX , true ) , Some ( ref y) if & y. bytes[ ..] == b"ghijklmn" ) ;
432
429
x. insert ( 13 , Bytes :: from_static ( b"nopq" ) ) ;
433
- assert_matches ! ( x. read( usize :: MAX , true ) , Ok ( Some ( ref y) ) if & y. bytes[ ..] == b"opq" ) ;
430
+ assert_matches ! ( x. read( usize :: MAX , true ) , Some ( ref y) if & y. bytes[ ..] == b"opq" ) ;
434
431
x. insert ( 15 , Bytes :: from_static ( b"pqrs" ) ) ;
435
- assert_matches ! ( x. read( usize :: MAX , true ) , Ok ( Some ( ref y) ) if & y. bytes[ ..] == b"rs" ) ;
436
- assert_matches ! ( x. read( usize :: MAX , true ) , Ok ( None ) ) ;
432
+ assert_matches ! ( x. read( usize :: MAX , true ) , Some ( ref y) if & y. bytes[ ..] == b"rs" ) ;
433
+ assert_matches ! ( x. read( usize :: MAX , true ) , None ) ;
437
434
}
438
435
439
436
#[ test]
440
437
fn unordered_happy_path ( ) {
441
438
let mut x = Assembler :: new ( ) ;
439
+ x. ensure_ordering ( false ) . unwrap ( ) ;
442
440
x. insert ( 0 , Bytes :: from_static ( b"abc" ) ) ;
443
441
assert_eq ! (
444
442
next_unordered( & mut x) ,
445
443
Chunk :: new( 0 , Bytes :: from_static( b"abc" ) )
446
444
) ;
447
- assert_eq ! ( x. read( usize :: MAX , false ) . unwrap ( ) , None ) ;
445
+ assert_eq ! ( x. read( usize :: MAX , false ) , None ) ;
448
446
x. insert ( 3 , Bytes :: from_static ( b"def" ) ) ;
449
447
assert_eq ! (
450
448
next_unordered( & mut x) ,
451
449
Chunk :: new( 3 , Bytes :: from_static( b"def" ) )
452
450
) ;
453
- assert_eq ! ( x. read( usize :: MAX , false ) . unwrap ( ) , None ) ;
451
+ assert_eq ! ( x. read( usize :: MAX , false ) , None ) ;
454
452
}
455
453
456
454
#[ test]
457
455
fn unordered_dedup ( ) {
458
456
let mut x = Assembler :: new ( ) ;
457
+ x. ensure_ordering ( false ) . unwrap ( ) ;
459
458
x. insert ( 3 , Bytes :: from_static ( b"def" ) ) ;
460
459
assert_eq ! (
461
460
next_unordered( & mut x) ,
462
461
Chunk :: new( 3 , Bytes :: from_static( b"def" ) )
463
462
) ;
464
- assert_eq ! ( x. read( usize :: MAX , false ) . unwrap ( ) , None ) ;
463
+ assert_eq ! ( x. read( usize :: MAX , false ) , None ) ;
465
464
x. insert ( 0 , Bytes :: from_static ( b"a" ) ) ;
466
465
x. insert ( 0 , Bytes :: from_static ( b"abcdefghi" ) ) ;
467
466
x. insert ( 0 , Bytes :: from_static ( b"abcd" ) ) ;
@@ -477,54 +476,54 @@ mod test {
477
476
next_unordered( & mut x) ,
478
477
Chunk :: new( 6 , Bytes :: from_static( b"ghi" ) )
479
478
) ;
480
- assert_eq ! ( x. read( usize :: MAX , false ) . unwrap ( ) , None ) ;
479
+ assert_eq ! ( x. read( usize :: MAX , false ) , None ) ;
481
480
x. insert ( 8 , Bytes :: from_static ( b"ijkl" ) ) ;
482
481
assert_eq ! (
483
482
next_unordered( & mut x) ,
484
483
Chunk :: new( 9 , Bytes :: from_static( b"jkl" ) )
485
484
) ;
486
- assert_eq ! ( x. read( usize :: MAX , false ) . unwrap ( ) , None ) ;
485
+ assert_eq ! ( x. read( usize :: MAX , false ) , None ) ;
487
486
x. insert ( 12 , Bytes :: from_static ( b"mno" ) ) ;
488
487
assert_eq ! (
489
488
next_unordered( & mut x) ,
490
489
Chunk :: new( 12 , Bytes :: from_static( b"mno" ) )
491
490
) ;
492
- assert_eq ! ( x. read( usize :: MAX , false ) . unwrap ( ) , None ) ;
491
+ assert_eq ! ( x. read( usize :: MAX , false ) , None ) ;
493
492
x. insert ( 2 , Bytes :: from_static ( b"cde" ) ) ;
494
- assert_eq ! ( x. read( usize :: MAX , false ) . unwrap ( ) , None ) ;
493
+ assert_eq ! ( x. read( usize :: MAX , false ) , None ) ;
495
494
}
496
495
497
496
#[ test]
498
497
fn chunks_dedup ( ) {
499
498
let mut x = Assembler :: new ( ) ;
500
499
x. insert ( 3 , Bytes :: from_static ( b"def" ) ) ;
501
- assert_eq ! ( x. read( usize :: MAX , true ) . unwrap ( ) , None ) ;
500
+ assert_eq ! ( x. read( usize :: MAX , true ) , None ) ;
502
501
x. insert ( 0 , Bytes :: from_static ( b"a" ) ) ;
503
502
x. insert ( 1 , Bytes :: from_static ( b"bcdefghi" ) ) ;
504
503
x. insert ( 0 , Bytes :: from_static ( b"abcd" ) ) ;
505
504
assert_eq ! (
506
- x. read( usize :: MAX , true ) . unwrap ( ) ,
505
+ x. read( usize :: MAX , true ) ,
507
506
Some ( Chunk :: new( 0 , Bytes :: from_static( b"abcd" ) ) )
508
507
) ;
509
508
assert_eq ! (
510
- x. read( usize :: MAX , true ) . unwrap ( ) ,
509
+ x. read( usize :: MAX , true ) ,
511
510
Some ( Chunk :: new( 4 , Bytes :: from_static( b"efghi" ) ) )
512
511
) ;
513
- assert_eq ! ( x. read( usize :: MAX , true ) . unwrap ( ) , None ) ;
512
+ assert_eq ! ( x. read( usize :: MAX , true ) , None ) ;
514
513
x. insert ( 8 , Bytes :: from_static ( b"ijkl" ) ) ;
515
514
assert_eq ! (
516
- x. read( usize :: MAX , true ) . unwrap ( ) ,
515
+ x. read( usize :: MAX , true ) ,
517
516
Some ( Chunk :: new( 9 , Bytes :: from_static( b"jkl" ) ) )
518
517
) ;
519
- assert_eq ! ( x. read( usize :: MAX , true ) . unwrap ( ) , None ) ;
518
+ assert_eq ! ( x. read( usize :: MAX , true ) , None ) ;
520
519
x. insert ( 12 , Bytes :: from_static ( b"mno" ) ) ;
521
520
assert_eq ! (
522
- x. read( usize :: MAX , true ) . unwrap ( ) ,
521
+ x. read( usize :: MAX , true ) ,
523
522
Some ( Chunk :: new( 12 , Bytes :: from_static( b"mno" ) ) )
524
523
) ;
525
- assert_eq ! ( x. read( usize :: MAX , true ) . unwrap ( ) , None ) ;
524
+ assert_eq ! ( x. read( usize :: MAX , true ) , None ) ;
526
525
x. insert ( 2 , Bytes :: from_static ( b"cde" ) ) ;
527
- assert_eq ! ( x. read( usize :: MAX , true ) . unwrap ( ) , None ) ;
526
+ assert_eq ! ( x. read( usize :: MAX , true ) , None ) ;
528
527
}
529
528
530
529
#[ test]
@@ -533,7 +532,7 @@ mod test {
533
532
x. insert ( 0 , Bytes :: from_static ( b"abc" ) ) ;
534
533
assert_eq ! ( x. data. len( ) , 1 ) ;
535
534
assert_eq ! (
536
- x. read( usize :: MAX , true ) . unwrap ( ) ,
535
+ x. read( usize :: MAX , true ) ,
537
536
Some ( Chunk :: new( 0 , Bytes :: from_static( b"abc" ) ) )
538
537
) ;
539
538
x. insert ( 0 , Bytes :: from_static ( b"ab" ) ) ;
@@ -549,10 +548,10 @@ mod test {
549
548
}
550
549
551
550
fn next_unordered ( x : & mut Assembler ) -> Chunk {
552
- x. read ( usize:: MAX , false ) . unwrap ( ) . unwrap ( )
551
+ x. read ( usize:: MAX , false ) . unwrap ( )
553
552
}
554
553
555
554
fn next ( x : & mut Assembler , size : usize ) -> Option < Bytes > {
556
- x. read ( size, true ) . unwrap ( ) . map ( |chunk| chunk. bytes )
555
+ x. read ( size, true ) . map ( |chunk| chunk. bytes )
557
556
}
558
557
}
0 commit comments