1 use super::{Buf};
2 
3 use std::io;
4 
5 /// Conversion into a `Buf`
6 ///
7 /// An `IntoBuf` implementation defines how to convert a value into a `Buf`.
8 /// This is common for types that represent byte storage of some kind. `IntoBuf`
9 /// may be implemented directly for types or on references for those types.
10 ///
11 /// # Examples
12 ///
13 /// ```
14 /// use bytes::{Buf, IntoBuf, BigEndian};
15 ///
16 /// let bytes = b"\x00\x01hello world";
17 /// let mut buf = bytes.into_buf();
18 ///
19 /// assert_eq!(1, buf.get_u16::<BigEndian>());
20 ///
21 /// let mut rest = [0; 11];
22 /// buf.copy_to_slice(&mut rest);
23 ///
24 /// assert_eq!(b"hello world", &rest);
25 /// ```
26 pub trait IntoBuf {
27     /// The `Buf` type that `self` is being converted into
28     type Buf: Buf;
29 
30     /// Creates a `Buf` from a value.
31     ///
32     /// # Examples
33     ///
34     /// ```
35     /// use bytes::{Buf, IntoBuf, BigEndian};
36     ///
37     /// let bytes = b"\x00\x01hello world";
38     /// let mut buf = bytes.into_buf();
39     ///
40     /// assert_eq!(1, buf.get_u16::<BigEndian>());
41     ///
42     /// let mut rest = [0; 11];
43     /// buf.copy_to_slice(&mut rest);
44     ///
45     /// assert_eq!(b"hello world", &rest);
46     /// ```
into_buf(self) -> Self::Buf47     fn into_buf(self) -> Self::Buf;
48 }
49 
50 impl<T: Buf> IntoBuf for T {
51     type Buf = Self;
52 
into_buf(self) -> Self53     fn into_buf(self) -> Self {
54         self
55     }
56 }
57 
58 impl<'a> IntoBuf for &'a [u8] {
59     type Buf = io::Cursor<&'a [u8]>;
60 
into_buf(self) -> Self::Buf61     fn into_buf(self) -> Self::Buf {
62         io::Cursor::new(self)
63     }
64 }
65 
66 impl<'a> IntoBuf for &'a mut [u8] {
67     type Buf = io::Cursor<&'a mut [u8]>;
68 
into_buf(self) -> Self::Buf69     fn into_buf(self) -> Self::Buf {
70         io::Cursor::new(self)
71     }
72 }
73 
74 impl<'a> IntoBuf for &'a str {
75     type Buf = io::Cursor<&'a [u8]>;
76 
into_buf(self) -> Self::Buf77     fn into_buf(self) -> Self::Buf {
78         self.as_bytes().into_buf()
79     }
80 }
81 
82 impl IntoBuf for Vec<u8> {
83     type Buf = io::Cursor<Vec<u8>>;
84 
into_buf(self) -> Self::Buf85     fn into_buf(self) -> Self::Buf {
86         io::Cursor::new(self)
87     }
88 }
89 
90 impl<'a> IntoBuf for &'a Vec<u8> {
91     type Buf = io::Cursor<&'a [u8]>;
92 
into_buf(self) -> Self::Buf93     fn into_buf(self) -> Self::Buf {
94         io::Cursor::new(&self[..])
95     }
96 }
97 
98 // Kind of annoying... but this impl is required to allow passing `&'static
99 // [u8]` where for<'a> &'a T: IntoBuf is required.
100 impl<'a> IntoBuf for &'a &'static [u8] {
101     type Buf = io::Cursor<&'static [u8]>;
102 
into_buf(self) -> Self::Buf103     fn into_buf(self) -> Self::Buf {
104         io::Cursor::new(self)
105     }
106 }
107 
108 impl<'a> IntoBuf for &'a &'static str {
109     type Buf = io::Cursor<&'static [u8]>;
110 
into_buf(self) -> Self::Buf111     fn into_buf(self) -> Self::Buf {
112         self.as_bytes().into_buf()
113     }
114 }
115 
116 impl IntoBuf for String {
117     type Buf = io::Cursor<Vec<u8>>;
118 
into_buf(self) -> Self::Buf119     fn into_buf(self) -> Self::Buf {
120         self.into_bytes().into_buf()
121     }
122 }
123 
124 impl<'a> IntoBuf for &'a String {
125     type Buf = io::Cursor<&'a [u8]>;
126 
into_buf(self) -> Self::Buf127     fn into_buf(self) -> Self::Buf {
128         self.as_bytes().into_buf()
129     }
130 }
131 
132 impl IntoBuf for u8 {
133     type Buf = Option<[u8; 1]>;
134 
into_buf(self) -> Self::Buf135     fn into_buf(self) -> Self::Buf {
136         Some([self])
137     }
138 }
139 
140 impl IntoBuf for i8 {
141     type Buf = Option<[u8; 1]>;
142 
into_buf(self) -> Self::Buf143     fn into_buf(self) -> Self::Buf {
144         Some([self as u8; 1])
145     }
146 }
147