bytes_expand/buf/
into_buf.rs

1use super::{Buf};
2
3use 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/// ```
26pub 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    /// ```
47    fn into_buf(self) -> Self::Buf;
48}
49
50impl<T: Buf> IntoBuf for T {
51    type Buf = Self;
52
53    fn into_buf(self) -> Self {
54        self
55    }
56}
57
58impl<'a> IntoBuf for &'a [u8] {
59    type Buf = io::Cursor<&'a [u8]>;
60
61    fn into_buf(self) -> Self::Buf {
62        io::Cursor::new(self)
63    }
64}
65
66impl<'a> IntoBuf for &'a mut [u8] {
67    type Buf = io::Cursor<&'a mut [u8]>;
68
69    fn into_buf(self) -> Self::Buf {
70        io::Cursor::new(self)
71    }
72}
73
74impl<'a> IntoBuf for &'a str {
75    type Buf = io::Cursor<&'a [u8]>;
76
77    fn into_buf(self) -> Self::Buf {
78        self.as_bytes().into_buf()
79    }
80}
81
82impl IntoBuf for Vec<u8> {
83    type Buf = io::Cursor<Vec<u8>>;
84
85    fn into_buf(self) -> Self::Buf {
86        io::Cursor::new(self)
87    }
88}
89
90impl<'a> IntoBuf for &'a Vec<u8> {
91    type Buf = io::Cursor<&'a [u8]>;
92
93    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.
100impl<'a> IntoBuf for &'a &'static [u8] {
101    type Buf = io::Cursor<&'static [u8]>;
102
103    fn into_buf(self) -> Self::Buf {
104        io::Cursor::new(self)
105    }
106}
107
108impl<'a> IntoBuf for &'a &'static str {
109    type Buf = io::Cursor<&'static [u8]>;
110
111    fn into_buf(self) -> Self::Buf {
112        self.as_bytes().into_buf()
113    }
114}
115
116impl IntoBuf for String {
117    type Buf = io::Cursor<Vec<u8>>;
118
119    fn into_buf(self) -> Self::Buf {
120        self.into_bytes().into_buf()
121    }
122}
123
124impl<'a> IntoBuf for &'a String {
125    type Buf = io::Cursor<&'a [u8]>;
126
127    fn into_buf(self) -> Self::Buf {
128        self.as_bytes().into_buf()
129    }
130}
131
132impl IntoBuf for u8 {
133    type Buf = Option<[u8; 1]>;
134
135    fn into_buf(self) -> Self::Buf {
136        Some([self])
137    }
138}
139
140impl IntoBuf for i8 {
141    type Buf = Option<[u8; 1]>;
142
143    fn into_buf(self) -> Self::Buf {
144        Some([self as u8; 1])
145    }
146}