aboutsummaryrefslogtreecommitdiff
path: root/src/bytes_buf.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-09-13 11:31:19 +0200
committerAlex Auvolat <alex@adnab.me>2022-09-13 11:31:19 +0200
commit2305c2cf03919f074ec92d98cb6593c4ead50c4b (patch)
treeb195029089b9fd407609d47acf2ba4fb42f39901 /src/bytes_buf.rs
parentf022a77f97c169807ae098e101a29301c0d19fbd (diff)
downloadnetapp-2305c2cf03919f074ec92d98cb6593c4ead50c4b.tar.gz
netapp-2305c2cf03919f074ec92d98cb6593c4ead50c4b.zip
Use BytesMut instead of Vec<u8> in bytes_buf (extend is probably faster)
Diffstat (limited to 'src/bytes_buf.rs')
-rw-r--r--src/bytes_buf.rs18
1 files changed, 10 insertions, 8 deletions
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()
}
}