@@ -35,15 +35,6 @@ pub struct Kawa<T: AsBuffer> {
35
35
pub consumed : bool ,
36
36
}
37
37
38
- /// Separate the content of the StatusLine and the crumbs from all the cookies from the stream of
39
- /// Blocks. It allows better indexing, persistance and reordering of data. However it is a double
40
- /// edge sword as it currently enables some unwanted/unsafe behavior such as Slice desync and over
41
- /// consuming.
42
- pub struct DetachedBlocks {
43
- pub status_line : StatusLine ,
44
- pub jar : VecDeque < Pair > ,
45
- }
46
-
47
38
impl < T : AsBuffer > Kawa < T > {
48
39
/// Create a new Kawa struct around a given storage.
49
40
///
@@ -97,7 +88,7 @@ impl<T: AsBuffer> Kawa<T> {
97
88
///
98
89
/// note: until you drop the resulting vector, Rust will prevent mutably borrowing Kawa as the
99
90
/// IoSlices keep a reference in the out vector. As always, nothing is copied.
100
- pub fn as_io_slice ( & mut self ) -> Vec < IoSlice > {
91
+ pub fn as_io_slice ( & self ) -> Vec < IoSlice > {
101
92
self . out
102
93
. iter ( )
103
94
. take_while ( |block| match block {
@@ -211,7 +202,33 @@ impl<T: AsBuffer> Kawa<T> {
211
202
}
212
203
}
213
204
214
- #[ derive( Debug , Clone , Copy ) ]
205
+ impl < T : AsBuffer + Clone > Clone for Kawa < T > {
206
+ fn clone ( & self ) -> Self {
207
+ Self {
208
+ storage : self . storage . clone ( ) ,
209
+ blocks : self . blocks . clone ( ) ,
210
+ out : self . out . clone ( ) ,
211
+ detached : self . detached . clone ( ) ,
212
+ kind : self . kind ,
213
+ expects : self . expects ,
214
+ parsing_phase : self . parsing_phase ,
215
+ body_size : self . body_size ,
216
+ consumed : self . consumed ,
217
+ }
218
+ }
219
+ }
220
+
221
+ /// Separate the content of the StatusLine and the crumbs from all the cookies from the stream of
222
+ /// Blocks. It allows better indexing, persistance and reordering of data. However it is a double
223
+ /// edge sword as it currently enables some unwanted/unsafe behavior such as Slice desync and over
224
+ /// consuming.
225
+ #[ derive( Debug , Clone ) ]
226
+ pub struct DetachedBlocks {
227
+ pub status_line : StatusLine ,
228
+ pub jar : VecDeque < Pair > ,
229
+ }
230
+
231
+ #[ derive( Debug , Clone , Copy , PartialEq , Eq ) ]
215
232
pub enum Kind {
216
233
Request ,
217
234
Response ,
@@ -295,7 +312,7 @@ pub enum BodySize {
295
312
Length ( usize ) ,
296
313
}
297
314
298
- #[ derive( Debug ) ]
315
+ #[ derive( Debug , Clone ) ]
299
316
pub enum Block {
300
317
StatusLine ,
301
318
Header ( Pair ) ,
@@ -370,7 +387,7 @@ impl StatusLine {
370
387
}
371
388
}
372
389
373
- #[ derive( Debug ) ]
390
+ #[ derive( Debug , Clone ) ]
374
391
pub struct Pair {
375
392
pub key : Store ,
376
393
pub val : Store ,
@@ -386,25 +403,25 @@ impl Pair {
386
403
}
387
404
}
388
405
389
- #[ derive( Debug ) ]
406
+ #[ derive( Debug , Clone ) ]
390
407
pub struct ChunkHeader {
391
408
pub length : Store ,
392
409
}
393
410
394
- #[ derive( Debug ) ]
411
+ #[ derive( Debug , Clone ) ]
395
412
pub struct Chunk {
396
413
pub data : Store ,
397
414
}
398
415
399
- #[ derive( Debug ) ]
416
+ #[ derive( Debug , Clone ) ]
400
417
pub struct Flags {
401
418
pub end_body : bool ,
402
419
pub end_chunk : bool ,
403
420
pub end_header : bool ,
404
421
pub end_stream : bool ,
405
422
}
406
423
407
- #[ derive( Debug ) ]
424
+ #[ derive( Debug , Clone ) ]
408
425
pub enum OutBlock {
409
426
Delimiter ,
410
427
Store ( Store ) ,
@@ -432,10 +449,9 @@ pub enum Store {
432
449
Slice ( Slice ) ,
433
450
Detached ( Slice ) ,
434
451
Static ( & ' static [ u8 ] ) ,
435
- #[ cfg( feature = "rc-alloc" ) ]
436
- Alloc ( Rc < [ u8 ] > , u32 ) ,
437
- #[ cfg( not( feature = "rc-alloc" ) ) ]
438
452
Alloc ( Box < [ u8 ] > , u32 ) ,
453
+ #[ cfg( feature = "rc-alloc" ) ]
454
+ Shared ( Rc < [ u8 ] > , u32 ) ,
439
455
}
440
456
441
457
impl Store {
@@ -447,14 +463,16 @@ impl Store {
447
463
Store :: Detached ( Slice :: new ( buffer, data) )
448
464
}
449
465
450
- pub fn new_vec ( data : & [ u8 ] ) -> Store {
451
- #[ allow( clippy:: useless_conversion) ]
452
- Store :: Alloc ( data. to_vec ( ) . into_boxed_slice ( ) . into ( ) , 0 )
466
+ pub fn from_vec ( data : Vec < u8 > ) -> Store {
467
+ Store :: Alloc ( data. into_boxed_slice ( ) , 0 )
468
+ }
469
+
470
+ pub fn from_slice ( data : & [ u8 ] ) -> Store {
471
+ Store :: Alloc ( data. to_vec ( ) . into_boxed_slice ( ) , 0 )
453
472
}
454
473
455
474
pub fn from_string ( data : String ) -> Store {
456
- #[ allow( clippy:: useless_conversion) ]
457
- Store :: Alloc ( data. into_bytes ( ) . into_boxed_slice ( ) . into ( ) , 0 )
475
+ Store :: Alloc ( data. into_bytes ( ) . into_boxed_slice ( ) , 0 )
458
476
}
459
477
460
478
pub fn push_left ( & mut self , amount : u32 ) {
@@ -479,6 +497,8 @@ impl Store {
479
497
Store :: Slice ( slice) | Store :: Detached ( slice) => slice. data ( buf) ,
480
498
Store :: Static ( data) => data,
481
499
Store :: Alloc ( data, index) => & data[ * index as usize ..] ,
500
+ #[ cfg( feature = "rc-alloc" ) ]
501
+ Store :: Shared ( data, index) => & data[ * index as usize ..] ,
482
502
}
483
503
}
484
504
pub fn data_opt < ' a > ( & ' a self , buf : & ' a [ u8 ] ) -> Option < & ' a [ u8 ] > {
@@ -487,25 +507,20 @@ impl Store {
487
507
Store :: Slice ( slice) | Store :: Detached ( slice) => slice. data_opt ( buf) ,
488
508
Store :: Static ( data) => Some ( data) ,
489
509
Store :: Alloc ( data, index) => Some ( & data[ * index as usize ..] ) ,
510
+ #[ cfg( feature = "rc-alloc" ) ]
511
+ Store :: Shared ( data, index) => Some ( & data[ * index as usize ..] ) ,
490
512
}
491
513
}
492
514
493
515
pub fn capture ( self , buf : & [ u8 ] ) -> Store {
494
516
match self {
495
- Store :: Slice ( slice) | Store :: Detached ( slice) => Store :: new_vec ( slice. data ( buf) ) ,
517
+ Store :: Slice ( slice) | Store :: Detached ( slice) => Store :: from_slice ( slice. data ( buf) ) ,
496
518
_ => self ,
497
519
}
498
520
}
499
521
500
522
pub fn modify ( & mut self , buf : & mut [ u8 ] , new_value : & [ u8 ] ) {
501
- match & self {
502
- Store :: Empty | Store :: Detached ( _) | Store :: Static ( _) | Store :: Alloc ( ..) => {
503
- println ! ( "WARNING: modification is not expected on: {self:?}" )
504
- }
505
- Store :: Slice ( _) => { }
506
- }
507
523
match self {
508
- Store :: Empty | Store :: Static ( _) | Store :: Alloc ( ..) => * self = Store :: new_vec ( new_value) ,
509
524
Store :: Slice ( slice) | Store :: Detached ( slice) => {
510
525
let new_len = new_value. len ( ) ;
511
526
if slice. len ( ) >= new_len {
@@ -514,9 +529,13 @@ impl Store {
514
529
buf[ start..end] . copy_from_slice ( new_value) ;
515
530
slice. len = new_len as u32 ;
516
531
} else {
517
- * self = Store :: new_vec ( new_value)
532
+ * self = Store :: from_slice ( new_value)
518
533
}
519
534
}
535
+ _ => {
536
+ println ! ( "WARNING: modification is not expected on: {self:?}" ) ;
537
+ * self = Store :: from_slice ( new_value)
538
+ }
520
539
}
521
540
}
522
541
@@ -545,6 +564,14 @@ impl Store {
545
564
( 0 , Some ( Store :: Alloc ( data, index + amount as u32 ) ) )
546
565
}
547
566
}
567
+ #[ cfg( feature = "rc-alloc" ) ]
568
+ Store :: Shared ( data, index) => {
569
+ if amount >= data. len ( ) - index as usize {
570
+ ( amount - data. len ( ) + index as usize , None )
571
+ } else {
572
+ ( 0 , Some ( Store :: Shared ( data, index + amount as u32 ) ) )
573
+ }
574
+ }
548
575
}
549
576
}
550
577
}
0 commit comments