Bytes::unsplit (#182)

Add `Bytes::unsplit`, analogous to `BytesMut::unsplit`.
This commit is contained in:
Alan Somers
2018-02-26 09:19:20 -08:00
committed by Carl Lerche
parent 4b68ef407f
commit ff7c0a1d90
2 changed files with 201 additions and 31 deletions
+58 -22
View File
@@ -804,6 +804,36 @@ impl Bytes {
mem::replace(self, result.freeze());
}
/// Combine splitted Bytes objects back as contiguous.
///
/// If `Bytes` objects were not contiguous originally, they will be extended.
///
/// # Examples
///
/// ```
/// use bytes::Bytes;
///
/// let mut buf = Bytes::with_capacity(64);
/// buf.extend_from_slice(b"aaabbbcccddd");
///
/// let splitted = buf.split_off(6);
/// assert_eq!(b"aaabbb", &buf[..]);
/// assert_eq!(b"cccddd", &splitted[..]);
///
/// buf.unsplit(splitted);
/// assert_eq!(b"aaabbbcccddd", &buf[..]);
/// ```
pub fn unsplit(&mut self, other: Bytes) {
if self.is_empty() {
*self = other;
return;
}
if let Err(other_inner) = self.inner.try_unsplit(other.inner) {
self.extend_from_slice(other_inner.as_ref());
}
}
}
impl IntoBuf for Bytes {
@@ -1087,7 +1117,7 @@ impl BytesMut {
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
self.inner.is_empty()
}
/// Return true if the `BytesMut` uses inline allocation
@@ -1437,32 +1467,13 @@ impl BytesMut {
/// assert_eq!(b"aaabbbcccddd", &buf[..]);
/// ```
pub fn unsplit(&mut self, other: BytesMut) {
let ptr;
if other.is_empty() {
return;
}
if self.is_empty() {
*self = other;
return;
}
unsafe {
ptr = self.inner.ptr.offset(self.inner.len as isize);
}
if ptr == other.inner.ptr &&
self.inner.kind() == KIND_ARC &&
other.inner.kind() == KIND_ARC
{
debug_assert_eq!(self.inner.arc.load(Acquire),
other.inner.arc.load(Acquire));
// Contiguous blocks, just combine directly
self.inner.len += other.inner.len;
self.inner.cap += other.inner.cap;
}
else {
self.extend_from_slice(&other);
if let Err(other_inner) = self.inner.try_unsplit(other.inner) {
self.extend_from_slice(other_inner.as_ref());
}
}
}
@@ -1922,6 +1933,31 @@ impl Inner {
}
}
fn try_unsplit(&mut self, other: Inner) -> Result<(), Inner> {
let ptr;
if other.is_empty() {
return Ok(());
}
unsafe {
ptr = self.ptr.offset(self.len as isize);
}
if ptr == other.ptr &&
self.kind() == KIND_ARC &&
other.kind() == KIND_ARC
{
debug_assert_eq!(self.arc.load(Acquire),
other.arc.load(Acquire));
// Contiguous blocks, just combine directly
self.len += other.len;
self.cap += other.cap;
Ok(())
} else {
Err(other)
}
}
unsafe fn set_start(&mut self, start: usize) {
// Setting the start to 0 is a no-op, so return early if this is the
// case.