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

131 lines
3.6 KiB
Rust
Raw Normal View History

2019-10-22 10:13:49 -07:00
use crate::codec::encoder::Encoder;
2020-05-12 12:47:38 +01:00
use crate::codec::framed_impl::{FramedImpl, WriteFrame};
2020-05-12 12:47:38 +01:00
use tokio::{io::AsyncWrite, stream::Stream};
2019-06-27 18:10:29 +01:00
use futures_sink::Sink;
use pin_project_lite::pin_project;
use std::fmt;
2020-05-12 12:47:38 +01:00
use std::io;
2019-06-27 18:10:29 +01:00
use std::pin::Pin;
use std::task::{Context, Poll};
pin_project! {
/// A [`Sink`] of frames encoded to an `AsyncWrite`.
///
/// [`Sink`]: futures_sink::Sink
pub struct FramedWrite<T, E> {
#[pin]
2020-05-12 12:47:38 +01:00
inner: FramedImpl<T, E, WriteFrame>,
}
2019-06-27 18:10:29 +01:00
}
impl<T, E> FramedWrite<T, E>
where
T: AsyncWrite,
{
/// Creates a new `FramedWrite` with the given `encoder`.
pub fn new(inner: T, encoder: E) -> FramedWrite<T, E> {
FramedWrite {
2020-05-12 12:47:38 +01:00
inner: FramedImpl {
inner,
codec: encoder,
2020-05-12 12:47:38 +01:00
state: WriteFrame::default(),
},
2019-06-27 18:10:29 +01:00
}
}
}
impl<T, E> FramedWrite<T, E> {
/// Returns a reference to the underlying I/O stream wrapped by
/// `FramedWrite`.
///
/// 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
/// `FramedWrite`.
///
/// 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 `FramedWrite`, 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
}
2020-05-12 12:47:38 +01:00
/// Returns a reference to the underlying encoder.
2019-06-27 18:10:29 +01:00
pub fn encoder(&self) -> &E {
2020-05-12 12:47:38 +01:00
&self.inner.codec
2019-06-27 18:10:29 +01:00
}
2020-05-12 12:47:38 +01:00
/// Returns a mutable reference to the underlying encoder.
2019-06-27 18:10:29 +01:00
pub fn encoder_mut(&mut self) -> &mut E {
2020-05-12 12:47:38 +01:00
&mut self.inner.codec
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, I, E> Sink<I> for FramedWrite<T, E>
where
T: AsyncWrite,
2020-03-04 15:54:41 -05:00
E: Encoder<I>,
2019-06-27 18:10:29 +01:00
E::Error: From<io::Error>,
{
type Error = E::Error;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.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> {
self.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>> {
self.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>> {
self.project().inner.poll_close(cx)
2019-06-27 18:10:29 +01:00
}
}
2020-05-12 12:47:38 +01:00
// This impl just defers to the underlying T: Stream
2019-06-27 18:10:29 +01:00
impl<T, D> Stream for FramedWrite<T, D>
where
T: Stream,
2019-06-27 18:10:29 +01:00
{
type Item = T::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
2020-05-12 12:47:38 +01:00
self.project().inner.project().inner.poll_next(cx)
2019-06-27 18:10:29 +01:00
}
}
impl<T, U> fmt::Debug for FramedWrite<T, U>
where
T: fmt::Debug,
U: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("FramedWrite")
2020-05-12 12:47:38 +01:00
.field("inner", &self.get_ref())
.field("encoder", &self.encoder())
.field("buffer", &self.inner.state.buffer)
2019-06-27 18:10:29 +01:00
.finish()
}
}