Files
tokio/tokio-util/src/codec/framed_read.rs
T

163 lines
4.6 KiB
Rust
Raw Normal View History

2020-05-12 12:47:38 +01:00
use crate::codec::framed_impl::{FramedImpl, ReadFrame};
2019-10-22 10:13:49 -07:00
use crate::codec::Decoder;
2020-12-15 23:24:38 -05:00
use tokio::io::AsyncRead;
use tokio_stream::Stream;
2019-06-27 18:10:29 +01:00
use bytes::BytesMut;
use futures_sink::Sink;
use pin_project_lite::pin_project;
use std::fmt;
use std::pin::Pin;
use std::task::{Context, Poll};
2019-06-27 18:10:29 +01:00
pin_project! {
/// A [`Stream`] of messages decoded from an [`AsyncRead`].
///
2020-12-15 23:24:38 -05:00
/// [`Stream`]: tokio_stream::Stream
/// [`AsyncRead`]: tokio::io::AsyncRead
pub struct FramedRead<T, D> {
#[pin]
2020-05-12 12:47:38 +01:00
inner: FramedImpl<T, D, ReadFrame>,
}
2019-06-27 18:10:29 +01:00
}
// ===== impl FramedRead =====
impl<T, D> FramedRead<T, D>
where
T: AsyncRead,
D: Decoder,
{
/// Creates a new `FramedRead` with the given `decoder`.
pub fn new(inner: T, decoder: D) -> FramedRead<T, D> {
FramedRead {
2020-05-12 12:47:38 +01:00
inner: FramedImpl {
inner,
codec: decoder,
2020-05-12 12:47:38 +01:00
state: Default::default(),
},
2019-06-27 18:10:29 +01:00
}
}
2020-03-02 17:40:54 +01:00
/// Creates a new `FramedRead` with the given `decoder` and a buffer of `capacity`
/// initial size.
pub fn with_capacity(inner: T, decoder: D, capacity: usize) -> FramedRead<T, D> {
FramedRead {
2020-05-12 12:47:38 +01:00
inner: FramedImpl {
inner,
codec: decoder,
state: ReadFrame {
eof: false,
is_readable: false,
buffer: BytesMut::with_capacity(capacity),
2020-03-02 17:40:54 +01:00
},
2020-05-12 12:47:38 +01:00
},
2020-03-02 17:40:54 +01:00
}
}
2019-06-27 18:10:29 +01:00
}
impl<T, D> FramedRead<T, D> {
/// Returns a reference to the underlying I/O stream wrapped by
/// `FramedRead`.
///
/// 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 get_ref(&self) -> &T {
2020-05-12 12:47:38 +01:00
&self.inner.inner
2019-06-27 18:10:29 +01:00
}
/// Returns a mutable reference to the underlying I/O stream wrapped by
/// `FramedRead`.
///
/// 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 get_mut(&mut self) -> &mut T {
2020-05-12 12:47:38 +01:00
&mut self.inner.inner
2019-06-27 18:10:29 +01:00
}
/// 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 {
2020-05-12 12:47:38 +01:00
self.inner.inner
2019-06-27 18:10:29 +01:00
}
/// Returns a reference to the underlying decoder.
pub fn decoder(&self) -> &D {
2020-05-12 12:47:38 +01:00
&self.inner.codec
2019-06-27 18:10:29 +01:00
}
/// Returns a mutable reference to the underlying decoder.
pub fn decoder_mut(&mut self) -> &mut D {
2020-05-12 12:47:38 +01:00
&mut self.inner.codec
2019-06-27 18:10:29 +01:00
}
/// Returns a reference to the read buffer.
pub fn read_buffer(&self) -> &BytesMut {
2020-05-12 12:47:38 +01:00
&self.inner.state.buffer
}
/// Returns a mutable reference to the read buffer.
pub fn read_buffer_mut(&mut self) -> &mut BytesMut {
&mut self.inner.state.buffer
}
2019-06-27 18:10:29 +01:00
}
2020-05-12 12:47:38 +01:00
// This impl just defers to the underlying FramedImpl
2019-06-27 18:10:29 +01:00
impl<T, D> Stream for FramedRead<T, D>
where
T: AsyncRead,
D: Decoder,
2019-06-27 18:10:29 +01:00
{
type Item = Result<D::Item, D::Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
self.project().inner.poll_next(cx)
2019-06-27 18:10:29 +01:00
}
}
// This impl just defers to the underlying T: Sink
impl<T, I, D> Sink<I> for FramedRead<T, D>
where
T: Sink<I>,
2019-06-27 18:10:29 +01:00
{
type Error = T::Error;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2020-05-12 12:47:38 +01:00
self.project().inner.project().inner.poll_ready(cx)
2019-06-27 18:10:29 +01:00
}
fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error> {
2020-05-12 12:47:38 +01:00
self.project().inner.project().inner.start_send(item)
2019-06-27 18:10:29 +01:00
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2020-05-12 12:47:38 +01:00
self.project().inner.project().inner.poll_flush(cx)
2019-06-27 18:10:29 +01:00
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
2020-05-12 12:47:38 +01:00
self.project().inner.project().inner.poll_close(cx)
2019-06-27 18:10:29 +01:00
}
}
impl<T, D> fmt::Debug for FramedRead<T, D>
where
T: fmt::Debug,
D: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FramedRead")
2020-05-12 12:47:38 +01:00
.field("inner", &self.get_ref())
.field("decoder", &self.decoder())
.field("eof", &self.inner.state.eof)
.field("is_readable", &self.inner.state.is_readable)
.field("buffer", &self.read_buffer())
2019-06-27 18:10:29 +01:00
.finish()
}
}