|
| 1 | +use crate::io::IoSliceMut; |
| 2 | +use std::fmt; |
| 3 | +use std::pin::Pin; |
| 4 | + |
| 5 | +use crate::io::{self, BufRead, Read}; |
| 6 | +use crate::task::{Context, Poll}; |
| 7 | + |
| 8 | +/// Adaptor to chain together two readers. |
| 9 | +/// |
| 10 | +/// This struct is generally created by calling [`chain`] on a reader. |
| 11 | +/// Please see the documentation of [`chain`] for more details. |
| 12 | +/// |
| 13 | +/// [`chain`]: trait.Read.html#method.chain |
| 14 | +pub struct Chain<T, U> { |
| 15 | + pub(crate) first: T, |
| 16 | + pub(crate) second: U, |
| 17 | + pub(crate) done_first: bool, |
| 18 | +} |
| 19 | + |
| 20 | +impl<T, U> Chain<T, U> { |
| 21 | + /// Consumes the `Chain`, returning the wrapped readers. |
| 22 | + /// |
| 23 | + /// # Examples |
| 24 | + /// |
| 25 | + /// ```no_run |
| 26 | + /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { |
| 27 | + /// # |
| 28 | + /// use async_std::prelude::*; |
| 29 | + /// use async_std::fs::File; |
| 30 | + /// |
| 31 | + /// let foo_file = File::open("foo.txt").await?; |
| 32 | + /// let bar_file = File::open("bar.txt").await?; |
| 33 | + /// |
| 34 | + /// let chain = foo_file.chain(bar_file); |
| 35 | + /// let (foo_file, bar_file) = chain.into_inner(); |
| 36 | + /// # |
| 37 | + /// # Ok(()) }) } |
| 38 | + /// ``` |
| 39 | + pub fn into_inner(self) -> (T, U) { |
| 40 | + (self.first, self.second) |
| 41 | + } |
| 42 | + |
| 43 | + /// Gets references to the underlying readers in this `Chain`. |
| 44 | + /// |
| 45 | + /// # Examples |
| 46 | + /// |
| 47 | + /// ```no_run |
| 48 | + /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { |
| 49 | + /// # |
| 50 | + /// use async_std::prelude::*; |
| 51 | + /// use async_std::fs::File; |
| 52 | + /// |
| 53 | + /// let foo_file = File::open("foo.txt").await?; |
| 54 | + /// let bar_file = File::open("bar.txt").await?; |
| 55 | + /// |
| 56 | + /// let chain = foo_file.chain(bar_file); |
| 57 | + /// let (foo_file, bar_file) = chain.get_ref(); |
| 58 | + /// # |
| 59 | + /// # Ok(()) }) } |
| 60 | + /// ``` |
| 61 | + pub fn get_ref(&self) -> (&T, &U) { |
| 62 | + (&self.first, &self.second) |
| 63 | + } |
| 64 | + |
| 65 | + /// Gets mutable references to the underlying readers in this `Chain`. |
| 66 | + /// |
| 67 | + /// Care should be taken to avoid modifying the internal I/O state of the |
| 68 | + /// underlying readers as doing so may corrupt the internal state of this |
| 69 | + /// `Chain`. |
| 70 | + /// |
| 71 | + /// # Examples |
| 72 | + /// |
| 73 | + /// ```no_run |
| 74 | + /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { |
| 75 | + /// # |
| 76 | + /// use async_std::prelude::*; |
| 77 | + /// use async_std::fs::File; |
| 78 | + /// |
| 79 | + /// let foo_file = File::open("foo.txt").await?; |
| 80 | + /// let bar_file = File::open("bar.txt").await?; |
| 81 | + /// |
| 82 | + /// let mut chain = foo_file.chain(bar_file); |
| 83 | + /// let (foo_file, bar_file) = chain.get_mut(); |
| 84 | + /// # |
| 85 | + /// # Ok(()) }) } |
| 86 | + /// ``` |
| 87 | + pub fn get_mut(&mut self) -> (&mut T, &mut U) { |
| 88 | + (&mut self.first, &mut self.second) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +impl<T: fmt::Debug, U: fmt::Debug> fmt::Debug for Chain<T, U> { |
| 93 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 94 | + f.debug_struct("Chain") |
| 95 | + .field("t", &self.first) |
| 96 | + .field("u", &self.second) |
| 97 | + .finish() |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +impl<T: Read + Unpin, U: Read + Unpin> Read for Chain<T, U> { |
| 102 | + fn poll_read( |
| 103 | + mut self: Pin<&mut Self>, |
| 104 | + cx: &mut Context<'_>, |
| 105 | + buf: &mut [u8], |
| 106 | + ) -> Poll<io::Result<usize>> { |
| 107 | + if !self.done_first { |
| 108 | + let rd = Pin::new(&mut self.first); |
| 109 | + |
| 110 | + match futures_core::ready!(rd.poll_read(cx, buf)) { |
| 111 | + Ok(0) if !buf.is_empty() => self.done_first = true, |
| 112 | + Ok(n) => return Poll::Ready(Ok(n)), |
| 113 | + Err(err) => return Poll::Ready(Err(err)), |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + let rd = Pin::new(&mut self.second); |
| 118 | + rd.poll_read(cx, buf) |
| 119 | + } |
| 120 | + |
| 121 | + fn poll_read_vectored( |
| 122 | + mut self: Pin<&mut Self>, |
| 123 | + cx: &mut Context<'_>, |
| 124 | + bufs: &mut [IoSliceMut<'_>], |
| 125 | + ) -> Poll<io::Result<usize>> { |
| 126 | + if !self.done_first { |
| 127 | + let rd = Pin::new(&mut self.first); |
| 128 | + |
| 129 | + match futures_core::ready!(rd.poll_read_vectored(cx, bufs)) { |
| 130 | + Ok(0) if !bufs.is_empty() => self.done_first = true, |
| 131 | + Ok(n) => return Poll::Ready(Ok(n)), |
| 132 | + Err(err) => return Poll::Ready(Err(err)), |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + let rd = Pin::new(&mut self.second); |
| 137 | + rd.poll_read_vectored(cx, bufs) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +impl<T: BufRead + Unpin, U: BufRead + Unpin> BufRead for Chain<T, U> { |
| 142 | + fn poll_fill_buf(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<&[u8]>> { |
| 143 | + let Self { |
| 144 | + first, |
| 145 | + second, |
| 146 | + done_first, |
| 147 | + } = unsafe { self.get_unchecked_mut() }; |
| 148 | + |
| 149 | + if !*done_first { |
| 150 | + let first = unsafe { Pin::new_unchecked(first) }; |
| 151 | + match futures_core::ready!(first.poll_fill_buf(cx)) { |
| 152 | + Ok(buf) if buf.is_empty() => { |
| 153 | + *done_first = true; |
| 154 | + } |
| 155 | + Ok(buf) => return Poll::Ready(Ok(buf)), |
| 156 | + Err(err) => return Poll::Ready(Err(err)), |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + let second = unsafe { Pin::new_unchecked(second) }; |
| 161 | + second.poll_fill_buf(cx) |
| 162 | + } |
| 163 | + |
| 164 | + fn consume(mut self: Pin<&mut Self>, amt: usize) { |
| 165 | + if !self.done_first { |
| 166 | + let rd = Pin::new(&mut self.first); |
| 167 | + rd.consume(amt) |
| 168 | + } else { |
| 169 | + let rd = Pin::new(&mut self.second); |
| 170 | + rd.consume(amt) |
| 171 | + } |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +#[cfg(test)] |
| 176 | +mod tests { |
| 177 | + use crate::io; |
| 178 | + use crate::prelude::*; |
| 179 | + use crate::task; |
| 180 | + |
| 181 | + #[test] |
| 182 | + fn test_chain_basics() -> std::io::Result<()> { |
| 183 | + let source1: io::Cursor<Vec<u8>> = io::Cursor::new(vec![0, 1, 2]); |
| 184 | + let source2: io::Cursor<Vec<u8>> = io::Cursor::new(vec![3, 4, 5]); |
| 185 | + |
| 186 | + task::block_on(async move { |
| 187 | + let mut buffer = Vec::new(); |
| 188 | + |
| 189 | + let mut source = source1.chain(source2); |
| 190 | + |
| 191 | + assert_eq!(6, source.read_to_end(&mut buffer).await?); |
| 192 | + assert_eq!(buffer, vec![0, 1, 2, 3, 4, 5]); |
| 193 | + |
| 194 | + Ok(()) |
| 195 | + }) |
| 196 | + } |
| 197 | +} |
0 commit comments