mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-16 00:00:15 +02:00
Implement chain combinator for Buf
This commit is contained in:
+25
-1
@@ -1,4 +1,4 @@
|
||||
use super::{Take, Reader, Iter, FromBuf};
|
||||
use super::{IntoBuf, Take, Reader, Iter, FromBuf, Chain};
|
||||
use byteorder::ByteOrder;
|
||||
use iovec::IoVec;
|
||||
|
||||
@@ -522,6 +522,30 @@ pub trait Buf {
|
||||
super::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::{Bytes, Buf, IntoBuf};
|
||||
/// use bytes::buf::Chain;
|
||||
///
|
||||
/// let buf = Bytes::from(&b"hello "[..]).into_buf()
|
||||
/// .chain(Bytes::from(&b"world"[..]));
|
||||
///
|
||||
/// let full: Bytes = buf.collect();
|
||||
/// assert_eq!(full[..], b"hello world"[..]);
|
||||
/// ```
|
||||
fn chain<U>(self, next: U) -> Chain<Self, U::Buf>
|
||||
where U: IntoBuf,
|
||||
Self: Sized,
|
||||
{
|
||||
Chain::new(self, next.into_buf())
|
||||
}
|
||||
|
||||
/// Creates a "by reference" adaptor for this instance of `Buf`.
|
||||
///
|
||||
/// The returned adaptor also implements `Buf` and will simply borrow `self`.
|
||||
|
||||
Reference in New Issue
Block a user