1use crate::codec::framed_impl::{FramedImpl, ReadFrame};
2use crate::codec::Decoder;
34use futures_core::Stream;
5use tokio::io::AsyncRead;
67use bytes::BytesMut;
8use futures_sink::Sink;
9use pin_project_lite::pin_project;
10use std::fmt;
11use std::pin::Pin;
12use std::task::{Context, Poll};
1314pin_project! {
15/// A [`Stream`] of messages decoded from an [`AsyncRead`].
16 ///
17 /// For examples of how to use `FramedRead` with a codec, see the
18 /// examples on the [`codec`] module.
19 ///
20 /// # Cancellation safety
21 /// * [`tokio_stream::StreamExt::next`]: This method is cancel safe. The returned
22 /// future only holds onto a reference to the underlying stream, so dropping it will
23 /// never lose a value.
24 ///
25 /// [`Stream`]: futures_core::Stream
26 /// [`AsyncRead`]: tokio::io::AsyncRead
27 /// [`codec`]: crate::codec
28 /// [`tokio_stream::StreamExt::next`]: https://docs.rs/tokio-stream/latest/tokio_stream/trait.StreamExt.html#method.next
29pub struct FramedRead<T, D> {
30#[pin]
31inner: FramedImpl<T, D, ReadFrame>,
32 }
33}
3435// ===== impl FramedRead =====
3637impl<T, D> FramedRead<T, D>
38where
39T: AsyncRead,
40 D: Decoder,
41{
42/// Creates a new `FramedRead` with the given `decoder`.
43pub fn new(inner: T, decoder: D) -> FramedRead<T, D> {
44 FramedRead {
45 inner: FramedImpl {
46 inner,
47 codec: decoder,
48 state: Default::default(),
49 },
50 }
51 }
5253/// Creates a new `FramedRead` with the given `decoder` and a buffer of `capacity`
54 /// initial size.
55pub fn with_capacity(inner: T, decoder: D, capacity: usize) -> FramedRead<T, D> {
56 FramedRead {
57 inner: FramedImpl {
58 inner,
59 codec: decoder,
60 state: ReadFrame {
61 eof: false,
62 is_readable: false,
63 buffer: BytesMut::with_capacity(capacity),
64 has_errored: false,
65 },
66 },
67 }
68 }
69}
7071impl<T, D> FramedRead<T, D> {
72/// Returns a reference to the underlying I/O stream wrapped by
73 /// `FramedRead`.
74 ///
75 /// Note that care should be taken to not tamper with the underlying stream
76 /// of data coming in as it may corrupt the stream of frames otherwise
77 /// being worked with.
78pub fn get_ref(&self) -> &T {
79&self.inner.inner
80 }
8182/// Returns a mutable reference to the underlying I/O stream wrapped by
83 /// `FramedRead`.
84 ///
85 /// Note that care should be taken to not tamper with the underlying stream
86 /// of data coming in as it may corrupt the stream of frames otherwise
87 /// being worked with.
88pub fn get_mut(&mut self) -> &mut T {
89&mut self.inner.inner
90 }
9192/// Returns a pinned mutable reference to the underlying I/O stream wrapped by
93 /// `FramedRead`.
94 ///
95 /// Note that care should be taken to not tamper with the underlying stream
96 /// of data coming in as it may corrupt the stream of frames otherwise
97 /// being worked with.
98pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> {
99self.project().inner.project().inner
100 }
101102/// Consumes the `FramedRead`, returning its underlying I/O stream.
103 ///
104 /// Note that care should be taken to not tamper with the underlying stream
105 /// of data coming in as it may corrupt the stream of frames otherwise
106 /// being worked with.
107pub fn into_inner(self) -> T {
108self.inner.inner
109 }
110111/// Returns a reference to the underlying decoder.
112pub fn decoder(&self) -> &D {
113&self.inner.codec
114 }
115116/// Returns a mutable reference to the underlying decoder.
117pub fn decoder_mut(&mut self) -> &mut D {
118&mut self.inner.codec
119 }
120121/// Maps the decoder `D` to `C`, preserving the read buffer
122 /// wrapped by `Framed`.
123pub fn map_decoder<C, F>(self, map: F) -> FramedRead<T, C>
124where
125F: FnOnce(D) -> C,
126 {
127// This could be potentially simplified once rust-lang/rust#86555 hits stable
128let FramedImpl {
129 inner,
130 state,
131 codec,
132 } = self.inner;
133 FramedRead {
134 inner: FramedImpl {
135 inner,
136 state,
137 codec: map(codec),
138 },
139 }
140 }
141142/// Returns a mutable reference to the underlying decoder.
143pub fn decoder_pin_mut(self: Pin<&mut Self>) -> &mut D {
144self.project().inner.project().codec
145 }
146147/// Returns a reference to the read buffer.
148pub fn read_buffer(&self) -> &BytesMut {
149&self.inner.state.buffer
150 }
151152/// Returns a mutable reference to the read buffer.
153pub fn read_buffer_mut(&mut self) -> &mut BytesMut {
154&mut self.inner.state.buffer
155 }
156}
157158// This impl just defers to the underlying FramedImpl
159impl<T, D> Stream for FramedRead<T, D>
160where
161T: AsyncRead,
162 D: Decoder,
163{
164type Item = Result<D::Item, D::Error>;
165166fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
167self.project().inner.poll_next(cx)
168 }
169}
170171// This impl just defers to the underlying T: Sink
172impl<T, I, D> Sink<I> for FramedRead<T, D>
173where
174T: Sink<I>,
175{
176type Error = T::Error;
177178fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
179self.project().inner.project().inner.poll_ready(cx)
180 }
181182fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error> {
183self.project().inner.project().inner.start_send(item)
184 }
185186fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
187self.project().inner.project().inner.poll_flush(cx)
188 }
189190fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
191self.project().inner.project().inner.poll_close(cx)
192 }
193}
194195impl<T, D> fmt::Debug for FramedRead<T, D>
196where
197T: fmt::Debug,
198 D: fmt::Debug,
199{
200fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
201 f.debug_struct("FramedRead")
202 .field("inner", &self.get_ref())
203 .field("decoder", &self.decoder())
204 .field("eof", &self.inner.state.eof)
205 .field("is_readable", &self.inner.state.is_readable)
206 .field("buffer", &self.read_buffer())
207 .finish()
208 }
209}