use super::fuse::Fuse; use super::Decoder; use bytes::BytesMut; use futures::io::AsyncRead; use futures::{ready, Sink, Stream, TryStreamExt}; use pin_project::pin_project; use std::io; use std::marker::Unpin; use std::ops::{Deref, DerefMut}; use std::pin::Pin; use std::task::{Context, Poll}; /// A `Stream` of messages decoded from an `AsyncRead`. /// /// # Example /// ``` /// use futures_codec::{BytesCodec, FramedRead}; /// use futures::TryStreamExt; /// use bytes::{Bytes}; /// /// let buf = [3u8; 3]; /// let mut framed = FramedRead::new(&buf[..], BytesCodec); /// /// # futures::executor::block_on(async move { /// if let Some(bytes) = framed.try_next().await? { /// assert_eq!(bytes, Bytes::copy_from_slice(&buf[..])); /// } /// # Ok::<_, std::io::Error>(()) /// # }).unwrap(); /// ``` #[derive(Debug)] pub struct FramedRead { inner: FramedRead2>, } impl Deref for FramedRead { type Target = T; fn deref(&self) -> &T { &self.inner } } impl DerefMut for FramedRead { fn deref_mut(&mut self) -> &mut T { &mut self.inner } } impl FramedRead where T: AsyncRead, D: Decoder, { /// Creates a new `FramedRead` transport with the given `Decoder`. pub fn new(inner: T, decoder: D) -> Self { Self { inner: framed_read_2(Fuse::new(inner, decoder)), } } /// Release the I/O and Decoder pub fn release(self: Self) -> (T, D) { let fuse = self.inner.release(); (fuse.t, fuse.u) } /// Consumes the `FramedRead`, returning its underlying I/O stream. /// /// Note that care should be taken to not tamper with the underlying stream /// of data coming in as it may corrupt the stream of frames otherwise /// being worked with. pub fn into_inner(self) -> T { self.release().0 } /// Returns a reference to the underlying decoder. /// /// Note that care should be taken to not tamper with the underlying decoder /// as it may corrupt the stream of frames otherwise being worked with. pub fn decoder(&self) -> &D { &self.inner.u } /// Returns a mutable reference to the underlying decoder. /// /// Note that care should be taken to not tamper with the underlying decoder /// as it may corrupt the stream of frames otherwise being worked with. pub fn decoder_mut(&mut self) -> &mut D { &mut self.inner.u } /// Returns a reference to the read buffer. pub fn read_buffer(&self) -> &BytesMut { &self.inner.buffer } } impl Stream for FramedRead where T: AsyncRead + Unpin, D: Decoder, { type Item = Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { self.inner.try_poll_next_unpin(cx) } } #[pin_project] #[derive(Debug)] pub struct FramedRead2 { #[pin] inner: T, buffer: BytesMut, } impl Deref for FramedRead2 { type Target = T; fn deref(&self) -> &T { &self.inner } } impl DerefMut for FramedRead2 { fn deref_mut(&mut self) -> &mut T { &mut self.inner } } const INITIAL_CAPACITY: usize = 8 * 1024; pub fn framed_read_2(inner: T) -> FramedRead2 { FramedRead2 { inner, buffer: BytesMut::with_capacity(INITIAL_CAPACITY), } } impl Stream for FramedRead2 where T: AsyncRead + Decoder + Unpin, { type Item = Result; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll> { let this = &mut *self; if let Some(item) = this.inner.decode(&mut this.buffer)? { return Poll::Ready(Some(Ok(item))); } let mut buf = [0u8; INITIAL_CAPACITY]; loop { let n = ready!(Pin::new(&mut this.inner).poll_read(cx, &mut buf))?; this.buffer.extend_from_slice(&buf[..n]); let ended = n == 0; match this.inner.decode(&mut this.buffer)? { Some(item) => return Poll::Ready(Some(Ok(item))), None if ended => { if this.buffer.is_empty() { return Poll::Ready(None); } else { match this.inner.decode_eof(&mut this.buffer)? { Some(item) => return Poll::Ready(Some(Ok(item))), None if this.buffer.is_empty() => return Poll::Ready(None), None => { return Poll::Ready(Some(Err(io::Error::new( io::ErrorKind::UnexpectedEof, "bytes remaining in stream", ) .into()))); } } } } _ => continue, } } } } impl Sink for FramedRead2 where T: Sink + Unpin, { type Error = T::Error; fn poll_ready(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { self.project().inner.poll_ready(cx) } fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error> { self.project().inner.start_send(item) } fn poll_flush(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { self.project().inner.poll_flush(cx) } fn poll_close(self: Pin<&mut Self>, cx: &mut Context) -> Poll> { self.project().inner.poll_close(cx) } } impl FramedRead2 { pub fn release(self: Self) -> T { self.inner } pub fn buffer(&self) -> &BytesMut { &self.buffer } }