mirror of
https://github.com/tokio-rs/bytes.git
synced 2026-08-19 00:00:12 +02:00
Implement FromBuf and Buf::collect
Enables collecting the contents of a `Buf` value into a relevant concrete buffer implementation.
This commit is contained in:
+25
-1
@@ -1,4 +1,4 @@
|
||||
use super::{Take, Reader};
|
||||
use super::{Take, Reader, FromBuf};
|
||||
use byteorder::ByteOrder;
|
||||
|
||||
use std::{cmp, ptr};
|
||||
@@ -441,6 +441,30 @@ pub trait Buf {
|
||||
T::read_f64(&buf)
|
||||
}
|
||||
|
||||
/// Transforms a `Buf` into a concrete buffer.
|
||||
///
|
||||
/// `collect()` can operate on any value that implements `Buf`, and turn it
|
||||
/// into the relevent concrete buffer type.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Collecting a buffer and loading the contents into a `Vec<u8>`.
|
||||
///
|
||||
/// ```
|
||||
/// use bytes::{Buf, Bytes, IntoBuf};
|
||||
///
|
||||
/// let buf = Bytes::from(&b"hello world"[..]).into_buf();
|
||||
/// 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
|
||||
|
||||
Reference in New Issue
Block a user