Skip to content

Commit 813b98e

Browse files
authored
Merge pull request #12 from CleverCloud/convenience
Convenient changes
2 parents 96291e7 + 6bfafa1 commit 813b98e

File tree

6 files changed

+83
-37
lines changed

6 files changed

+83
-37
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ authors = [
1919
nom = "7.1.3"
2020

2121
[features]
22-
default = ["simd", "tolerant-parsing"]
22+
default = ["simd", "tolerant-parsing", "rc-alloc"]
2323
rc-alloc = []
2424
custom-vecdeque = []
2525
simd = []

src/protocol/h1/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn handle_recovery_error<T: AsBuffer>(
5353
}
5454

5555
fn process_headers<T: AsBuffer>(kawa: &mut Kawa<T>) {
56-
let buf = kawa.storage.mut_buffer();
56+
let buf = kawa.storage.buffer();
5757

5858
let (mut authority, path) = match &kawa.detached.status_line {
5959
StatusLine::Request {

src/storage/buffer.rs

+11
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,17 @@ pub struct Buffer<T: AsBuffer> {
5151
pub buffer: T,
5252
}
5353

54+
impl<T: AsBuffer + Clone> Clone for Buffer<T> {
55+
fn clone(&self) -> Self {
56+
Self {
57+
start: self.start,
58+
head: self.head,
59+
end: self.end,
60+
buffer: self.buffer.clone(),
61+
}
62+
}
63+
}
64+
5465
impl<T: AsBuffer> Buffer<T> {
5566
pub fn new(buffer: T) -> Self {
5667
Self {

src/storage/debug.rs

+8
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,14 @@ impl Store {
216216
to_utf8(Some(&data[*index as usize..]))
217217
))?;
218218
}
219+
#[cfg(feature = "rc-alloc")]
220+
Store::Shared(data, index) => {
221+
result.write_fmt(format_args!(
222+
"Store::Shared({:?}, {:?})",
223+
to_utf8(Some(&data[..*index as usize])),
224+
to_utf8(Some(&data[*index as usize..]))
225+
))?;
226+
}
219227
}
220228
Ok(())
221229
}

src/storage/repr.rs

+61-34
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@ pub struct Kawa<T: AsBuffer> {
3535
pub consumed: bool,
3636
}
3737

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-
4738
impl<T: AsBuffer> Kawa<T> {
4839
/// Create a new Kawa struct around a given storage.
4940
///
@@ -97,7 +88,7 @@ impl<T: AsBuffer> Kawa<T> {
9788
///
9889
/// note: until you drop the resulting vector, Rust will prevent mutably borrowing Kawa as the
9990
/// 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> {
10192
self.out
10293
.iter()
10394
.take_while(|block| match block {
@@ -211,7 +202,33 @@ impl<T: AsBuffer> Kawa<T> {
211202
}
212203
}
213204

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)]
215232
pub enum Kind {
216233
Request,
217234
Response,
@@ -295,7 +312,7 @@ pub enum BodySize {
295312
Length(usize),
296313
}
297314

298-
#[derive(Debug)]
315+
#[derive(Debug, Clone)]
299316
pub enum Block {
300317
StatusLine,
301318
Header(Pair),
@@ -370,7 +387,7 @@ impl StatusLine {
370387
}
371388
}
372389

373-
#[derive(Debug)]
390+
#[derive(Debug, Clone)]
374391
pub struct Pair {
375392
pub key: Store,
376393
pub val: Store,
@@ -386,25 +403,25 @@ impl Pair {
386403
}
387404
}
388405

389-
#[derive(Debug)]
406+
#[derive(Debug, Clone)]
390407
pub struct ChunkHeader {
391408
pub length: Store,
392409
}
393410

394-
#[derive(Debug)]
411+
#[derive(Debug, Clone)]
395412
pub struct Chunk {
396413
pub data: Store,
397414
}
398415

399-
#[derive(Debug)]
416+
#[derive(Debug, Clone)]
400417
pub struct Flags {
401418
pub end_body: bool,
402419
pub end_chunk: bool,
403420
pub end_header: bool,
404421
pub end_stream: bool,
405422
}
406423

407-
#[derive(Debug)]
424+
#[derive(Debug, Clone)]
408425
pub enum OutBlock {
409426
Delimiter,
410427
Store(Store),
@@ -432,10 +449,9 @@ pub enum Store {
432449
Slice(Slice),
433450
Detached(Slice),
434451
Static(&'static [u8]),
435-
#[cfg(feature = "rc-alloc")]
436-
Alloc(Rc<[u8]>, u32),
437-
#[cfg(not(feature = "rc-alloc"))]
438452
Alloc(Box<[u8]>, u32),
453+
#[cfg(feature = "rc-alloc")]
454+
Shared(Rc<[u8]>, u32),
439455
}
440456

441457
impl Store {
@@ -447,14 +463,16 @@ impl Store {
447463
Store::Detached(Slice::new(buffer, data))
448464
}
449465

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)
453472
}
454473

455474
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)
458476
}
459477

460478
pub fn push_left(&mut self, amount: u32) {
@@ -479,6 +497,8 @@ impl Store {
479497
Store::Slice(slice) | Store::Detached(slice) => slice.data(buf),
480498
Store::Static(data) => data,
481499
Store::Alloc(data, index) => &data[*index as usize..],
500+
#[cfg(feature = "rc-alloc")]
501+
Store::Shared(data, index) => &data[*index as usize..],
482502
}
483503
}
484504
pub fn data_opt<'a>(&'a self, buf: &'a [u8]) -> Option<&'a [u8]> {
@@ -487,25 +507,20 @@ impl Store {
487507
Store::Slice(slice) | Store::Detached(slice) => slice.data_opt(buf),
488508
Store::Static(data) => Some(data),
489509
Store::Alloc(data, index) => Some(&data[*index as usize..]),
510+
#[cfg(feature = "rc-alloc")]
511+
Store::Shared(data, index) => Some(&data[*index as usize..]),
490512
}
491513
}
492514

493515
pub fn capture(self, buf: &[u8]) -> Store {
494516
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)),
496518
_ => self,
497519
}
498520
}
499521

500522
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-
}
507523
match self {
508-
Store::Empty | Store::Static(_) | Store::Alloc(..) => *self = Store::new_vec(new_value),
509524
Store::Slice(slice) | Store::Detached(slice) => {
510525
let new_len = new_value.len();
511526
if slice.len() >= new_len {
@@ -514,9 +529,13 @@ impl Store {
514529
buf[start..end].copy_from_slice(new_value);
515530
slice.len = new_len as u32;
516531
} else {
517-
*self = Store::new_vec(new_value)
532+
*self = Store::from_slice(new_value)
518533
}
519534
}
535+
_ => {
536+
println!("WARNING: modification is not expected on: {self:?}");
537+
*self = Store::from_slice(new_value)
538+
}
520539
}
521540
}
522541

@@ -545,6 +564,14 @@ impl Store {
545564
(0, Some(Store::Alloc(data, index + amount as u32)))
546565
}
547566
}
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+
}
548575
}
549576
}
550577
}

src/storage/vecdeque.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::{
66
slice::from_raw_parts_mut,
77
};
88

9-
#[derive(Debug)]
9+
#[derive(Debug, Clone)]
1010
pub struct VecDeque<T: Sized> {
1111
tail: usize,
1212
head: usize,

0 commit comments

Comments
 (0)