1use crate::codec::encoder::Encoder;
2use crate::codec::framed_impl::{FramedImpl, WriteFrame};
34use futures_core::Stream;
5use tokio::io::AsyncWrite;
67use bytes::BytesMut;
8use futures_sink::Sink;
9use pin_project_lite::pin_project;
10use std::fmt;
11use std::io;
12use std::pin::Pin;
13use std::task::{Context, Poll};
1415pin_project! {
16/// A [`Sink`] of frames encoded to an `AsyncWrite`.
17 ///
18 /// For examples of how to use `FramedWrite` with a codec, see the
19 /// examples on the [`codec`] module.
20 ///
21 /// # Cancellation safety
22 ///
23 /// * [`futures_util::sink::SinkExt::send`]: if send is used as the event in a
24 /// `tokio::select!` statement and some other branch completes first, then it is
25 /// guaranteed that the message was not sent, but the message itself is lost.
26 ///
27 /// [`Sink`]: futures_sink::Sink
28 /// [`codec`]: crate::codec
29 /// [`futures_util::sink::SinkExt::send`]: futures_util::sink::SinkExt::send
30pub struct FramedWrite<T, E> {
31#[pin]
32inner: FramedImpl<T, E, WriteFrame>,
33 }
34}
3536impl<T, E> FramedWrite<T, E>
37where
38T: AsyncWrite,
39{
40/// Creates a new `FramedWrite` with the given `encoder`.
41pub fn new(inner: T, encoder: E) -> FramedWrite<T, E> {
42 FramedWrite {
43 inner: FramedImpl {
44 inner,
45 codec: encoder,
46 state: WriteFrame::default(),
47 },
48 }
49 }
50}
5152impl<T, E> FramedWrite<T, E> {
53/// Returns a reference to the underlying I/O stream wrapped by
54 /// `FramedWrite`.
55 ///
56 /// Note that care should be taken to not tamper with the underlying stream
57 /// of data coming in as it may corrupt the stream of frames otherwise
58 /// being worked with.
59pub fn get_ref(&self) -> &T {
60&self.inner.inner
61 }
6263/// Returns a mutable reference to the underlying I/O stream wrapped by
64 /// `FramedWrite`.
65 ///
66 /// Note that care should be taken to not tamper with the underlying stream
67 /// of data coming in as it may corrupt the stream of frames otherwise
68 /// being worked with.
69pub fn get_mut(&mut self) -> &mut T {
70&mut self.inner.inner
71 }
7273/// Returns a pinned mutable reference to the underlying I/O stream wrapped by
74 /// `FramedWrite`.
75 ///
76 /// Note that care should be taken to not tamper with the underlying stream
77 /// of data coming in as it may corrupt the stream of frames otherwise
78 /// being worked with.
79pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> {
80self.project().inner.project().inner
81 }
8283/// Consumes the `FramedWrite`, returning its underlying I/O stream.
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 into_inner(self) -> T {
89self.inner.inner
90 }
9192/// Returns a reference to the underlying encoder.
93pub fn encoder(&self) -> &E {
94&self.inner.codec
95 }
9697/// Returns a mutable reference to the underlying encoder.
98pub fn encoder_mut(&mut self) -> &mut E {
99&mut self.inner.codec
100 }
101102/// Maps the encoder `E` to `C`, preserving the write buffer
103 /// wrapped by `Framed`.
104pub fn map_encoder<C, F>(self, map: F) -> FramedWrite<T, C>
105where
106F: FnOnce(E) -> C,
107 {
108// This could be potentially simplified once rust-lang/rust#86555 hits stable
109let FramedImpl {
110 inner,
111 state,
112 codec,
113 } = self.inner;
114 FramedWrite {
115 inner: FramedImpl {
116 inner,
117 state,
118 codec: map(codec),
119 },
120 }
121 }
122123/// Returns a mutable reference to the underlying encoder.
124pub fn encoder_pin_mut(self: Pin<&mut Self>) -> &mut E {
125self.project().inner.project().codec
126 }
127128/// Returns a reference to the write buffer.
129pub fn write_buffer(&self) -> &BytesMut {
130&self.inner.state.buffer
131 }
132133/// Returns a mutable reference to the write buffer.
134pub fn write_buffer_mut(&mut self) -> &mut BytesMut {
135&mut self.inner.state.buffer
136 }
137138/// Returns backpressure boundary
139pub fn backpressure_boundary(&self) -> usize {
140self.inner.state.backpressure_boundary
141 }
142143/// Updates backpressure boundary
144pub fn set_backpressure_boundary(&mut self, boundary: usize) {
145self.inner.state.backpressure_boundary = boundary;
146 }
147}
148149// This impl just defers to the underlying FramedImpl
150impl<T, I, E> Sink<I> for FramedWrite<T, E>
151where
152T: AsyncWrite,
153 E: Encoder<I>,
154 E::Error: From<io::Error>,
155{
156type Error = E::Error;
157158fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
159self.project().inner.poll_ready(cx)
160 }
161162fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error> {
163self.project().inner.start_send(item)
164 }
165166fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
167self.project().inner.poll_flush(cx)
168 }
169170fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
171self.project().inner.poll_close(cx)
172 }
173}
174175// This impl just defers to the underlying T: Stream
176impl<T, D> Stream for FramedWrite<T, D>
177where
178T: Stream,
179{
180type Item = T::Item;
181182fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
183self.project().inner.project().inner.poll_next(cx)
184 }
185}
186187impl<T, U> fmt::Debug for FramedWrite<T, U>
188where
189T: fmt::Debug,
190 U: fmt::Debug,
191{
192fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
193 f.debug_struct("FramedWrite")
194 .field("inner", &self.get_ref())
195 .field("encoder", &self.encoder())
196 .field("buffer", &self.inner.state.buffer)
197 .finish()
198 }
199}