mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-26 00:00:20 +02:00
Cleanup Bytes
This commit is contained in:
+108
-48
@@ -10,9 +10,8 @@ use std::sync::Arc;
|
|||||||
/// be immutable, `Bytes` is `Sync`, `Clone` is shallow (ref count increment),
|
/// be immutable, `Bytes` is `Sync`, `Clone` is shallow (ref count increment),
|
||||||
/// and all operations only update views into the underlying data without
|
/// and all operations only update views into the underlying data without
|
||||||
/// requiring any copies.
|
/// requiring any copies.
|
||||||
#[derive(Eq)]
|
|
||||||
pub struct Bytes {
|
pub struct Bytes {
|
||||||
inner: BytesMut,
|
inner: Inner,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A unique reference to a slice of bytes.
|
/// A unique reference to a slice of bytes.
|
||||||
@@ -20,6 +19,10 @@ pub struct Bytes {
|
|||||||
/// A `BytesMut` is a unique handle to a slice of bytes allowing mutation of
|
/// A `BytesMut` is a unique handle to a slice of bytes allowing mutation of
|
||||||
/// the underlying bytes.
|
/// the underlying bytes.
|
||||||
pub struct BytesMut {
|
pub struct BytesMut {
|
||||||
|
inner: Inner
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Inner {
|
||||||
// Pointer to the start of the memory owned by this BytesMut
|
// Pointer to the start of the memory owned by this BytesMut
|
||||||
ptr: *mut u8,
|
ptr: *mut u8,
|
||||||
|
|
||||||
@@ -45,7 +48,7 @@ impl Bytes {
|
|||||||
pub fn new() -> Bytes {
|
pub fn new() -> Bytes {
|
||||||
use std::ptr;
|
use std::ptr;
|
||||||
Bytes {
|
Bytes {
|
||||||
inner: BytesMut {
|
inner: Inner {
|
||||||
ptr: ptr::null_mut(),
|
ptr: ptr::null_mut(),
|
||||||
len: 0,
|
len: 0,
|
||||||
cap: 0,
|
cap: 0,
|
||||||
@@ -109,7 +112,7 @@ impl Bytes {
|
|||||||
///
|
///
|
||||||
/// Panics if `at > len`
|
/// Panics if `at > len`
|
||||||
pub fn split_off(&mut self, at: usize) -> Bytes {
|
pub fn split_off(&mut self, at: usize) -> Bytes {
|
||||||
self.inner.split_off(at).freeze()
|
Bytes { inner: self.inner.split_off(at) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Splits the buffer into two at the given index.
|
/// Splits the buffer into two at the given index.
|
||||||
@@ -124,7 +127,7 @@ impl Bytes {
|
|||||||
///
|
///
|
||||||
/// Panics if `at > len`
|
/// Panics if `at > len`
|
||||||
pub fn drain_to(&mut self, at: usize) -> Bytes {
|
pub fn drain_to(&mut self, at: usize) -> Bytes {
|
||||||
self.inner.drain_to(at).freeze()
|
Bytes { inner: self.inner.drain_to(at) }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Attempt to convert into a `BytesMut` handle.
|
/// Attempt to convert into a `BytesMut` handle.
|
||||||
@@ -133,7 +136,7 @@ impl Bytes {
|
|||||||
/// the underlying chunk of memory.
|
/// the underlying chunk of memory.
|
||||||
pub fn try_mut(mut self) -> Result<BytesMut, Bytes> {
|
pub fn try_mut(mut self) -> Result<BytesMut, Bytes> {
|
||||||
if self.inner.is_mut_safe() {
|
if self.inner.is_mut_safe() {
|
||||||
Ok(self.inner)
|
Ok(BytesMut { inner: self.inner })
|
||||||
} else {
|
} else {
|
||||||
Err(self)
|
Err(self)
|
||||||
}
|
}
|
||||||
@@ -181,7 +184,7 @@ impl ops::Deref for Bytes {
|
|||||||
type Target = [u8];
|
type Target = [u8];
|
||||||
|
|
||||||
fn deref(&self) -> &[u8] {
|
fn deref(&self) -> &[u8] {
|
||||||
self.as_ref()
|
self.inner.as_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,13 +208,16 @@ impl<'a> From<&'a [u8]> for Bytes {
|
|||||||
|
|
||||||
impl PartialEq for Bytes {
|
impl PartialEq for Bytes {
|
||||||
fn eq(&self, other: &Bytes) -> bool {
|
fn eq(&self, other: &Bytes) -> bool {
|
||||||
self.inner == other.inner
|
self.inner.as_ref() == other.inner.as_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Eq for Bytes {
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Bytes {
|
impl fmt::Debug for Bytes {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt::Debug::fmt(&self.inner, fmt)
|
fmt::Debug::fmt(&self.inner.as_ref(), fmt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -238,27 +244,27 @@ impl BytesMut {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of bytes contained in this `BytesMut`.
|
/// Returns the number of bytes contained in this `BytesMut`.
|
||||||
/// #[inline]
|
#[inline]
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.len
|
self.inner.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the value contains no bytes
|
/// Returns true if the value contains no bytes
|
||||||
/// #[inline]
|
#[inline]
|
||||||
pub fn is_empty(&self) -> bool {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.len() == 0
|
self.len() == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the total byte capacity of this `BytesMut`
|
/// Returns the total byte capacity of this `BytesMut`
|
||||||
/// #[inline]
|
#[inline]
|
||||||
pub fn capacity(&self) -> usize {
|
pub fn capacity(&self) -> usize {
|
||||||
self.cap
|
self.inner.capacity()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return an immutable handle to the bytes
|
/// Return an immutable handle to the bytes
|
||||||
/// #[inline]
|
#[inline]
|
||||||
pub fn freeze(self) -> Bytes {
|
pub fn freeze(self) -> Bytes {
|
||||||
Bytes { inner: self }
|
Bytes { inner: self.inner }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Splits the bytes into two at the given index.
|
/// Splits the bytes into two at the given index.
|
||||||
@@ -273,14 +279,7 @@ impl BytesMut {
|
|||||||
///
|
///
|
||||||
/// Panics if `at > capacity`
|
/// Panics if `at > capacity`
|
||||||
pub fn split_off(&mut self, at: usize) -> BytesMut {
|
pub fn split_off(&mut self, at: usize) -> BytesMut {
|
||||||
let mut other = self.shallow_clone();
|
BytesMut { inner: self.inner.split_off(at) }
|
||||||
|
|
||||||
unsafe {
|
|
||||||
other.set_start(at);
|
|
||||||
self.set_end(at);
|
|
||||||
}
|
|
||||||
|
|
||||||
return other
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Splits the buffer into two at the given index.
|
/// Splits the buffer into two at the given index.
|
||||||
@@ -295,14 +294,7 @@ impl BytesMut {
|
|||||||
///
|
///
|
||||||
/// Panics if `at > len`
|
/// Panics if `at > len`
|
||||||
pub fn drain_to(&mut self, at: usize) -> BytesMut {
|
pub fn drain_to(&mut self, at: usize) -> BytesMut {
|
||||||
let mut other = self.shallow_clone();
|
BytesMut { inner: self.inner.drain_to(at) }
|
||||||
|
|
||||||
unsafe {
|
|
||||||
other.set_end(at);
|
|
||||||
self.set_start(at);
|
|
||||||
}
|
|
||||||
|
|
||||||
return other
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the inner contents of this `BytesMut` as a slice.
|
/// Returns the inner contents of this `BytesMut` as a slice.
|
||||||
@@ -314,7 +306,7 @@ impl BytesMut {
|
|||||||
///
|
///
|
||||||
/// This a slice of bytes that have been initialized
|
/// This a slice of bytes that have been initialized
|
||||||
pub fn as_mut(&mut self) -> &mut [u8] {
|
pub fn as_mut(&mut self) -> &mut [u8] {
|
||||||
unsafe { slice::from_raw_parts_mut(self.ptr, self.len) }
|
self.inner.as_mut()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the length of the buffer
|
/// Sets the length of the buffer
|
||||||
@@ -328,17 +320,83 @@ impl BytesMut {
|
|||||||
/// This method will panic if `len` is out of bounds for the underlying
|
/// This method will panic if `len` is out of bounds for the underlying
|
||||||
/// slice or if it comes after the `end` of the configured window.
|
/// slice or if it comes after the `end` of the configured window.
|
||||||
pub unsafe fn set_len(&mut self, len: usize) {
|
pub unsafe fn set_len(&mut self, len: usize) {
|
||||||
assert!(len <= self.cap);
|
self.inner.set_len(len);
|
||||||
self.len = len;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the inner contents of this `BytesMut` as a mutable slice
|
/// Returns the inner contents of this `BytesMut` as a mutable slice
|
||||||
///
|
///
|
||||||
/// This a slice of all bytes, including uninitialized memory
|
/// This a slice of all bytes, including uninitialized memory
|
||||||
|
#[inline]
|
||||||
pub unsafe fn as_raw(&mut self) -> &mut [u8] {
|
pub unsafe fn as_raw(&mut self) -> &mut [u8] {
|
||||||
|
self.inner.as_raw()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ===== Inner =====
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
impl Inner {
|
||||||
|
#[inline]
|
||||||
|
fn as_ref(&self) -> &[u8] {
|
||||||
|
unsafe { slice::from_raw_parts(self.ptr, self.len) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn as_mut(&mut self) -> &mut [u8] {
|
||||||
|
unsafe { slice::from_raw_parts_mut(self.ptr, self.len) }
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
unsafe fn as_raw(&mut self) -> &mut [u8] {
|
||||||
slice::from_raw_parts_mut(self.ptr, self.cap)
|
slice::from_raw_parts_mut(self.ptr, self.cap)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn len(&self) -> usize {
|
||||||
|
self.len
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
unsafe fn set_len(&mut self, len: usize) {
|
||||||
|
assert!(len <= self.cap);
|
||||||
|
self.len = len;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn is_empty(&self) -> bool {
|
||||||
|
self.len() == 0
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn capacity(&self) -> usize {
|
||||||
|
self.cap
|
||||||
|
}
|
||||||
|
|
||||||
|
fn split_off(&mut self, at: usize) -> Inner {
|
||||||
|
let mut other = self.shallow_clone();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
other.set_start(at);
|
||||||
|
self.set_end(at);
|
||||||
|
}
|
||||||
|
|
||||||
|
return other
|
||||||
|
}
|
||||||
|
|
||||||
|
fn drain_to(&mut self, at: usize) -> Inner {
|
||||||
|
let mut other = self.shallow_clone();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
other.set_end(at);
|
||||||
|
self.set_start(at);
|
||||||
|
}
|
||||||
|
|
||||||
|
return other
|
||||||
|
}
|
||||||
|
|
||||||
/// Changes the starting index of this window to the index specified.
|
/// Changes the starting index of this window to the index specified.
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
@@ -390,7 +448,7 @@ impl BytesMut {
|
|||||||
/// Increments the ref count. This should only be done if it is known that
|
/// Increments the ref count. This should only be done if it is known that
|
||||||
/// it can be done safely. As such, this fn is not public, instead other
|
/// it can be done safely. As such, this fn is not public, instead other
|
||||||
/// fns will use this one while maintaining the guarantees.
|
/// fns will use this one while maintaining the guarantees.
|
||||||
fn shallow_clone(&self) -> BytesMut {
|
fn shallow_clone(&self) -> Inner {
|
||||||
let arc = unsafe {
|
let arc = unsafe {
|
||||||
match *self.arc.get() {
|
match *self.arc.get() {
|
||||||
Some(ref arc) => {
|
Some(ref arc) => {
|
||||||
@@ -409,7 +467,7 @@ impl BytesMut {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
BytesMut {
|
Inner {
|
||||||
arc: UnsafeCell::new(Some(arc)),
|
arc: UnsafeCell::new(Some(arc)),
|
||||||
.. *self
|
.. *self
|
||||||
}
|
}
|
||||||
@@ -420,7 +478,7 @@ impl BytesMut {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for BytesMut {
|
impl Drop for Inner {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if !self.is_shared() {
|
if !self.is_shared() {
|
||||||
unsafe {
|
unsafe {
|
||||||
@@ -431,6 +489,8 @@ impl Drop for BytesMut {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsafe impl Send for Inner {}
|
||||||
|
|
||||||
impl IntoBuf for BytesMut {
|
impl IntoBuf for BytesMut {
|
||||||
type Buf = SliceBuf<Self>;
|
type Buf = SliceBuf<Self>;
|
||||||
|
|
||||||
@@ -449,7 +509,7 @@ impl<'a> IntoBuf for &'a BytesMut {
|
|||||||
|
|
||||||
impl AsRef<[u8]> for BytesMut {
|
impl AsRef<[u8]> for BytesMut {
|
||||||
fn as_ref(&self) -> &[u8] {
|
fn as_ref(&self) -> &[u8] {
|
||||||
unsafe { slice::from_raw_parts(self.ptr, self.len) }
|
self.inner.as_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -476,10 +536,12 @@ impl From<Vec<u8>> for BytesMut {
|
|||||||
mem::forget(src);
|
mem::forget(src);
|
||||||
|
|
||||||
BytesMut {
|
BytesMut {
|
||||||
ptr: ptr,
|
inner: Inner {
|
||||||
len: len,
|
ptr: ptr,
|
||||||
cap: cap,
|
len: len,
|
||||||
arc: UnsafeCell::new(None),
|
cap: cap,
|
||||||
|
arc: UnsafeCell::new(None),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -492,7 +554,7 @@ impl<'a> From<&'a [u8]> for BytesMut {
|
|||||||
|
|
||||||
impl PartialEq for BytesMut {
|
impl PartialEq for BytesMut {
|
||||||
fn eq(&self, other: &BytesMut) -> bool {
|
fn eq(&self, other: &BytesMut) -> bool {
|
||||||
**self == **other
|
self.inner.as_ref() == other.inner.as_ref()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -501,12 +563,10 @@ impl Eq for BytesMut {
|
|||||||
|
|
||||||
impl fmt::Debug for BytesMut {
|
impl fmt::Debug for BytesMut {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt::Debug::fmt(self.as_ref(), fmt)
|
fmt::Debug::fmt(self.inner.as_ref(), fmt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Send for BytesMut {}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
*
|
*
|
||||||
* ===== PartialEq =====
|
* ===== PartialEq =====
|
||||||
@@ -553,7 +613,7 @@ impl<'a> PartialEq<BytesMut> for &'a [u8] {
|
|||||||
|
|
||||||
impl PartialEq<[u8]> for Bytes {
|
impl PartialEq<[u8]> for Bytes {
|
||||||
fn eq(&self, other: &[u8]) -> bool {
|
fn eq(&self, other: &[u8]) -> bool {
|
||||||
self.inner == *other
|
self.inner.as_ref() == other
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user