@@ -13,6 +13,7 @@ use tokio::io::{AsyncBufRead, AsyncRead, ReadBuf};
13
13
enum State {
14
14
Encoding ,
15
15
Flushing ,
16
+ Finishing ,
16
17
Done ,
17
18
}
18
19
@@ -57,27 +58,80 @@ impl<R: AsyncBufRead, E: Encode> Encoder<R, E> {
57
58
output : & mut PartialBuffer < & mut [ u8 ] > ,
58
59
) -> Poll < Result < ( ) > > {
59
60
let mut this = self . project ( ) ;
61
+ let mut read = 0usize ;
60
62
61
63
loop {
64
+ println ! ( "encoder state: {:?}" , this. state) ;
62
65
* this. state = match this. state {
63
66
State :: Encoding => {
64
- let input = ready ! ( this. reader. as_mut( ) . poll_fill_buf( cx) ) ?;
65
- if input. is_empty ( ) {
66
- State :: Flushing
67
- } else {
68
- let mut input = PartialBuffer :: new ( input) ;
69
- this. encoder . encode ( & mut input, output) ?;
70
- let len = input. written ( ) . len ( ) ;
71
- this. reader . as_mut ( ) . consume ( len) ;
72
- State :: Encoding
67
+ let res = this. reader . as_mut ( ) . poll_fill_buf ( cx) ;
68
+
69
+ match res {
70
+ Poll :: Pending => {
71
+ println ! (
72
+ "[{}] encoder got pending, read={}" ,
73
+ std:: time:: SystemTime :: now( )
74
+ . duration_since( std:: time:: UNIX_EPOCH )
75
+ . unwrap( )
76
+ . as_secs( ) ,
77
+ read
78
+ ) ;
79
+ if read == 0 {
80
+ return Poll :: Pending ;
81
+ } else {
82
+ println ! (
83
+ "will flush, output unwritten = {}" ,
84
+ output. unwritten( ) . len( )
85
+ ) ;
86
+
87
+ State :: Flushing
88
+ }
89
+ }
90
+ Poll :: Ready ( res) => {
91
+ println ! (
92
+ "[{}]encoder: res_err={:?}" ,
93
+ std:: time:: SystemTime :: now( )
94
+ . duration_since( std:: time:: UNIX_EPOCH )
95
+ . unwrap( )
96
+ . as_secs( ) ,
97
+ match & res {
98
+ Ok ( _) => String :: new( ) ,
99
+ Err ( e) => e. clone( ) . to_string( ) ,
100
+ }
101
+ ) ;
102
+ let input = res?;
103
+
104
+ if input. is_empty ( ) {
105
+ State :: Finishing
106
+ } else {
107
+ println ! (
108
+ "encoder got input: {}" ,
109
+ std:: str :: from_utf8( input) . unwrap( )
110
+ ) ;
111
+ let mut input = PartialBuffer :: new ( input) ;
112
+ this. encoder . encode ( & mut input, output) ?;
113
+ let len = input. written ( ) . len ( ) ;
114
+ this. reader . as_mut ( ) . consume ( len) ;
115
+ read += len;
116
+ State :: Encoding
117
+ }
118
+ }
73
119
}
74
120
}
75
121
76
122
State :: Flushing => {
123
+ if this. encoder . flush ( output) ? {
124
+ State :: Encoding
125
+ } else {
126
+ State :: Flushing
127
+ }
128
+ }
129
+
130
+ State :: Finishing => {
77
131
if this. encoder . finish ( output) ? {
78
132
State :: Done
79
133
} else {
80
- State :: Flushing
134
+ State :: Finishing
81
135
}
82
136
}
83
137
@@ -87,7 +141,14 @@ impl<R: AsyncBufRead, E: Encode> Encoder<R, E> {
87
141
if let State :: Done = * this. state {
88
142
return Poll :: Ready ( Ok ( ( ) ) ) ;
89
143
}
144
+
145
+ println ! (
146
+ "should send chunk: output.unwritten().len()={}, output.written().len()={}" ,
147
+ output. unwritten( ) . len( ) ,
148
+ output. written( ) . len( )
149
+ ) ;
90
150
if output. unwritten ( ) . is_empty ( ) {
151
+ println ! ( "returning chunk" ) ;
91
152
return Poll :: Ready ( Ok ( ( ) ) ) ;
92
153
}
93
154
}
0 commit comments