tokio_util/codec/
framed_write.rs1use crate::codec::encoder::Encoder;
2use crate::codec::framed_impl::{FramedImpl, WriteFrame};
3
4use futures_core::Stream;
5use tokio::io::AsyncWrite;
6
7use 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};
14
15pin_project! {
16 pub struct FramedWrite<T, E> {
31 #[pin]
32 inner: FramedImpl<T, E, WriteFrame>,
33 }
34}
35
36impl<T, E> FramedWrite<T, E>
37where
38 T: AsyncWrite,
39{
40 pub 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}
51
52impl<T, E> FramedWrite<T, E> {
53 pub fn get_ref(&self) -> &T {
60 &self.inner.inner
61 }
62
63 pub fn get_mut(&mut self) -> &mut T {
70 &mut self.inner.inner
71 }
72
73 pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut T> {
80 self.project().inner.project().inner
81 }
82
83 pub fn into_inner(self) -> T {
89 self.inner.inner
90 }
91
92 pub fn encoder(&self) -> &E {
94 &self.inner.codec
95 }
96
97 pub fn encoder_mut(&mut self) -> &mut E {
99 &mut self.inner.codec
100 }
101
102 pub fn map_encoder<C, F>(self, map: F) -> FramedWrite<T, C>
105 where
106 F: FnOnce(E) -> C,
107 {
108 let 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 }
122
123 pub fn encoder_pin_mut(self: Pin<&mut Self>) -> &mut E {
125 self.project().inner.project().codec
126 }
127
128 pub fn write_buffer(&self) -> &BytesMut {
130 &self.inner.state.buffer
131 }
132
133 pub fn write_buffer_mut(&mut self) -> &mut BytesMut {
135 &mut self.inner.state.buffer
136 }
137
138 pub fn backpressure_boundary(&self) -> usize {
140 self.inner.state.backpressure_boundary
141 }
142
143 pub fn set_backpressure_boundary(&mut self, boundary: usize) {
145 self.inner.state.backpressure_boundary = boundary;
146 }
147}
148
149impl<T, I, E> Sink<I> for FramedWrite<T, E>
151where
152 T: AsyncWrite,
153 E: Encoder<I>,
154 E::Error: From<io::Error>,
155{
156 type Error = E::Error;
157
158 fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
159 self.project().inner.poll_ready(cx)
160 }
161
162 fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error> {
163 self.project().inner.start_send(item)
164 }
165
166 fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
167 self.project().inner.poll_flush(cx)
168 }
169
170 fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
171 self.project().inner.poll_close(cx)
172 }
173}
174
175impl<T, D> Stream for FramedWrite<T, D>
177where
178 T: Stream,
179{
180 type Item = T::Item;
181
182 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
183 self.project().inner.project().inner.poll_next(cx)
184 }
185}
186
187impl<T, U> fmt::Debug for FramedWrite<T, U>
188where
189 T: fmt::Debug,
190 U: fmt::Debug,
191{
192 fn 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}