bytes/buf/chain.rs
1use crate::buf::{IntoIter, UninitSlice};
2use crate::{Buf, BufMut};
3
4#[cfg(feature = "std")]
5use std::io::IoSlice;
6
7/// A `Chain` sequences two buffers.
8///
9/// `Chain` is an adapter that links two underlying buffers and provides a
10/// continuous view across both buffers. It is able to sequence either immutable
11/// buffers ([`Buf`] values) or mutable buffers ([`BufMut`] values).
12///
13/// This struct is generally created by calling [`Buf::chain`]. Please see that
14/// function's documentation for more detail.
15///
16/// # Examples
17///
18/// ```
19/// use bytes::{Bytes, Buf};
20///
21/// let mut buf = (&b"hello "[..])
22/// .chain(&b"world"[..]);
23///
24/// let full: Bytes = buf.copy_to_bytes(11);
25/// assert_eq!(full[..], b"hello world"[..]);
26/// ```
27///
28/// [`Buf::chain`]: Buf::chain
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 pub(crate) fn new(a: T, b: U) -> Chain<T, U> {
38 Chain { a, b }
39 }
40
41 /// Gets a reference to the first underlying `Buf`.
42 ///
43 /// # Examples
44 ///
45 /// ```
46 /// use bytes::Buf;
47 ///
48 /// let buf = (&b"hello"[..])
49 /// .chain(&b"world"[..]);
50 ///
51 /// assert_eq!(buf.first_ref()[..], b"hello"[..]);
52 /// ```
53 pub fn first_ref(&self) -> &T {
54 &self.a
55 }
56
57 /// Gets a mutable reference to the first underlying `Buf`.
58 ///
59 /// # Examples
60 ///
61 /// ```
62 /// use bytes::Buf;
63 ///
64 /// let mut buf = (&b"hello"[..])
65 /// .chain(&b"world"[..]);
66 ///
67 /// buf.first_mut().advance(1);
68 ///
69 /// let full = buf.copy_to_bytes(9);
70 /// assert_eq!(full, b"elloworld"[..]);
71 /// ```
72 pub fn first_mut(&mut self) -> &mut T {
73 &mut self.a
74 }
75
76 /// Gets a reference to the last underlying `Buf`.
77 ///
78 /// # Examples
79 ///
80 /// ```
81 /// use bytes::Buf;
82 ///
83 /// let buf = (&b"hello"[..])
84 /// .chain(&b"world"[..]);
85 ///
86 /// assert_eq!(buf.last_ref()[..], b"world"[..]);
87 /// ```
88 pub fn last_ref(&self) -> &U {
89 &self.b
90 }
91
92 /// Gets a mutable reference to the last underlying `Buf`.
93 ///
94 /// # Examples
95 ///
96 /// ```
97 /// use bytes::Buf;
98 ///
99 /// let mut buf = (&b"hello "[..])
100 /// .chain(&b"world"[..]);
101 ///
102 /// buf.last_mut().advance(1);
103 ///
104 /// let full = buf.copy_to_bytes(10);
105 /// assert_eq!(full, b"hello orld"[..]);
106 /// ```
107 pub fn last_mut(&mut self) -> &mut U {
108 &mut self.b
109 }
110
111 /// Consumes this `Chain`, returning the underlying values.
112 ///
113 /// # Examples
114 ///
115 /// ```
116 /// use bytes::Buf;
117 ///
118 /// let chain = (&b"hello"[..])
119 /// .chain(&b"world"[..]);
120 ///
121 /// let (first, last) = chain.into_inner();
122 /// assert_eq!(first[..], b"hello"[..]);
123 /// assert_eq!(last[..], b"world"[..]);
124 /// ```
125 pub fn into_inner(self) -> (T, U) {
126 (self.a, self.b)
127 }
128}
129
130impl<T, U> Buf for Chain<T, U>
131where
132 T: Buf,
133 U: Buf,
134{
135 fn remaining(&self) -> usize {
136 self.a.remaining().saturating_add(self.b.remaining())
137 }
138
139 fn chunk(&self) -> &[u8] {
140 if self.a.has_remaining() {
141 self.a.chunk()
142 } else {
143 self.b.chunk()
144 }
145 }
146
147 fn advance(&mut self, mut cnt: usize) {
148 let a_rem = self.a.remaining();
149
150 if a_rem != 0 {
151 if a_rem >= cnt {
152 self.a.advance(cnt);
153 return;
154 }
155
156 // Consume what is left of a
157 self.a.advance(a_rem);
158
159 cnt -= a_rem;
160 }
161
162 self.b.advance(cnt);
163 }
164
165 #[cfg(feature = "std")]
166 fn chunks_vectored<'a>(&'a self, dst: &mut [IoSlice<'a>]) -> usize {
167 let mut n = self.a.chunks_vectored(dst);
168 if n == dst.len() {
169 return n;
170 }
171
172 // Only add b's chunks once a's chunks cover all of a,
173 // otherwise bytes would come out of order.
174 let a_len = dst[..n].iter().map(|slice| slice.len()).sum::<usize>();
175 if a_len == self.a.remaining() {
176 n += self.b.chunks_vectored(&mut dst[n..]);
177 }
178 n
179 }
180
181 fn copy_to_bytes(&mut self, len: usize) -> crate::Bytes {
182 let a_rem = self.a.remaining();
183 if a_rem >= len {
184 self.a.copy_to_bytes(len)
185 } else if a_rem == 0 {
186 self.b.copy_to_bytes(len)
187 } else {
188 assert!(
189 len - a_rem <= self.b.remaining(),
190 "`len` greater than remaining"
191 );
192 let mut ret = crate::BytesMut::with_capacity(len);
193 ret.put(&mut self.a);
194 ret.put((&mut self.b).take(len - a_rem));
195 ret.freeze()
196 }
197 }
198}
199
200unsafe impl<T, U> BufMut for Chain<T, U>
201where
202 T: BufMut,
203 U: BufMut,
204{
205 fn remaining_mut(&self) -> usize {
206 self.a
207 .remaining_mut()
208 .saturating_add(self.b.remaining_mut())
209 }
210
211 fn chunk_mut(&mut self) -> &mut UninitSlice {
212 if self.a.has_remaining_mut() {
213 self.a.chunk_mut()
214 } else {
215 self.b.chunk_mut()
216 }
217 }
218
219 unsafe fn advance_mut(&mut self, mut cnt: usize) {
220 let a_rem = self.a.remaining_mut();
221
222 if a_rem != 0 {
223 if a_rem >= cnt {
224 self.a.advance_mut(cnt);
225 return;
226 }
227
228 // Consume what is left of a
229 self.a.advance_mut(a_rem);
230
231 cnt -= a_rem;
232 }
233
234 self.b.advance_mut(cnt);
235 }
236}
237
238impl<T, U> IntoIterator for Chain<T, U>
239where
240 T: Buf,
241 U: Buf,
242{
243 type Item = u8;
244 type IntoIter = IntoIter<Chain<T, U>>;
245
246 fn into_iter(self) -> Self::IntoIter {
247 IntoIter::new(self)
248 }
249}