@@ -78,7 +78,9 @@ impl<T: AsBuffer> Kawa<T> {
78
78
pub fn prepare < C : BlockConverter < T > > ( & mut self , converter : & mut C ) {
79
79
converter. initialize ( self ) ;
80
80
while let Some ( block) = self . blocks . pop_front ( ) {
81
- converter. call ( block, self ) ;
81
+ if !converter. call ( block, self ) {
82
+ break ;
83
+ }
82
84
}
83
85
converter. finalize ( self ) ;
84
86
}
@@ -109,8 +111,8 @@ impl<T: AsBuffer> Kawa<T> {
109
111
/// note: this function assumes blocks is empty! To respect this invariant you should always
110
112
/// call prepare before consume
111
113
pub fn consume ( & mut self , mut amount : usize ) {
112
- assert ! ( self . blocks. is_empty( ) ) ;
113
- assert ! ( self . detached. jar. is_empty( ) ) ;
114
+ // assert!(self.blocks.is_empty());
115
+ // assert!(self.detached.jar.is_empty());
114
116
if amount > 0 {
115
117
self . consumed = true ;
116
118
}
@@ -142,7 +144,12 @@ impl<T: AsBuffer> Kawa<T> {
142
144
return slice. start as usize ;
143
145
}
144
146
}
145
- self . storage . head
147
+ if self . blocks . is_empty ( ) {
148
+ // conservative estimate
149
+ self . storage . head
150
+ } else {
151
+ self . storage . start
152
+ }
146
153
}
147
154
148
155
pub fn push_block ( & mut self , block : Block ) {
@@ -487,6 +494,17 @@ impl Store {
487
494
}
488
495
}
489
496
497
+ pub fn len ( & self ) -> usize {
498
+ match self {
499
+ Store :: Empty => 0 ,
500
+ Store :: Slice ( s) | Store :: Detached ( s) => s. len ( ) ,
501
+ Store :: Static ( s) => s. len ( ) ,
502
+ Store :: Alloc ( s, i) => s. len ( ) - * i as usize ,
503
+ #[ cfg( feature = "rc-alloc" ) ]
504
+ Store :: Shared ( s, i) => s. len ( ) - * i as usize ,
505
+ }
506
+ }
507
+
490
508
pub fn is_empty ( & self ) -> bool {
491
509
matches ! ( self , Store :: Empty )
492
510
}
@@ -539,6 +557,37 @@ impl Store {
539
557
}
540
558
}
541
559
560
+ pub fn split ( self , at : usize ) -> ( Store , Store ) {
561
+ let at32 = at as u32 ;
562
+ match self {
563
+ Store :: Empty => ( Store :: Empty , Store :: Empty ) ,
564
+ Store :: Slice ( Slice { start, len } ) => (
565
+ Store :: Slice ( Slice { start, len : at32 } ) ,
566
+ Store :: Slice ( Slice {
567
+ start : start + at32,
568
+ len : len - at32,
569
+ } ) ,
570
+ ) ,
571
+ Store :: Detached ( Slice { start, len } ) => (
572
+ Store :: Detached ( Slice { start, len : at32 } ) ,
573
+ Store :: Detached ( Slice {
574
+ start : start + at32,
575
+ len : len - at32,
576
+ } ) ,
577
+ ) ,
578
+ Store :: Static ( s) => ( Store :: Static ( & s[ ..at] ) , Store :: Static ( & s[ at..] ) ) ,
579
+ Store :: Alloc ( s, i) => (
580
+ Store :: from_slice ( & s[ i as usize ..i as usize + at] ) ,
581
+ Store :: Alloc ( s, i + at32) ,
582
+ ) ,
583
+ #[ cfg( feature = "rc-alloc" ) ]
584
+ Store :: Shared ( s, i) => (
585
+ Store :: from_slice ( & s[ i as usize ..i as usize + at] ) ,
586
+ Store :: Shared ( s, i + at32) ,
587
+ ) ,
588
+ }
589
+ }
590
+
542
591
fn consume ( self , amount : usize ) -> ( usize , Option < Store > ) {
543
592
match self {
544
593
Store :: Empty => ( amount, None ) ,
0 commit comments