use futures_core::Stream; use tokio::io::AsyncWrite;
use bytes::BytesMut; use futures_sink::Sink; use pin_project_lite::pin_project; use std::fmt; use std::io; use std::pin::Pin; use std::task::{Context, Poll};
pin_project! { /// A [`Sink`] of frames encoded to an `AsyncWrite`. /// /// [`Sink`]: futures_sink::Sink pubstruct FramedWrite<T, E> { #[pin]
inner: FramedImpl<T, E, WriteFrame>,
}
}
impl<T, E> FramedWrite<T, E> where
T: AsyncWrite,
{ /// Creates a new `FramedWrite` with the given `encoder`. pubfn new(inner: T, encoder: E) -> FramedWrite<T, E> {
FramedWrite {
inner: FramedImpl {
inner,
codec: encoder,
state: WriteFrame::default(),
},
}
}
}
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. pubfn get_ref(&self) -> &T {
&self.inner.inner
}
/// 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. pubfn get_mut(&mutself) -> &mut T {
&mutself.inner.inner
}
/// Returns a pinned 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. pubfn get_pin_mut(self: Pin<&mutSelf>) -> Pin<&mut T> { self.project().inner.project().inner
}
/// 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. pubfn into_inner(self) -> T { self.inner.inner
}
/// Returns a reference to the underlying encoder. pubfn encoder(&self) -> &E {
&self.inner.codec
}
/// Returns a mutable reference to the underlying encoder. pubfn encoder_mut(&mutself) -> &mut E {
&mutself.inner.codec
}
/// Maps the encoder `E` to `C`, preserving the write buffer /// wrapped by `Framed`. pubfn map_encoder<C, F>(self, map: F) -> FramedWrite<T, C> where
F: FnOnce(E) -> C,
{ // This could be potentially simplified once rust-lang/rust#86555 hits stable let FramedImpl {
inner,
state,
codec,
} = self.inner;
FramedWrite {
inner: FramedImpl {
inner,
state,
codec: map(codec),
},
}
}
/// Returns a mutable reference to the underlying encoder. pubfn encoder_pin_mut(self: Pin<&mutSelf>) -> &mut E { self.project().inner.project().codec
}
/// Returns a reference to the write buffer. pubfn write_buffer(&self) -> &BytesMut {
&self.inner.state.buffer
}
/// Returns a mutable reference to the write buffer. pubfn write_buffer_mut(&mutself) -> &mutBytesMut {
&mutself.inner.state.buffer
}
}
// This impl just defers to the underlying FramedImpl impl<T, I, E> Sink<I> for FramedWrite<T, E> where
T: AsyncWrite,
E: Encoder<I>,
E::Error: From<io::Error>,
{ type Error = E::Error;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.