aboutsummaryrefslogtreecommitdiff
path: root/src/buf/buf.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/buf/buf.rs')
-rw-r--r--src/buf/buf.rs77
1 files changed, 43 insertions, 34 deletions
diff --git a/src/buf/buf.rs b/src/buf/buf.rs
index 97b85fc..e126bb4 100644
--- a/src/buf/buf.rs
+++ b/src/buf/buf.rs
@@ -1,4 +1,4 @@
-use super::{IntoBuf, Take, Reader, FromBuf, Chain};
+use super::{Take, Reader, Chain};
use std::{cmp, io::IoSlice, ptr, mem};
@@ -787,30 +787,6 @@ pub trait Buf {
f64::from_bits(Self::get_u64_le(self))
}
- /// Transforms a `Buf` into a concrete buffer.
- ///
- /// `collect()` can operate on any value that implements `Buf`, and turn it
- /// into the relevant concrete buffer type.
- ///
- /// # Examples
- ///
- /// Collecting a buffer and loading the contents into a `Vec<u8>`.
- ///
- /// ```
- /// use bytes::Buf;
- ///
- /// let buf = &b"hello world"[..];
- /// let vec: Vec<u8> = buf.collect();
- ///
- /// assert_eq!(vec, b"hello world");
- /// ```
- fn collect<B>(self) -> B
- where Self: Sized,
- B: FromBuf,
- {
- B::from_buf(self)
- }
-
/// Creates an adaptor which will read at most `limit` bytes from `self`.
///
/// This function returns a new instance of `Buf` which will read at most
@@ -848,16 +824,15 @@ pub trait Buf {
/// ```
/// use bytes::Buf;
///
- /// let chain = b"hello "[..].chain(&b"world"[..]);
+ /// let mut chain = b"hello "[..].chain(&b"world"[..]);
///
- /// let full: Vec<u8> = chain.collect();
- /// assert_eq!(full, b"hello world");
+ /// let full = chain.to_bytes();
+ /// assert_eq!(full.bytes(), b"hello world");
/// ```
- fn chain<U>(self, next: U) -> Chain<Self, U::Buf>
- where U: IntoBuf,
- Self: Sized,
+ fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
+ where Self: Sized
{
- Chain::new(self, next.into_buf())
+ Chain::new(self, next)
}
/// Creates a "by reference" adaptor for this instance of `Buf`.
@@ -896,10 +871,10 @@ pub trait Buf {
/// # Examples
///
/// ```
- /// use bytes::{Buf, IntoBuf, Bytes};
+ /// use bytes::{Buf, Bytes};
/// use std::io::Read;
///
- /// let buf = Bytes::from("hello world").into_buf();
+ /// let buf = Bytes::from("hello world");
///
/// let mut reader = buf.reader();
/// let mut dst = [0; 1024];
@@ -912,6 +887,23 @@ pub trait Buf {
fn reader(self) -> Reader<Self> where Self: Sized {
super::reader::new(self)
}
+
+ /// Consumes remaining bytes inside self and returns new instance of `Bytes`
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Buf};
+ ///
+ /// let bytes = "hello world".to_bytes();
+ /// assert_eq!(&bytes[..], &b"hello world"[..]);
+ /// ```
+ fn to_bytes(&mut self) -> crate::Bytes {
+ use super::BufMut;
+ let mut ret = crate::BytesMut::with_capacity(self.remaining());
+ ret.put(self);
+ ret.freeze()
+ }
}
impl<T: Buf + ?Sized> Buf for &mut T {
@@ -967,6 +959,23 @@ impl Buf for &[u8] {
}
}
+impl Buf for &str {
+ #[inline]
+ fn remaining(&self) -> usize {
+ self.len()
+ }
+
+ #[inline]
+ fn bytes(&self) -> &[u8] {
+ self.as_bytes()
+ }
+
+ #[inline]
+ fn advance(&mut self, cnt: usize) {
+ *self = &self[cnt..];
+ }
+}
+
impl Buf for Option<[u8; 1]> {
fn remaining(&self) -> usize {
if self.is_some() {