@@ -118,41 +118,7 @@ where
118
118
}
119
119
120
120
pub fn write_io ( & mut self , cx : & mut Context ) -> Poll < io:: Result < usize > > {
121
- struct Writer < ' a , ' b , T > {
122
- io : & ' a mut T ,
123
- cx : & ' a mut Context < ' b > ,
124
- }
125
-
126
- impl < ' a , ' b , T : Unpin > Writer < ' a , ' b , T > {
127
- #[ inline]
128
- fn poll_with < U > (
129
- & mut self ,
130
- f : impl FnOnce ( Pin < & mut T > , & mut Context < ' _ > ) -> Poll < io:: Result < U > > ,
131
- ) -> io:: Result < U > {
132
- match f ( Pin :: new ( self . io ) , self . cx ) {
133
- Poll :: Ready ( result) => result,
134
- Poll :: Pending => Err ( io:: ErrorKind :: WouldBlock . into ( ) ) ,
135
- }
136
- }
137
- }
138
-
139
- impl < ' a , ' b , T : AsyncWrite + Unpin > Write for Writer < ' a , ' b , T > {
140
- #[ inline]
141
- fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
142
- self . poll_with ( |io, cx| io. poll_write ( cx, buf) )
143
- }
144
-
145
- #[ inline]
146
- fn write_vectored ( & mut self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
147
- self . poll_with ( |io, cx| io. poll_write_vectored ( cx, bufs) )
148
- }
149
-
150
- fn flush ( & mut self ) -> io:: Result < ( ) > {
151
- self . poll_with ( |io, cx| io. poll_flush ( cx) )
152
- }
153
- }
154
-
155
- let mut writer = Writer { io : self . io , cx } ;
121
+ let mut writer = SyncWriteAdapter { io : self . io , cx } ;
156
122
157
123
match self . session . write_tls ( & mut writer) {
158
124
Err ( ref err) if err. kind ( ) == io:: ErrorKind :: WouldBlock => Poll :: Pending ,
@@ -360,5 +326,43 @@ impl<'a, 'b, T: AsyncRead + Unpin> Read for SyncReadAdapter<'a, 'b, T> {
360
326
}
361
327
}
362
328
329
+ /// An adapter that implements a [`Write`] interface for [`AsyncWrite`] types and an
330
+ /// associated [`Context`].
331
+ ///
332
+ /// Turns `Poll::Pending` into `WouldBlock`.
333
+ pub struct SyncWriteAdapter < ' a , ' b , T > {
334
+ pub io : & ' a mut T ,
335
+ pub cx : & ' a mut Context < ' b > ,
336
+ }
337
+
338
+ impl < ' a , ' b , T : Unpin > SyncWriteAdapter < ' a , ' b , T > {
339
+ #[ inline]
340
+ fn poll_with < U > (
341
+ & mut self ,
342
+ f : impl FnOnce ( Pin < & mut T > , & mut Context < ' _ > ) -> Poll < io:: Result < U > > ,
343
+ ) -> io:: Result < U > {
344
+ match f ( Pin :: new ( self . io ) , self . cx ) {
345
+ Poll :: Ready ( result) => result,
346
+ Poll :: Pending => Err ( io:: ErrorKind :: WouldBlock . into ( ) ) ,
347
+ }
348
+ }
349
+ }
350
+
351
+ impl < ' a , ' b , T : AsyncWrite + Unpin > Write for SyncWriteAdapter < ' a , ' b , T > {
352
+ #[ inline]
353
+ fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
354
+ self . poll_with ( |io, cx| io. poll_write ( cx, buf) )
355
+ }
356
+
357
+ #[ inline]
358
+ fn write_vectored ( & mut self , bufs : & [ IoSlice < ' _ > ] ) -> io:: Result < usize > {
359
+ self . poll_with ( |io, cx| io. poll_write_vectored ( cx, bufs) )
360
+ }
361
+
362
+ fn flush ( & mut self ) -> io:: Result < ( ) > {
363
+ self . poll_with ( |io, cx| io. poll_flush ( cx) )
364
+ }
365
+ }
366
+
363
367
#[ cfg( test) ]
364
368
mod test_stream;
0 commit comments