bytes_expand/buf/
from_buf.rs

1use {Buf, BufMut, IntoBuf, Bytes, BytesMut};
2
3/// Conversion from a [`Buf`]
4///
5/// Implementing `FromBuf` for a type defines how it is created from a buffer.
6/// This is common for types which represent byte storage of some kind.
7///
8/// [`FromBuf::from_buf`] is rarely called explicitly, and it is instead used
9/// through [`Buf::collect`]. See [`Buf::collect`] documentation for more examples.
10///
11/// See also [`IntoBuf`].
12///
13/// # Examples
14///
15/// Basic  usage:
16///
17/// ```
18/// use bytes::{Bytes, IntoBuf};
19/// use bytes::buf::FromBuf;
20///
21/// let buf = Bytes::from(&b"hello world"[..]).into_buf();
22/// let vec = Vec::from_buf(buf);
23///
24/// assert_eq!(vec, &b"hello world"[..]);
25/// ```
26///
27/// Using [`Buf::collect`] to implicitly use `FromBuf`:
28///
29/// ```
30/// use bytes::{Buf, Bytes, IntoBuf};
31///
32/// let buf = Bytes::from(&b"hello world"[..]).into_buf();
33/// let vec: Vec<u8> = buf.collect();
34///
35/// assert_eq!(vec, &b"hello world"[..]);
36/// ```
37///
38/// Implementing `FromBuf` for your type:
39///
40/// ```
41/// use bytes::{BufMut, Bytes};
42/// use bytes::buf::{IntoBuf, FromBuf};
43///
44/// // A sample buffer, that's just a wrapper over Vec<u8>
45/// struct MyBuffer(Vec<u8>);
46///
47/// impl FromBuf for MyBuffer {
48///     fn from_buf<B>(buf: B) -> Self where B: IntoBuf {
49///         let mut v = Vec::new();
50///         v.put(buf.into_buf());
51///         MyBuffer(v)
52///     }
53/// }
54///
55/// // Now we can make a new buf
56/// let buf = Bytes::from(&b"hello world"[..]);
57///
58/// // And make a MyBuffer out of it
59/// let my_buf = MyBuffer::from_buf(buf);
60///
61/// assert_eq!(my_buf.0, &b"hello world"[..]);
62/// ```
63///
64/// [`Buf`]: trait.Buf.html
65/// [`FromBuf::from_buf`]: #method.from_buf
66/// [`Buf::collect`]: trait.Buf.html#method.collect
67/// [`IntoBuf`]: trait.IntoBuf.html
68pub trait FromBuf {
69    /// Creates a value from a buffer.
70    ///
71    /// See the [type-level documentation](#) for more details.
72    ///
73    /// # Examples
74    ///
75    /// Basic  usage:
76    ///
77    /// ```
78    /// use bytes::{Bytes, IntoBuf};
79    /// use bytes::buf::FromBuf;
80    ///
81    /// let buf = Bytes::from(&b"hello world"[..]).into_buf();
82    /// let vec = Vec::from_buf(buf);
83    ///
84    /// assert_eq!(vec, &b"hello world"[..]);
85    /// ```
86    fn from_buf<T>(buf: T) -> Self where T: IntoBuf;
87}
88
89impl FromBuf for Vec<u8> {
90    fn from_buf<T>(buf: T) -> Self
91        where T: IntoBuf
92    {
93        let buf = buf.into_buf();
94        let mut ret = Vec::with_capacity(buf.remaining());
95        ret.put(buf);
96        ret
97    }
98}
99
100impl FromBuf for Bytes {
101    fn from_buf<T>(buf: T) -> Self
102        where T: IntoBuf
103    {
104        BytesMut::from_buf(buf).freeze()
105    }
106}
107
108impl FromBuf for BytesMut {
109    fn from_buf<T>(buf: T) -> Self
110        where T: IntoBuf
111    {
112        let buf = buf.into_buf();
113        let mut ret = BytesMut::with_capacity(buf.remaining(), crate::DEFAULT_AUTO_EXPAND_SIZE);
114        ret.put(buf);
115        ret
116    }
117}