@@ -16,6 +16,7 @@ use tokio::io::{AsyncBufRead, AsyncRead, AsyncWrite, ReadBuf};
16
16
#[ derive( Debug ) ]
17
17
enum State {
18
18
Encoding ,
19
+ Flushing ,
19
20
Finishing ,
20
21
Done ,
21
22
}
@@ -80,6 +81,12 @@ impl<W: AsyncWrite, E: Encode> Encoder<W, E> {
80
81
State :: Encoding
81
82
}
82
83
84
+ // Once a flush has been started, it must be completed.
85
+ State :: Flushing => match this. encoder . flush ( & mut output) ? {
86
+ true => State :: Encoding ,
87
+ false => State :: Flushing ,
88
+ } ,
89
+
83
90
State :: Finishing | State :: Done => {
84
91
return Poll :: Ready ( Err ( io:: Error :: new (
85
92
io:: ErrorKind :: Other ,
@@ -105,7 +112,7 @@ impl<W: AsyncWrite, E: Encode> Encoder<W, E> {
105
112
let mut output = PartialBuffer :: new ( output) ;
106
113
107
114
let done = match this. state {
108
- State :: Encoding => this. encoder . flush ( & mut output) ?,
115
+ State :: Encoding | State :: Flushing => this. encoder . flush ( & mut output) ?,
109
116
110
117
State :: Finishing | State :: Done => {
111
118
return Poll :: Ready ( Err ( io:: Error :: new (
@@ -114,11 +121,13 @@ impl<W: AsyncWrite, E: Encode> Encoder<W, E> {
114
121
) ) )
115
122
}
116
123
} ;
124
+ * this. state = State :: Flushing ;
117
125
118
126
let produced = output. written ( ) . len ( ) ;
119
127
this. writer . as_mut ( ) . produce ( produced) ;
120
128
121
129
if done {
130
+ * this. state = State :: Encoding ;
122
131
return Poll :: Ready ( Ok ( ( ) ) ) ;
123
132
}
124
133
}
@@ -140,6 +149,12 @@ impl<W: AsyncWrite, E: Encode> Encoder<W, E> {
140
149
}
141
150
}
142
151
152
+ // Once a flush has been started, it must be completed.
153
+ State :: Flushing => match this. encoder . flush ( & mut output) ? {
154
+ true => State :: Finishing ,
155
+ false => State :: Flushing ,
156
+ } ,
157
+
143
158
State :: Done => State :: Done ,
144
159
} ;
145
160
0 commit comments