aboutsummaryrefslogtreecommitdiff
path: root/src/buf/buf_impl.rs
diff options
context:
space:
mode:
authorGravatar Carl Lerche <me@carllerche.com> 2020-10-16 15:16:23 -0700
committerGravatar GitHub <noreply@github.com> 2020-10-16 15:16:23 -0700
commit94c543f74b111e894d16faa43e4ad361b97ee87d (patch)
tree8cad5f70cc986e8b1952dc52436933e9a5a34f05 /src/buf/buf_impl.rs
parent447530b8a6f97fc6864b39d29b24efb4ac9202d3 (diff)
downloadbytes-94c543f74b111e894d16faa43e4ad361b97ee87d.tar.gz
bytes-94c543f74b111e894d16faa43e4ad361b97ee87d.tar.zst
bytes-94c543f74b111e894d16faa43e4ad361b97ee87d.zip
remove ext traits (#431)
Diffstat (limited to 'src/buf/buf_impl.rs')
-rw-r--r--src/buf/buf_impl.rs85
1 files changed, 85 insertions, 0 deletions
diff --git a/src/buf/buf_impl.rs b/src/buf/buf_impl.rs
index 5cd7c68..c26681f 100644
--- a/src/buf/buf_impl.rs
+++ b/src/buf/buf_impl.rs
@@ -1,3 +1,7 @@
+#[cfg(feature = "std")]
+use crate::buf::{reader, Reader};
+use crate::buf::{take, Chain, Take};
+
use core::{cmp, mem, ptr};
#[cfg(feature = "std")]
@@ -807,6 +811,87 @@ pub trait Buf {
ret.put(self);
ret.freeze()
}
+
+ /// 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
+ /// `limit` bytes.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Buf, BufMut};
+ ///
+ /// let mut buf = b"hello world"[..].take(5);
+ /// let mut dst = vec![];
+ ///
+ /// dst.put(&mut buf);
+ /// assert_eq!(dst, b"hello");
+ ///
+ /// let mut buf = buf.into_inner();
+ /// dst.clear();
+ /// dst.put(&mut buf);
+ /// assert_eq!(dst, b" world");
+ /// ```
+ fn take(self, limit: usize) -> Take<Self>
+ where
+ Self: Sized,
+ {
+ take::new(self, limit)
+ }
+
+ /// Creates an adaptor which will chain this buffer with another.
+ ///
+ /// The returned `Buf` instance will first consume all bytes from `self`.
+ /// Afterwards the output is equivalent to the output of next.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::Buf;
+ ///
+ /// let mut chain = b"hello "[..].chain(&b"world"[..]);
+ ///
+ /// let full = chain.to_bytes();
+ /// assert_eq!(full.bytes(), b"hello world");
+ /// ```
+ fn chain<U: Buf>(self, next: U) -> Chain<Self, U>
+ where
+ Self: Sized,
+ {
+ Chain::new(self, next)
+ }
+
+ /// Creates an adaptor which implements the `Read` trait for `self`.
+ ///
+ /// This function returns a new value which implements `Read` by adapting
+ /// the `Read` trait functions to the `Buf` trait functions. Given that
+ /// `Buf` operations are infallible, none of the `Read` functions will
+ /// return with `Err`.
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use bytes::{Bytes, Buf};
+ /// use std::io::Read;
+ ///
+ /// let buf = Bytes::from("hello world");
+ ///
+ /// let mut reader = buf.reader();
+ /// let mut dst = [0; 1024];
+ ///
+ /// let num = reader.read(&mut dst).unwrap();
+ ///
+ /// assert_eq!(11, num);
+ /// assert_eq!(&dst[..11], &b"hello world"[..]);
+ /// ```
+ #[cfg(feature = "std")]
+ fn reader(self) -> Reader<Self>
+ where
+ Self: Sized,
+ {
+ reader::new(self)
+ }
}
macro_rules! deref_forward_buf {