mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-25 00:00:22 +02:00
Unsplit improvements (#173)
* Handle empty self and other for unsplit. * Change extend() to extend_from_slice().
This commit is contained in:
+14
-1
@@ -295,6 +295,8 @@ pub struct BytesMut {
|
|||||||
#[cfg(target_endian = "little")]
|
#[cfg(target_endian = "little")]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
struct Inner {
|
struct Inner {
|
||||||
|
// WARNING: Do not access the fields directly unless you know what you are
|
||||||
|
// doing. Instead, use the fns. See implementation comment above.
|
||||||
arc: AtomicPtr<Shared>,
|
arc: AtomicPtr<Shared>,
|
||||||
ptr: *mut u8,
|
ptr: *mut u8,
|
||||||
len: usize,
|
len: usize,
|
||||||
@@ -304,6 +306,8 @@ struct Inner {
|
|||||||
#[cfg(target_endian = "big")]
|
#[cfg(target_endian = "big")]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
struct Inner {
|
struct Inner {
|
||||||
|
// WARNING: Do not access the fields directly unless you know what you are
|
||||||
|
// doing. Instead, use the fns. See implementation comment above.
|
||||||
ptr: *mut u8,
|
ptr: *mut u8,
|
||||||
len: usize,
|
len: usize,
|
||||||
cap: usize,
|
cap: usize,
|
||||||
@@ -1401,6 +1405,15 @@ impl BytesMut {
|
|||||||
pub fn unsplit(&mut self, other: BytesMut) {
|
pub fn unsplit(&mut self, other: BytesMut) {
|
||||||
let ptr;
|
let ptr;
|
||||||
|
|
||||||
|
if other.is_empty() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.is_empty() {
|
||||||
|
*self = other;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
ptr = self.inner.ptr.offset(self.inner.len as isize);
|
ptr = self.inner.ptr.offset(self.inner.len as isize);
|
||||||
}
|
}
|
||||||
@@ -1415,7 +1428,7 @@ impl BytesMut {
|
|||||||
self.inner.cap += other.inner.cap;
|
self.inner.cap += other.inner.cap;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
self.extend(other);
|
self.extend_from_slice(&other);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -566,6 +566,30 @@ fn unsplit_basic() {
|
|||||||
assert_eq!(b"aaabbbcccddd", &buf[..]);
|
assert_eq!(b"aaabbbcccddd", &buf[..]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unsplit_empty_other() {
|
||||||
|
let mut buf = BytesMut::with_capacity(64);
|
||||||
|
buf.extend_from_slice(b"aaabbbcccddd");
|
||||||
|
|
||||||
|
// empty other
|
||||||
|
let other = BytesMut::new();
|
||||||
|
|
||||||
|
buf.unsplit(other);
|
||||||
|
assert_eq!(b"aaabbbcccddd", &buf[..]);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn unsplit_empty_self() {
|
||||||
|
// empty self
|
||||||
|
let mut buf = BytesMut::new();
|
||||||
|
|
||||||
|
let mut other = BytesMut::with_capacity(64);
|
||||||
|
other.extend_from_slice(b"aaabbbcccddd");
|
||||||
|
|
||||||
|
buf.unsplit(other);
|
||||||
|
assert_eq!(b"aaabbbcccddd", &buf[..]);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn unsplit_inline_arc() {
|
fn unsplit_inline_arc() {
|
||||||
let mut buf = BytesMut::with_capacity(8); //inline
|
let mut buf = BytesMut::with_capacity(8); //inline
|
||||||
|
|||||||
Reference in New Issue
Block a user