mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-25 00:00:22 +02:00
Move Inner constructions to struct fns
This commit is contained in:
+64
-44
@@ -365,12 +365,7 @@ impl Bytes {
|
|||||||
pub fn new() -> Bytes {
|
pub fn new() -> Bytes {
|
||||||
Bytes {
|
Bytes {
|
||||||
inner: Inner2 {
|
inner: Inner2 {
|
||||||
inner: Inner {
|
inner: Inner::empty(),
|
||||||
arc: AtomicPtr::new(ptr::null_mut()),
|
|
||||||
ptr: ptr::null_mut(),
|
|
||||||
len: 0,
|
|
||||||
cap: 0,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -390,19 +385,9 @@ impl Bytes {
|
|||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_static(bytes: &'static [u8]) -> Bytes {
|
pub fn from_static(bytes: &'static [u8]) -> Bytes {
|
||||||
let ptr = bytes.as_ptr() as *mut u8;
|
|
||||||
|
|
||||||
Bytes {
|
Bytes {
|
||||||
inner: Inner2 {
|
inner: Inner2 {
|
||||||
inner: Inner {
|
inner: Inner::from_static(bytes),
|
||||||
// `arc` won't ever store a pointer. Instead, use it to
|
|
||||||
// track the fact that the `Bytes` handle is backed by a
|
|
||||||
// static buffer.
|
|
||||||
arc: AtomicPtr::new(KIND_STATIC as *mut Shared),
|
|
||||||
ptr: ptr,
|
|
||||||
len: bytes.len(),
|
|
||||||
cap: bytes.len(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -784,20 +769,10 @@ impl BytesMut {
|
|||||||
/// ```
|
/// ```
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn with_capacity(capacity: usize) -> BytesMut {
|
pub fn with_capacity(capacity: usize) -> BytesMut {
|
||||||
if capacity <= INLINE_CAP {
|
BytesMut {
|
||||||
unsafe {
|
inner: Inner2 {
|
||||||
// Using uninitialized memory is ~30% faster
|
inner: Inner::with_capacity(capacity),
|
||||||
BytesMut {
|
},
|
||||||
inner: Inner2 {
|
|
||||||
inner: Inner {
|
|
||||||
arc: AtomicPtr::new(KIND_INLINE as *mut Shared),
|
|
||||||
.. mem::uninitialized()
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
BytesMut::from(Vec::with_capacity(capacity))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1186,21 +1161,10 @@ impl ops::DerefMut for BytesMut {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl From<Vec<u8>> for BytesMut {
|
impl From<Vec<u8>> for BytesMut {
|
||||||
fn from(mut src: Vec<u8>) -> BytesMut {
|
fn from(src: Vec<u8>) -> BytesMut {
|
||||||
let len = src.len();
|
|
||||||
let cap = src.capacity();
|
|
||||||
let ptr = src.as_mut_ptr();
|
|
||||||
|
|
||||||
mem::forget(src);
|
|
||||||
|
|
||||||
BytesMut {
|
BytesMut {
|
||||||
inner: Inner2 {
|
inner: Inner2 {
|
||||||
inner: Inner {
|
inner: Inner::from_vec(src),
|
||||||
arc: AtomicPtr::new(ptr::null_mut()),
|
|
||||||
ptr: ptr,
|
|
||||||
len: len,
|
|
||||||
cap: cap,
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1335,6 +1299,62 @@ impl<'a> IntoIterator for &'a BytesMut {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
impl Inner {
|
impl Inner {
|
||||||
|
#[inline]
|
||||||
|
fn empty() -> Inner {
|
||||||
|
Inner {
|
||||||
|
arc: AtomicPtr::new(ptr::null_mut()),
|
||||||
|
ptr: ptr::null_mut(),
|
||||||
|
len: 0,
|
||||||
|
cap: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn from_static(bytes: &'static [u8]) -> Inner {
|
||||||
|
let ptr = bytes.as_ptr() as *mut u8;
|
||||||
|
|
||||||
|
Inner {
|
||||||
|
// `arc` won't ever store a pointer. Instead, use it to
|
||||||
|
// track the fact that the `Bytes` handle is backed by a
|
||||||
|
// static buffer.
|
||||||
|
arc: AtomicPtr::new(KIND_STATIC as *mut Shared),
|
||||||
|
ptr: ptr,
|
||||||
|
len: bytes.len(),
|
||||||
|
cap: bytes.len(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn from_vec(mut src: Vec<u8>) -> Inner {
|
||||||
|
let len = src.len();
|
||||||
|
let cap = src.capacity();
|
||||||
|
let ptr = src.as_mut_ptr();
|
||||||
|
|
||||||
|
mem::forget(src);
|
||||||
|
|
||||||
|
Inner {
|
||||||
|
arc: AtomicPtr::new(ptr::null_mut()),
|
||||||
|
ptr: ptr,
|
||||||
|
len: len,
|
||||||
|
cap: cap,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn with_capacity(capacity: usize) -> Inner {
|
||||||
|
if capacity <= INLINE_CAP {
|
||||||
|
unsafe {
|
||||||
|
// Using uninitialized memory is ~30% faster
|
||||||
|
Inner {
|
||||||
|
arc: AtomicPtr::new(KIND_INLINE as *mut Shared),
|
||||||
|
.. mem::uninitialized()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Inner::from_vec(Vec::with_capacity(capacity))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Return a slice for the handle's view into the shared buffer
|
/// Return a slice for the handle's view into the shared buffer
|
||||||
#[inline]
|
#[inline]
|
||||||
fn as_ref(&self) -> &[u8] {
|
fn as_ref(&self) -> &[u8] {
|
||||||
|
|||||||
Reference in New Issue
Block a user