1
- use std:: future:: Future ;
1
+ use std:: future:: { poll_fn , Future } ;
2
2
use std:: io;
3
3
use std:: path:: Path ;
4
- use std:: pin:: Pin ;
5
4
use std:: task:: { ready, Context , Poll } ;
6
5
7
6
use bytes:: BufMut ;
@@ -27,125 +26,66 @@ pub trait Socket: Send + Sync + Unpin + 'static {
27
26
}
28
27
29
28
fn poll_shutdown ( & mut self , cx : & mut Context < ' _ > ) -> Poll < io:: Result < ( ) > > ;
30
-
31
- fn read < ' a , B : ReadBuf > ( & ' a mut self , buf : & ' a mut B ) -> Read < ' a , Self , B >
32
- where
33
- Self : Sized ,
34
- {
35
- Read { socket : self , buf }
36
- }
37
-
38
- fn write < ' a > ( & ' a mut self , buf : & ' a [ u8 ] ) -> Write < ' a , Self >
39
- where
40
- Self : Sized ,
41
- {
42
- Write { socket : self , buf }
43
- }
44
-
45
- fn flush ( & mut self ) -> Flush < ' _ , Self >
46
- where
47
- Self : Sized ,
48
- {
49
- Flush { socket : self }
50
- }
51
-
52
- fn shutdown ( & mut self ) -> Shutdown < ' _ , Self >
53
- where
54
- Self : Sized ,
55
- {
56
- Shutdown { socket : self }
57
- }
58
29
}
59
30
60
- pub struct Read < ' a , S : ?Sized , B > {
61
- socket : & ' a mut S ,
62
- buf : & ' a mut B ,
63
- }
64
-
65
- impl < S : ?Sized , B > Future for Read < ' _ , S , B >
66
- where
67
- S : Socket ,
68
- B : ReadBuf ,
69
- {
70
- type Output = io:: Result < usize > ;
71
-
72
- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
73
- let this = & mut * self ;
74
-
75
- while this. buf . has_remaining_mut ( ) {
76
- match this. socket . try_read ( & mut * this. buf ) {
31
+ pub trait SocketExt : Socket {
32
+ fn poll_read (
33
+ & mut self ,
34
+ cx : & mut Context < ' _ > ,
35
+ buf : & mut dyn ReadBuf ,
36
+ ) -> Poll < Result < usize , io:: Error > > {
37
+ while buf. has_remaining_mut ( ) {
38
+ match self . try_read ( buf) {
77
39
Err ( e) if e. kind ( ) == io:: ErrorKind :: WouldBlock => {
78
- ready ! ( this . socket . poll_read_ready( cx) ) ?;
40
+ ready ! ( self . poll_read_ready( cx) ) ?;
79
41
}
80
42
ready => return Poll :: Ready ( ready) ,
81
43
}
82
44
}
83
45
84
46
Poll :: Ready ( Ok ( 0 ) )
85
47
}
86
- }
87
-
88
- pub struct Write < ' a , S : ?Sized > {
89
- socket : & ' a mut S ,
90
- buf : & ' a [ u8 ] ,
91
- }
92
48
93
- impl < S : ?Sized > Future for Write < ' _ , S >
94
- where
95
- S : Socket ,
96
- {
97
- type Output = io:: Result < usize > ;
98
-
99
- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
100
- let this = & mut * self ;
101
-
102
- while !this. buf . is_empty ( ) {
103
- match this. socket . try_write ( this. buf ) {
49
+ fn poll_write ( & mut self , cx : & mut Context < ' _ > , buf : & [ u8 ] ) -> Poll < Result < usize , io:: Error > > {
50
+ while !buf. is_empty ( ) {
51
+ match self . try_write ( buf) {
104
52
Err ( e) if e. kind ( ) == io:: ErrorKind :: WouldBlock => {
105
- ready ! ( this . socket . poll_write_ready( cx) ) ?;
53
+ ready ! ( self . poll_write_ready( cx) ) ?;
106
54
}
107
55
ready => return Poll :: Ready ( ready) ,
108
56
}
109
57
}
110
58
111
59
Poll :: Ready ( Ok ( 0 ) )
112
60
}
113
- }
114
61
115
- pub struct Flush < ' a , S : ?Sized > {
116
- socket : & ' a mut S ,
117
- }
118
-
119
- impl < S : Socket + ?Sized > Future for Flush < ' _ , S > {
120
- type Output = io:: Result < ( ) > ;
121
-
122
- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
123
- self . socket . poll_flush ( cx)
62
+ #[ inline( always) ]
63
+ fn shutdown ( & mut self ) -> impl Future < Output = io:: Result < ( ) > > {
64
+ poll_fn ( |cx| self . poll_shutdown ( cx) )
124
65
}
125
- }
126
66
127
- pub struct Shutdown < ' a , S : ?Sized > {
128
- socket : & ' a mut S ,
129
- }
67
+ #[ inline( always) ]
68
+ fn flush ( & mut self ) -> impl Future < Output = io:: Result < ( ) > > {
69
+ poll_fn ( |cx| self . poll_flush ( cx) )
70
+ }
130
71
131
- impl < S : ?Sized > Future for Shutdown < ' _ , S >
132
- where
133
- S : Socket ,
134
- {
135
- type Output = io:: Result < ( ) > ;
72
+ #[ inline( always) ]
73
+ fn write ( & mut self , buf : & [ u8 ] ) -> impl Future < Output = io:: Result < usize > > {
74
+ poll_fn ( |cx| self . poll_write ( cx, buf) )
75
+ }
136
76
137
- fn poll ( mut self : Pin < & mut Self > , cx : & mut Context < ' _ > ) -> Poll < Self :: Output > {
138
- self . socket . poll_shutdown ( cx)
77
+ #[ inline( always) ]
78
+ fn read ( & mut self , buf : & mut impl ReadBuf ) -> impl Future < Output = io:: Result < usize > > {
79
+ poll_fn ( |cx| self . poll_read ( cx, buf) )
139
80
}
140
81
}
141
82
83
+ impl < S : Socket > SocketExt for S { }
84
+
142
85
pub trait WithSocket {
143
86
type Output ;
144
87
145
- fn with_socket < S : Socket > (
146
- self ,
147
- socket : S ,
148
- ) -> impl std:: future:: Future < Output = Self :: Output > + Send ;
88
+ fn with_socket < S : Socket > ( self , socket : S ) -> impl Future < Output = Self :: Output > + Send ;
149
89
}
150
90
151
91
pub struct SocketIntoBox ;
0 commit comments