@@ -91,7 +91,8 @@ pub enum Identity {
91
91
92
92
/// A type implementing `Write`, which when done can be transformed into a `Read` for obtaining the response.
93
93
pub struct RequestWriter < ' a > {
94
- pub ( crate ) writer : WritePacketOnDrop < Box < dyn io:: Write + ' a > > ,
94
+ on_into_read : MessageKind ,
95
+ pub ( crate ) writer : git_packetline:: Writer < Box < dyn io:: Write + ' a > > ,
95
96
pub ( crate ) reader : Box < dyn ExtendedBufRead + ' a > ,
96
97
}
97
98
@@ -110,24 +111,32 @@ impl<'a> RequestWriter<'a> {
110
111
writer : W ,
111
112
reader : Box < dyn ExtendedBufRead + ' a > ,
112
113
write_mode : WriteMode ,
113
- on_drop : Vec < MessageKind > ,
114
+ on_into_read : MessageKind ,
114
115
) -> Self {
115
116
let mut writer = git_packetline:: Writer :: new ( Box :: new ( writer) as Box < dyn io:: Write > ) ;
116
117
match write_mode {
117
118
WriteMode :: Binary => writer. enable_binary_mode ( ) ,
118
119
WriteMode :: OneLFTerminatedLinePerWriteCall => writer. enable_text_mode ( ) ,
119
120
}
120
121
RequestWriter {
121
- writer : WritePacketOnDrop :: new ( writer, on_drop) ,
122
+ on_into_read,
123
+ writer,
122
124
reader,
123
125
}
124
126
}
125
- pub fn into_read ( self ) -> Box < dyn ExtendedBufRead + ' a > {
126
- self . reader
127
+ pub fn into_read ( mut self ) -> io:: Result < Box < dyn ExtendedBufRead + ' a > > {
128
+ self . write_message ( self . on_into_read ) ?;
129
+ Ok ( self . reader )
127
130
}
128
131
129
- pub fn write_message ( & mut self , kind : MessageKind ) -> io:: Result < ( ) > {
130
- self . writer . write_message ( kind)
132
+ pub fn write_message ( & mut self , message : MessageKind ) -> io:: Result < ( ) > {
133
+ match message {
134
+ MessageKind :: Flush => git_packetline:: PacketLine :: Flush . to_write ( & mut self . writer . inner ) ,
135
+ MessageKind :: Delimiter => git_packetline:: PacketLine :: Delimiter . to_write ( & mut self . writer . inner ) ,
136
+ MessageKind :: Text ( t) => git_packetline:: borrowed:: Text :: from ( t) . to_write ( & mut self . writer . inner ) ,
137
+ }
138
+ . map ( |_| ( ) )
139
+ . map_err ( |err| io:: Error :: new ( io:: ErrorKind :: Other , err) )
131
140
}
132
141
}
133
142
@@ -143,47 +152,6 @@ impl<'a, T: io::Read> ExtendedBufRead for git_packetline::provider::ReadWithSide
143
152
144
153
pub type HandleProgress = Box < dyn FnMut ( bool , & [ u8 ] ) > ;
145
154
146
- pub ( crate ) struct WritePacketOnDrop < W : io:: Write > {
147
- inner : git_packetline:: Writer < W > ,
148
- on_drop : Vec < MessageKind > ,
149
- }
150
-
151
- impl < W : io:: Write > WritePacketOnDrop < W > {
152
- pub fn new ( inner : git_packetline:: Writer < W > , on_drop : Vec < MessageKind > ) -> Self {
153
- WritePacketOnDrop { inner, on_drop }
154
- }
155
-
156
- pub fn write_message ( & mut self , message : MessageKind ) -> io:: Result < ( ) > {
157
- match message {
158
- MessageKind :: Flush => git_packetline:: PacketLine :: Flush . to_write ( & mut self . inner . inner ) ,
159
- MessageKind :: Delimiter => git_packetline:: PacketLine :: Delimiter . to_write ( & mut self . inner . inner ) ,
160
- MessageKind :: Text ( t) => git_packetline:: borrowed:: Text :: from ( t) . to_write ( & mut self . inner . inner ) ,
161
- }
162
- . map ( |_| ( ) )
163
- . map_err ( |err| io:: Error :: new ( io:: ErrorKind :: Other , err) )
164
- }
165
- }
166
-
167
- impl < W : io:: Write > io:: Write for WritePacketOnDrop < W > {
168
- fn write ( & mut self , buf : & [ u8 ] ) -> io:: Result < usize > {
169
- self . inner . write ( buf)
170
- }
171
-
172
- fn flush ( & mut self ) -> io:: Result < ( ) > {
173
- self . inner . flush ( )
174
- }
175
- }
176
-
177
- impl < W : io:: Write > Drop for WritePacketOnDrop < W > {
178
- fn drop ( & mut self ) {
179
- let mut on_drop = std:: mem:: take ( & mut self . on_drop ) ;
180
- for msg in on_drop. drain ( ..) {
181
- self . write_message ( msg)
182
- . expect ( "packet line write on drop must work or we may as well panic to prevent weird surprises" ) ;
183
- }
184
- }
185
- }
186
-
187
155
/// All methods provided here must be called in the correct order according to the communication protocol used to connect to them.
188
156
/// It does, however, know just enough to be able to provide a higher-level interface than would otherwise be possible.
189
157
/// Thus the consumer of this trait will not have to deal with packet lines at all.
@@ -208,10 +176,10 @@ pub trait Transport {
208
176
}
209
177
/// Obtain a writer for sending data and obtaining the response. It can be configured in various ways,
210
178
/// and should to support with the task at hand.
211
- /// `send_mode` determines how calls to the `write(…)` method are interpreted, and `on_drop ` determines
212
- /// which messages to write when the writer is dropped. This happens naturally when switching to reading the response with `into_read()`.
179
+ /// `send_mode` determines how calls to the `write(…)` method are interpreted, and `on_into_read ` determines
180
+ /// which message to write when the writer is turned into the response reader using `into_read()`.
213
181
/// If `handle_progress` is not None, it's function passed a text line without trailing LF from which progress information can be parsed.
214
- fn request ( & mut self , write_mode : WriteMode , on_drop : Vec < MessageKind > ) -> Result < RequestWriter , Error > ;
182
+ fn request ( & mut self , write_mode : WriteMode , on_into_read : MessageKind ) -> Result < RequestWriter , Error > ;
215
183
216
184
/// Closes the connection to indicate no further requests will be made.
217
185
fn close ( & mut self ) -> Result < ( ) , Error > ;
@@ -253,7 +221,7 @@ impl<T: Transport> TransportV2Ext for T {
253
221
capabilities : impl IntoIterator < Item = ( & ' a str , Option < & ' a str > ) > ,
254
222
arguments : Option < impl IntoIterator < Item = BString > > ,
255
223
) -> Result < Box < dyn ExtendedBufRead + ' _ > , Error > {
256
- let mut writer = self . request ( WriteMode :: OneLFTerminatedLinePerWriteCall , vec ! [ MessageKind :: Flush ] ) ?;
224
+ let mut writer = self . request ( WriteMode :: OneLFTerminatedLinePerWriteCall , MessageKind :: Flush ) ?;
257
225
writer. write_all ( format ! ( "command={}" , command) . as_bytes ( ) ) ?;
258
226
for ( name, value) in capabilities {
259
227
match value {
@@ -267,6 +235,6 @@ impl<T: Transport> TransportV2Ext for T {
267
235
writer. write_all ( argument. as_ref ( ) ) ?;
268
236
}
269
237
}
270
- Ok ( writer. into_read ( ) )
238
+ Ok ( writer. into_read ( ) ? )
271
239
}
272
240
}
0 commit comments