bytes_expand/buf/chain.rs
1use {Buf, BufMut};
2use iovec::IoVec;
3
4/// A `Chain` sequences two buffers.
5///
6/// `Chain` is an adapter that links two underlying buffers and provides a
7/// continous view across both buffers. It is able to sequence either immutable
8/// buffers ([`Buf`] values) or mutable buffers ([`BufMut`] values).
9///
10/// This struct is generally created by calling [`Buf::chain`]. Please see that
11/// function's documentation for more detail.
12///
13/// # Examples
14///
15/// ```
16/// use bytes::{Bytes, Buf, IntoBuf};
17/// use bytes::buf::Chain;
18///
19/// let buf = Bytes::from(&b"hello "[..]).into_buf()
20/// .chain(Bytes::from(&b"world"[..]));
21///
22/// let full: Bytes = buf.collect();
23/// assert_eq!(full[..], b"hello world"[..]);
24/// ```
25///
26/// [`Buf::chain`]: trait.Buf.html#method.chain
27/// [`Buf`]: trait.Buf.html
28/// [`BufMut`]: trait.BufMut.html
29#[derive(Debug)]
30pub struct Chain<T, U> {
31 a: T,
32 b: U,
33}
34
35impl<T, U> Chain<T, U> {
36 /// Creates a new `Chain` sequencing the provided values.
37 ///
38 /// # Examples
39 ///
40 /// ```
41 /// use bytes::BytesMut;
42 /// use bytes::buf::Chain;
43 ///
44 /// let buf = Chain::new(
45 /// BytesMut::with_capacity(1024),
46 /// BytesMut::with_capacity(1024));
47 ///
48 /// // Use the chained buffer
49 /// ```
50 pub fn new(a: T, b: U) -> Chain<T, U> {
51 Chain {
52 a: a,
53 b: b,
54 }
55 }
56
57 /// Gets a reference to the first underlying `Buf`.
58 ///
59 /// # Examples
60 ///
61 /// ```
62 /// use bytes::{Bytes, Buf, IntoBuf};
63 ///
64 /// let buf = Bytes::from(&b"hello"[..]).into_buf()
65 /// .chain(Bytes::from(&b"world"[..]));
66 ///
67 /// assert_eq!(buf.first_ref().get_ref()[..], b"hello"[..]);
68 /// ```
69 pub fn first_ref(&self) -> &T {
70 &self.a
71 }
72
73 /// Gets a mutable reference to the first underlying `Buf`.
74 ///
75 /// # Examples
76 ///
77 /// ```
78 /// use bytes::{Bytes, Buf, IntoBuf};
79 ///
80 /// let mut buf = Bytes::from(&b"hello "[..]).into_buf()
81 /// .chain(Bytes::from(&b"world"[..]));
82 ///
83 /// buf.first_mut().set_position(1);
84 ///
85 /// let full: Bytes = buf.collect();
86 /// assert_eq!(full[..], b"ello world"[..]);
87 /// ```
88 pub fn first_mut(&mut self) -> &mut T {
89 &mut self.a
90 }
91
92 /// Gets a reference to the last underlying `Buf`.
93 ///
94 /// # Examples
95 ///
96 /// ```
97 /// use bytes::{Bytes, Buf, IntoBuf};
98 ///
99 /// let buf = Bytes::from(&b"hello"[..]).into_buf()
100 /// .chain(Bytes::from(&b"world"[..]));
101 ///
102 /// assert_eq!(buf.last_ref().get_ref()[..], b"world"[..]);
103 /// ```
104 pub fn last_ref(&self) -> &U {
105 &self.b
106 }
107
108 /// Gets a mutable reference to the last underlying `Buf`.
109 ///
110 /// # Examples
111 ///
112 /// ```
113 /// use bytes::{Bytes, Buf, IntoBuf};
114 ///
115 /// let mut buf = Bytes::from(&b"hello "[..]).into_buf()
116 /// .chain(Bytes::from(&b"world"[..]));
117 ///
118 /// buf.last_mut().set_position(1);
119 ///
120 /// let full: Bytes = buf.collect();
121 /// assert_eq!(full[..], b"hello orld"[..]);
122 /// ```
123 pub fn last_mut(&mut self) -> &mut U {
124 &mut self.b
125 }
126
127 /// Consumes this `Chain`, returning the underlying values.
128 ///
129 /// # Examples
130 ///
131 /// ```
132 /// use bytes::{Bytes, Buf, IntoBuf};
133 ///
134 /// let buf = Bytes::from(&b"hello"[..]).into_buf()
135 /// .chain(Bytes::from(&b"world"[..]));
136 ///
137 /// let (first, last) = buf.into_inner();
138 /// assert_eq!(first.get_ref()[..], b"hello"[..]);
139 /// assert_eq!(last.get_ref()[..], b"world"[..]);
140 /// ```
141 pub fn into_inner(self) -> (T, U) {
142 (self.a, self.b)
143 }
144}
145
146impl<T, U> Buf for Chain<T, U>
147 where T: Buf,
148 U: Buf,
149{
150 fn remaining(&self) -> usize {
151 self.a.remaining() + self.b.remaining()
152 }
153
154 fn bytes(&self) -> &[u8] {
155 if self.a.has_remaining() {
156 self.a.bytes()
157 } else {
158 self.b.bytes()
159 }
160 }
161
162 fn advance(&mut self, mut cnt: usize) {
163 let a_rem = self.a.remaining();
164
165 if a_rem != 0 {
166 if a_rem >= cnt {
167 self.a.advance(cnt);
168 return;
169 }
170
171 // Consume what is left of a
172 self.a.advance(a_rem);
173
174 cnt -= a_rem;
175 }
176
177 self.b.advance(cnt);
178 }
179
180 fn bytes_vec<'a>(&'a self, dst: &mut [&'a IoVec]) -> usize {
181 let mut n = self.a.bytes_vec(dst);
182 n += self.b.bytes_vec(&mut dst[n..]);
183 n
184 }
185}
186
187impl<T, U> BufMut for Chain<T, U>
188 where T: BufMut,
189 U: BufMut,
190{
191 fn remaining_mut(&self) -> usize {
192 self.a.remaining_mut() + self.b.remaining_mut()
193 }
194
195 unsafe fn bytes_mut(&mut self) -> &mut [u8] {
196 if self.a.has_remaining_mut() {
197 self.a.bytes_mut()
198 } else {
199 self.b.bytes_mut()
200 }
201 }
202
203 unsafe fn advance_mut(&mut self, mut cnt: usize) {
204 let a_rem = self.a.remaining_mut();
205
206 if a_rem != 0 {
207 if a_rem >= cnt {
208 self.a.advance_mut(cnt);
209 return;
210 }
211
212 // Consume what is left of a
213 self.a.advance_mut(a_rem);
214
215 cnt -= a_rem;
216 }
217
218 self.b.advance_mut(cnt);
219 }
220
221 unsafe fn bytes_vec_mut<'a>(&'a mut self, dst: &mut [&'a mut IoVec]) -> usize {
222 let mut n = self.a.bytes_vec_mut(dst);
223 n += self.b.bytes_vec_mut(&mut dst[n..]);
224 n
225 }
226}