@@ -32,12 +32,15 @@ mod async_io {
32
32
task:: { Context , Poll } ,
33
33
} ;
34
34
35
- /// A way of writing packet lines asynchronously.
36
- pub struct LineWriter < ' a , ' b , W : ?Sized > {
37
- writer : & ' a mut W ,
38
- prefix : & ' b [ u8 ] ,
39
- suffix : & ' b [ u8 ] ,
40
- state : State < ' b > ,
35
+ pin_project_lite:: pin_project! {
36
+ /// A way of writing packet lines asynchronously.
37
+ pub struct LineWriter <' a, ' b, W : ?Sized > {
38
+ #[ pin]
39
+ writer: & ' a mut W ,
40
+ prefix: & ' b [ u8 ] ,
41
+ suffix: & ' b [ u8 ] ,
42
+ state: State <' b>,
43
+ }
41
44
}
42
45
enum State < ' a > {
43
46
Idle ,
@@ -71,8 +74,8 @@ mod async_io {
71
74
fn into_io_err ( err : Error ) -> io:: Error {
72
75
io:: Error :: new ( io:: ErrorKind :: Other , err)
73
76
}
74
- let mut this = & mut * self ;
75
77
loop {
78
+ let mut this = self . as_mut ( ) . project ( ) ;
76
79
match & mut this. state {
77
80
State :: Idle => {
78
81
let data_len = this. prefix . len ( ) + data. len ( ) + this. suffix . len ( ) ;
@@ -84,36 +87,36 @@ mod async_io {
84
87
}
85
88
let data_len = data_len + 4 ;
86
89
let len_buf = u16_to_hex ( data_len as u16 ) ;
87
- this. state = State :: WriteHexLen ( len_buf, 0 )
90
+ * this. state = State :: WriteHexLen ( len_buf, 0 )
88
91
}
89
92
State :: WriteHexLen ( hex_len, written) => {
90
93
while * written != hex_len. len ( ) {
91
- let n = ready ! ( Pin :: new ( & mut this. writer) . poll_write( cx, & hex_len[ * written..] ) ) ?;
94
+ let n = ready ! ( this. writer. as_mut ( ) . poll_write( cx, & hex_len[ * written..] ) ) ?;
92
95
if n == 0 {
93
96
return Poll :: Ready ( Err ( io:: ErrorKind :: WriteZero . into ( ) ) ) ;
94
97
}
95
98
* written += n;
96
99
}
97
100
if this. prefix . is_empty ( ) {
98
- this. state = State :: WriteData ( 0 )
101
+ * this. state = State :: WriteData ( 0 )
99
102
} else {
100
- this. state = State :: WritePrefix ( this. prefix )
103
+ * this. state = State :: WritePrefix ( this. prefix )
101
104
}
102
105
}
103
106
State :: WritePrefix ( buf) => {
104
107
while !buf. is_empty ( ) {
105
- let n = ready ! ( Pin :: new ( & mut this. writer) . poll_write( cx, buf) ) ?;
108
+ let n = ready ! ( this. writer. as_mut ( ) . poll_write( cx, buf) ) ?;
106
109
if n == 0 {
107
110
return Poll :: Ready ( Err ( io:: ErrorKind :: WriteZero . into ( ) ) ) ;
108
111
}
109
112
let ( _, rest) = std:: mem:: replace ( buf, & [ ] ) . split_at ( n) ;
110
113
* buf = rest;
111
114
}
112
- this. state = State :: WriteData ( 0 )
115
+ * this. state = State :: WriteData ( 0 )
113
116
}
114
117
State :: WriteData ( written) => {
115
118
while * written != data. len ( ) {
116
- let n = ready ! ( Pin :: new ( & mut this. writer) . poll_write( cx, & data[ * written..] ) ) ?;
119
+ let n = ready ! ( this. writer. as_mut ( ) . poll_write( cx, & data[ * written..] ) ) ?;
117
120
if n == 0 {
118
121
return Poll :: Ready ( Err ( io:: ErrorKind :: WriteZero . into ( ) ) ) ;
119
122
}
@@ -122,12 +125,12 @@ mod async_io {
122
125
if this. suffix . is_empty ( ) {
123
126
return Poll :: Ready ( Ok ( 4 + this. prefix . len ( ) + * written) ) ;
124
127
} else {
125
- this. state = State :: WriteSuffix ( this. suffix )
128
+ * this. state = State :: WriteSuffix ( this. suffix )
126
129
}
127
130
}
128
131
State :: WriteSuffix ( buf) => {
129
132
while !buf. is_empty ( ) {
130
- let n = ready ! ( Pin :: new ( & mut this. writer) . poll_write( cx, buf) ) ?;
133
+ let n = ready ! ( this. writer. as_mut ( ) . poll_write( cx, buf) ) ?;
131
134
if n == 0 {
132
135
return Poll :: Ready ( Err ( io:: ErrorKind :: WriteZero . into ( ) ) ) ;
133
136
}
@@ -140,12 +143,14 @@ mod async_io {
140
143
}
141
144
}
142
145
143
- fn poll_flush ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
144
- Pin :: new ( & mut * self . writer ) . poll_flush ( cx)
146
+ fn poll_flush ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
147
+ let this = self . project ( ) ;
148
+ this. writer . poll_flush ( cx)
145
149
}
146
150
147
- fn poll_close ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
148
- Pin :: new ( & mut self . writer ) . poll_close ( cx)
151
+ fn poll_close ( self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > {
152
+ let this = self . project ( ) ;
153
+ this. writer . poll_close ( cx)
149
154
}
150
155
}
151
156
0 commit comments