From 263db66fcee65deda39de18baa837228ea38baf1 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 1 Sep 2022 10:29:26 +0200 Subject: Refactor: create a BytesBuf utility crate (will also be usefull in Garage) --- src/bytes_buf.rs | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 src/bytes_buf.rs (limited to 'src/bytes_buf.rs') diff --git a/src/bytes_buf.rs b/src/bytes_buf.rs new file mode 100644 index 0000000..46c7039 --- /dev/null +++ b/src/bytes_buf.rs @@ -0,0 +1,167 @@ +use std::collections::VecDeque; + +pub use bytes::Bytes; + +/// A circular buffer of bytes, internally represented as a list of Bytes +/// for optimization, but that for all intent and purposes acts just like +/// a big byte slice which can be extended on the right and from which +/// one can take on the left. +pub struct BytesBuf { + buf: VecDeque, + buf_len: usize, +} + +impl BytesBuf { + /// Creates a new empty BytesBuf + pub fn new() -> Self { + Self { + buf: VecDeque::new(), + buf_len: 0, + } + } + + /// Returns the number of bytes stored in the BytesBuf + #[inline] + pub fn len(&self) -> usize { + self.buf_len + } + + /// Returns true iff the BytesBuf contains zero bytes + #[inline] + pub fn is_empty(&self) -> bool { + self.buf_len == 0 + } + + /// Adds some bytes to the right of the buffer + pub fn extend(&mut self, b: Bytes) { + if !b.is_empty() { + self.buf_len += b.len(); + self.buf.push_back(b); + } + } + + /// Takes the whole content of the buffer and returns it as a single Bytes unit + pub fn take_all(&mut self) -> Bytes { + if self.buf.len() == 0 { + Bytes::new() + } else if self.buf.len() == 1 { + self.buf_len = 0; + self.buf.pop_back().unwrap() + } else { + let mut ret = Vec::with_capacity(self.buf_len); + for b in self.buf.iter() { + ret.extend(&b[..]); + } + self.buf.clear(); + self.buf_len = 0; + Bytes::from(ret) + } + } + + /// Takes at most max_len bytes from the left of the buffer + pub fn take_max(&mut self, max_len: usize) -> Bytes { + if self.buf_len <= max_len { + self.take_all() + } else { + self.take_exact_ok(max_len) + } + } + + /// Take exactly len bytes from the left of the buffer, returns None if + /// the BytesBuf doesn't contain enough data + pub fn take_exact(&mut self, len: usize) -> Option { + if self.buf_len < len { + None + } else { + Some(self.take_exact_ok(len)) + } + } + + fn take_exact_ok(&mut self, len: usize) -> Bytes { + assert!(len <= self.buf_len); + let front = self.buf.pop_front().unwrap(); + if front.len() > len { + self.buf.push_front(front.slice(len..)); + self.buf_len -= len; + front.slice(..len) + } else if front.len() == len { + self.buf_len -= len; + front + } else { + let mut ret = Vec::with_capacity(len); + ret.extend(&front[..]); + self.buf_len -= front.len(); + while ret.len() < len { + let front = self.buf.pop_front().unwrap(); + if front.len() > len - ret.len() { + let take = len - ret.len(); + ret.extend(front.slice(..take)); + self.buf.push_front(front.slice(take..)); + self.buf_len -= take; + break; + } else { + ret.extend(&front[..]); + self.buf_len -= front.len(); + } + } + Bytes::from(ret) + } + } + + /// Return the internal sequence of Bytes slices that make up the buffer + pub fn into_slices(self) -> VecDeque { + self.buf + } +} + +impl From for BytesBuf { + fn from(b: Bytes) -> BytesBuf { + let mut ret = BytesBuf::new(); + ret.extend(b); + ret + } +} + +impl From for Bytes { + fn from(mut b: BytesBuf) -> Bytes { + b.take_all() + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_bytes_buf() { + let mut buf = BytesBuf::new(); + assert!(buf.len() == 0); + assert!(buf.is_empty()); + + buf.extend(Bytes::from(b"Hello, world!".to_vec())); + assert!(buf.len() == 13); + assert!(!buf.is_empty()); + + buf.extend(Bytes::from(b"1234567890".to_vec())); + assert!(buf.len() == 23); + assert!(!buf.is_empty()); + + assert_eq!(buf.take_all(), Bytes::from(b"Hello, world!1234567890".to_vec())); + assert!(buf.len() == 0); + assert!(buf.is_empty()); + + buf.extend(Bytes::from(b"1234567890".to_vec())); + buf.extend(Bytes::from(b"Hello, world!".to_vec())); + assert!(buf.len() == 23); + assert!(!buf.is_empty()); + + assert_eq!(buf.take_max(12), Bytes::from(b"1234567890He".to_vec())); + assert!(buf.len() == 11); + + assert_eq!(buf.take_exact(12), None); + assert!(buf.len() == 11); + assert_eq!(buf.take_exact(11), Some(Bytes::from(b"llo, world!".to_vec()))); + assert!(buf.len() == 0); + assert!(buf.is_empty()); + } +} -- cgit v1.2.3 From cd203f5708907c2bf172a3c5b7c5b40e2557b2f4 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Thu, 1 Sep 2022 12:15:50 +0200 Subject: Add OrderTag to Req and Resp, refactor errors --- src/bytes_buf.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'src/bytes_buf.rs') diff --git a/src/bytes_buf.rs b/src/bytes_buf.rs index 46c7039..857be9d 100644 --- a/src/bytes_buf.rs +++ b/src/bytes_buf.rs @@ -146,7 +146,10 @@ mod test { assert!(buf.len() == 23); assert!(!buf.is_empty()); - assert_eq!(buf.take_all(), Bytes::from(b"Hello, world!1234567890".to_vec())); + assert_eq!( + buf.take_all(), + Bytes::from(b"Hello, world!1234567890".to_vec()) + ); assert!(buf.len() == 0); assert!(buf.is_empty()); @@ -160,7 +163,10 @@ mod test { assert_eq!(buf.take_exact(12), None); assert!(buf.len() == 11); - assert_eq!(buf.take_exact(11), Some(Bytes::from(b"llo, world!".to_vec()))); + assert_eq!( + buf.take_exact(11), + Some(Bytes::from(b"llo, world!".to_vec())) + ); assert!(buf.len() == 0); assert!(buf.is_empty()); } -- cgit v1.2.3 From 8a7aca98375ff20effaab3d7c95124bd4cbc925c Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Mon, 12 Sep 2022 17:20:45 +0200 Subject: reword doc comment --- src/bytes_buf.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/bytes_buf.rs') diff --git a/src/bytes_buf.rs b/src/bytes_buf.rs index 857be9d..05b7edd 100644 --- a/src/bytes_buf.rs +++ b/src/bytes_buf.rs @@ -5,7 +5,7 @@ pub use bytes::Bytes; /// A circular buffer of bytes, internally represented as a list of Bytes /// for optimization, but that for all intent and purposes acts just like /// a big byte slice which can be extended on the right and from which -/// one can take on the left. +/// stuff can be taken on the left. pub struct BytesBuf { buf: VecDeque, buf_len: usize, -- cgit v1.2.3 From 2305c2cf03919f074ec92d98cb6593c4ead50c4b Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 13 Sep 2022 11:31:19 +0200 Subject: Use BytesMut instead of Vec in bytes_buf (extend is probably faster) --- src/bytes_buf.rs | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'src/bytes_buf.rs') diff --git a/src/bytes_buf.rs b/src/bytes_buf.rs index 05b7edd..931be82 100644 --- a/src/bytes_buf.rs +++ b/src/bytes_buf.rs @@ -1,5 +1,7 @@ use std::collections::VecDeque; +use bytes::BytesMut; + pub use bytes::Bytes; /// A circular buffer of bytes, internally represented as a list of Bytes @@ -48,13 +50,13 @@ impl BytesBuf { self.buf_len = 0; self.buf.pop_back().unwrap() } else { - let mut ret = Vec::with_capacity(self.buf_len); + let mut ret = BytesMut::with_capacity(self.buf_len); for b in self.buf.iter() { - ret.extend(&b[..]); + ret.extend_from_slice(&b[..]); } self.buf.clear(); self.buf_len = 0; - Bytes::from(ret) + ret.freeze() } } @@ -88,23 +90,23 @@ impl BytesBuf { self.buf_len -= len; front } else { - let mut ret = Vec::with_capacity(len); - ret.extend(&front[..]); + let mut ret = BytesMut::with_capacity(len); + ret.extend_from_slice(&front[..]); self.buf_len -= front.len(); while ret.len() < len { let front = self.buf.pop_front().unwrap(); if front.len() > len - ret.len() { let take = len - ret.len(); - ret.extend(front.slice(..take)); + ret.extend_from_slice(&front[..take]); self.buf.push_front(front.slice(take..)); self.buf_len -= take; break; } else { - ret.extend(&front[..]); + ret.extend_from_slice(&front[..]); self.buf_len -= front.len(); } } - Bytes::from(ret) + ret.freeze() } } -- cgit v1.2.3