summaryrefslogtreecommitdiffstats
path: root/third_party/rust/bytes-0.4.12/src/buf/from_buf.rs
blob: 55f5cef31b9043fd7cea86a32e11c57396b9b47d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use {Buf, BufMut, IntoBuf, Bytes, BytesMut};

/// Conversion from a [`Buf`]
///
/// Implementing `FromBuf` for a type defines how it is created from a buffer.
/// This is common for types which represent byte storage of some kind.
///
/// [`FromBuf::from_buf`] is rarely called explicitly, and it is instead used
/// through [`Buf::collect`]. See [`Buf::collect`] documentation for more examples.
///
/// See also [`IntoBuf`].
///
/// # Examples
///
/// Basic  usage:
///
/// ```
/// use bytes::{Bytes, IntoBuf};
/// use bytes::buf::FromBuf;
///
/// let buf = Bytes::from(&b"hello world"[..]).into_buf();
/// let vec = Vec::from_buf(buf);
///
/// assert_eq!(vec, &b"hello world"[..]);
/// ```
///
/// Using [`Buf::collect`] to implicitly use `FromBuf`:
///
/// ```
/// 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"[..]);
/// ```
///
/// Implementing `FromBuf` for your type:
///
/// ```
/// use bytes::{BufMut, Bytes};
/// use bytes::buf::{IntoBuf, FromBuf};
///
/// // A sample buffer, that's just a wrapper over Vec<u8>
/// struct MyBuffer(Vec<u8>);
///
/// impl FromBuf for MyBuffer {
///     fn from_buf<B>(buf: B) -> Self where B: IntoBuf {
///         let mut v = Vec::new();
///         v.put(buf.into_buf());
///         MyBuffer(v)
///     }
/// }
///
/// // Now we can make a new buf
/// let buf = Bytes::from(&b"hello world"[..]);
///
/// // And make a MyBuffer out of it
/// let my_buf = MyBuffer::from_buf(buf);
///
/// assert_eq!(my_buf.0, &b"hello world"[..]);
/// ```
///
/// [`Buf`]: trait.Buf.html
/// [`FromBuf::from_buf`]: #method.from_buf
/// [`Buf::collect`]: trait.Buf.html#method.collect
/// [`IntoBuf`]: trait.IntoBuf.html
pub trait FromBuf {
    /// Creates a value from a buffer.
    ///
    /// See the [type-level documentation](#) for more details.
    ///
    /// # Examples
    ///
    /// Basic  usage:
    ///
    /// ```
    /// use bytes::{Bytes, IntoBuf};
    /// use bytes::buf::FromBuf;
    ///
    /// let buf = Bytes::from(&b"hello world"[..]).into_buf();
    /// let vec = Vec::from_buf(buf);
    ///
    /// assert_eq!(vec, &b"hello world"[..]);
    /// ```
    fn from_buf<T>(buf: T) -> Self where T: IntoBuf;
}

impl FromBuf for Vec<u8> {
    fn from_buf<T>(buf: T) -> Self
        where T: IntoBuf
    {
        let buf = buf.into_buf();
        let mut ret = Vec::with_capacity(buf.remaining());
        ret.put(buf);
        ret
    }
}

impl FromBuf for Bytes {
    fn from_buf<T>(buf: T) -> Self
        where T: IntoBuf
    {
        BytesMut::from_buf(buf).freeze()
    }
}

impl FromBuf for BytesMut {
    fn from_buf<T>(buf: T) -> Self
        where T: IntoBuf
    {
        let buf = buf.into_buf();
        let mut ret = BytesMut::with_capacity(buf.remaining());
        ret.put(buf);
        ret
    }
}