mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-21 00:00:12 +02:00
Add explicit inlines
This commit is contained in:
@@ -10,11 +10,13 @@ pub struct ByteBuf {
|
|||||||
|
|
||||||
impl ByteBuf {
|
impl ByteBuf {
|
||||||
/// Create a new `ByteBuf` with 8kb capacity
|
/// Create a new `ByteBuf` with 8kb capacity
|
||||||
|
#[inline]
|
||||||
pub fn new() -> ByteBuf {
|
pub fn new() -> ByteBuf {
|
||||||
ByteBuf::with_capacity(8 * 1024)
|
ByteBuf::with_capacity(8 * 1024)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new `ByteBuf` with `cap` capacity
|
/// Create a new `ByteBuf` with `cap` capacity
|
||||||
|
#[inline]
|
||||||
pub fn with_capacity(cap: usize) -> ByteBuf {
|
pub fn with_capacity(cap: usize) -> ByteBuf {
|
||||||
ByteBuf {
|
ByteBuf {
|
||||||
mem: BytesMut::with_capacity(cap),
|
mem: BytesMut::with_capacity(cap),
|
||||||
@@ -23,6 +25,7 @@ impl ByteBuf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new `ByteBuf` backed by `bytes`
|
/// Create a new `ByteBuf` backed by `bytes`
|
||||||
|
#[inline]
|
||||||
pub fn from_bytes(bytes: BytesMut) -> ByteBuf {
|
pub fn from_bytes(bytes: BytesMut) -> ByteBuf {
|
||||||
ByteBuf {
|
ByteBuf {
|
||||||
mem: bytes,
|
mem: bytes,
|
||||||
@@ -31,6 +34,7 @@ impl ByteBuf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new `ByteBuf` containing the given slice
|
/// Create a new `ByteBuf` containing the given slice
|
||||||
|
#[inline]
|
||||||
pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> ByteBuf {
|
pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> ByteBuf {
|
||||||
let mut buf = ByteBuf::with_capacity(bytes.as_ref().len());
|
let mut buf = ByteBuf::with_capacity(bytes.as_ref().len());
|
||||||
buf.copy_from_slice(bytes.as_ref());
|
buf.copy_from_slice(bytes.as_ref());
|
||||||
@@ -137,19 +141,23 @@ impl ByteBuf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Buf for ByteBuf {
|
impl Buf for ByteBuf {
|
||||||
|
#[inline]
|
||||||
fn remaining(&self) -> usize {
|
fn remaining(&self) -> usize {
|
||||||
self.len() - self.rd
|
self.len() - self.rd
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn bytes(&self) -> &[u8] {
|
fn bytes(&self) -> &[u8] {
|
||||||
&self.mem[self.rd..]
|
&self.mem[self.rd..]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn advance(&mut self, cnt: usize) {
|
fn advance(&mut self, cnt: usize) {
|
||||||
assert!(cnt <= self.remaining(), "buffer overflow");
|
assert!(cnt <= self.remaining(), "buffer overflow");
|
||||||
self.rd += cnt;
|
self.rd += cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn copy_to_slice(&mut self, dst: &mut [u8]) {
|
fn copy_to_slice(&mut self, dst: &mut [u8]) {
|
||||||
assert!(self.remaining() >= dst.len());
|
assert!(self.remaining() >= dst.len());
|
||||||
|
|
||||||
@@ -160,20 +168,24 @@ impl Buf for ByteBuf {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl BufMut for ByteBuf {
|
impl BufMut for ByteBuf {
|
||||||
|
#[inline]
|
||||||
fn remaining_mut(&self) -> usize {
|
fn remaining_mut(&self) -> usize {
|
||||||
self.capacity() - self.len()
|
self.capacity() - self.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
unsafe fn advance_mut(&mut self, cnt: usize) {
|
unsafe fn advance_mut(&mut self, cnt: usize) {
|
||||||
let new_len = self.len() + cnt;
|
let new_len = self.len() + cnt;
|
||||||
self.mem.set_len(new_len);
|
self.mem.set_len(new_len);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
|
unsafe fn bytes_mut(&mut self) -> &mut [u8] {
|
||||||
let len = self.len();
|
let len = self.len();
|
||||||
&mut self.mem.as_raw()[len..]
|
&mut self.mem.as_raw()[len..]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn copy_from_slice(&mut self, src: &[u8]) {
|
fn copy_from_slice(&mut self, src: &[u8]) {
|
||||||
assert!(self.remaining_mut() >= src.len());
|
assert!(self.remaining_mut() >= src.len());
|
||||||
|
|
||||||
|
|||||||
@@ -40,7 +40,22 @@ pub struct BytesMut {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
impl Bytes {
|
impl Bytes {
|
||||||
|
/// Creates a new empty `Bytes`
|
||||||
|
#[inline]
|
||||||
|
pub fn new() -> Bytes {
|
||||||
|
use std::ptr;
|
||||||
|
Bytes {
|
||||||
|
inner: BytesMut {
|
||||||
|
ptr: ptr::null_mut(),
|
||||||
|
len: 0,
|
||||||
|
cap: 0,
|
||||||
|
arc: UnsafeCell::new(None),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a new `Bytes` and copy the given slice into it.
|
/// Creates a new `Bytes` and copy the given slice into it.
|
||||||
|
#[inline]
|
||||||
pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> Bytes {
|
pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> Bytes {
|
||||||
BytesMut::from_slice(bytes).freeze()
|
BytesMut::from_slice(bytes).freeze()
|
||||||
}
|
}
|
||||||
@@ -210,32 +225,38 @@ unsafe impl Sync for Bytes {}
|
|||||||
|
|
||||||
impl BytesMut {
|
impl BytesMut {
|
||||||
/// Create a new `BytesMut` with the specified capacity.
|
/// Create a new `BytesMut` with the specified capacity.
|
||||||
|
#[inline]
|
||||||
pub fn with_capacity(cap: usize) -> BytesMut {
|
pub fn with_capacity(cap: usize) -> BytesMut {
|
||||||
BytesMut::from(Vec::with_capacity(cap))
|
BytesMut::from(Vec::with_capacity(cap))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new `BytesMut` and copy the given slice into it.
|
/// Creates a new `BytesMut` and copy the given slice into it.
|
||||||
|
#[inline]
|
||||||
pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> BytesMut {
|
pub fn from_slice<T: AsRef<[u8]>>(bytes: T) -> BytesMut {
|
||||||
let buf = ByteBuf::from_slice(bytes);
|
let buf = ByteBuf::from_slice(bytes);
|
||||||
buf.into_inner()
|
buf.into_inner()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of bytes contained in this `BytesMut`.
|
/// Returns the number of bytes contained in this `BytesMut`.
|
||||||
|
/// #[inline]
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.len
|
self.len
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns true if the value contains no bytes
|
/// Returns true if the value contains no bytes
|
||||||
|
/// #[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]
|
||||||
pub fn capacity(&self) -> usize {
|
pub fn capacity(&self) -> usize {
|
||||||
self.cap
|
self.cap
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return an immutable handle to the bytes
|
/// Return an immutable handle to the bytes
|
||||||
|
/// #[inline]
|
||||||
pub fn freeze(self) -> Bytes {
|
pub fn freeze(self) -> Bytes {
|
||||||
Bytes { inner: self }
|
Bytes { inner: self }
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user