Skip to content

Commit 8638c0f

Browse files
committed
fix!: move TransportWithoutIO::request() method to I/O-specific traits
1 parent cbf7445 commit 8638c0f

File tree

8 files changed

+163
-141
lines changed

8 files changed

+163
-141
lines changed

gix-protocol/tests/protocol/fetch/arguments.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,6 @@ mod impls {
3434
self.inner.set_identity(identity)
3535
}
3636

37-
fn request(
38-
&mut self,
39-
write_mode: WriteMode,
40-
on_into_read: MessageKind,
41-
trace: bool,
42-
) -> Result<RequestWriter<'_>, Error> {
43-
self.inner.request(write_mode, on_into_read, trace)
44-
}
45-
4637
fn to_url(&self) -> Cow<'_, BStr> {
4738
self.inner.to_url()
4839
}
@@ -71,6 +62,15 @@ mod impls {
7162
) -> Result<SetServiceResponse<'_>, Error> {
7263
self.inner.handshake(service, extra_parameters)
7364
}
65+
66+
fn request(
67+
&mut self,
68+
write_mode: WriteMode,
69+
on_into_read: MessageKind,
70+
trace: bool,
71+
) -> Result<RequestWriter<'_>, Error> {
72+
self.inner.request(write_mode, on_into_read, trace)
73+
}
7474
}
7575
}
7676

gix-transport/src/client/async_io/traits.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use futures_lite::io::AsyncWriteExt;
66

77
use crate::{
88
client::{
9-
async_io::{ExtendedBufRead, ReadlineBufRead},
9+
async_io::{request::RequestWriter, ExtendedBufRead, ReadlineBufRead},
1010
Capabilities, Error, MessageKind, TransportWithoutIO, WriteMode,
1111
},
1212
Protocol, Service,
@@ -45,6 +45,18 @@ pub trait Transport: TransportWithoutIO {
4545
service: Service,
4646
extra_parameters: &'a [(&'a str, Option<&'a str>)],
4747
) -> Result<SetServiceResponse<'_>, Error>;
48+
49+
/// Get a writer for sending data and obtaining the response. It can be configured in various ways
50+
/// to support the task at hand.
51+
/// `write_mode` determines how calls to the `write(…)` method are interpreted, and `on_into_read` determines
52+
/// which message to write when the writer is turned into the response reader using [`into_read()`][RequestWriter::into_read()].
53+
/// If `trace` is `true`, then all packetlines written and received will be traced using facilities provided by the `gix_trace` crate.
54+
fn request(
55+
&mut self,
56+
write_mode: WriteMode,
57+
on_into_read: MessageKind,
58+
trace: bool,
59+
) -> Result<RequestWriter<'_>, Error>;
4860
}
4961

5062
// Would be nice if the box implementation could auto-forward to all implemented traits.
@@ -57,6 +69,15 @@ impl<T: Transport + ?Sized> Transport for Box<T> {
5769
) -> Result<SetServiceResponse<'_>, Error> {
5870
self.deref_mut().handshake(service, extra_parameters).await
5971
}
72+
73+
fn request(
74+
&mut self,
75+
write_mode: WriteMode,
76+
on_into_read: MessageKind,
77+
trace: bool,
78+
) -> Result<RequestWriter<'_>, Error> {
79+
self.deref_mut().request(write_mode, on_into_read, trace)
80+
}
6081
}
6182

6283
// Would be nice if the box implementation could auto-forward to all implemented traits.
@@ -69,6 +90,15 @@ impl<T: Transport + ?Sized> Transport for &mut T {
6990
) -> Result<SetServiceResponse<'_>, Error> {
7091
self.deref_mut().handshake(service, extra_parameters).await
7192
}
93+
94+
fn request(
95+
&mut self,
96+
write_mode: WriteMode,
97+
on_into_read: MessageKind,
98+
trace: bool,
99+
) -> Result<RequestWriter<'_>, Error> {
100+
self.deref_mut().request(write_mode, on_into_read, trace)
101+
}
72102
}
73103

74104
/// An extension trait to add more methods to everything implementing [`Transport`].

gix-transport/src/client/blocking_io/file.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,6 @@ impl client::TransportWithoutIO for SpawnProcessOnDemand {
108108
}
109109
}
110110

111-
fn request(
112-
&mut self,
113-
write_mode: WriteMode,
114-
on_into_read: MessageKind,
115-
trace: bool,
116-
) -> Result<RequestWriter<'_>, client::Error> {
117-
self.connection
118-
.as_mut()
119-
.ok_or(client::Error::MissingHandshake)?
120-
.request(write_mode, on_into_read, trace)
121-
}
122-
123111
fn to_url(&self) -> Cow<'_, BStr> {
124112
Cow::Owned(self.url.to_bstring())
125113
}
@@ -281,6 +269,18 @@ impl client::blocking_io::Transport for SpawnProcessOnDemand {
281269
.expect("connection to be there right after setting it")
282270
.handshake(service, extra_parameters)
283271
}
272+
273+
fn request(
274+
&mut self,
275+
write_mode: WriteMode,
276+
on_into_read: MessageKind,
277+
trace: bool,
278+
) -> Result<RequestWriter<'_>, client::Error> {
279+
self.connection
280+
.as_mut()
281+
.ok_or(client::Error::MissingHandshake)?
282+
.request(write_mode, on_into_read, trace)
283+
}
284284
}
285285

286286
/// Connect to a locally readable repository at `path` using the given `desired_version`.

gix-transport/src/client/blocking_io/http/mod.rs

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -336,56 +336,6 @@ impl<H: Http> client::TransportWithoutIO for Transport<H> {
336336
Ok(())
337337
}
338338

339-
fn request(
340-
&mut self,
341-
write_mode: client::WriteMode,
342-
on_into_read: MessageKind,
343-
trace: bool,
344-
) -> Result<RequestWriter<'_>, client::Error> {
345-
let service = self.service.ok_or(client::Error::MissingHandshake)?;
346-
let url = append_url(&self.url, service.as_str());
347-
let static_headers = &[
348-
Cow::Borrowed(self.user_agent_header),
349-
Cow::Owned(format!("Content-Type: application/x-{}-request", service.as_str())),
350-
format!("Accept: application/x-{}-result", service.as_str()).into(),
351-
];
352-
let mut dynamic_headers = Vec::new();
353-
self.add_basic_auth_if_present(&mut dynamic_headers)?;
354-
if self.actual_version != Protocol::V1 {
355-
dynamic_headers.push(Cow::Owned(format!(
356-
"Git-Protocol: version={}",
357-
self.actual_version as usize
358-
)));
359-
}
360-
361-
let PostResponse {
362-
headers,
363-
body,
364-
post_body,
365-
} = self.http.post(
366-
&url,
367-
&self.url,
368-
static_headers.iter().chain(&dynamic_headers),
369-
write_mode.into(),
370-
)?;
371-
let line_provider = self
372-
.line_provider
373-
.as_mut()
374-
.expect("handshake to have been called first");
375-
line_provider.replace(body);
376-
Ok(RequestWriter::new_from_bufread(
377-
post_body,
378-
Box::new(HeadersThenBody::<H, _> {
379-
service,
380-
headers: Some(headers),
381-
body: line_provider.as_read_without_sidebands(),
382-
}),
383-
write_mode,
384-
on_into_read,
385-
trace,
386-
))
387-
}
388-
389339
fn to_url(&self) -> Cow<'_, BStr> {
390340
Cow::Borrowed(self.url.as_str().into())
391341
}
@@ -475,6 +425,56 @@ impl<H: Http> blocking_io::Transport for Transport<H> {
475425
refs,
476426
})
477427
}
428+
429+
fn request(
430+
&mut self,
431+
write_mode: client::WriteMode,
432+
on_into_read: MessageKind,
433+
trace: bool,
434+
) -> Result<RequestWriter<'_>, client::Error> {
435+
let service = self.service.ok_or(client::Error::MissingHandshake)?;
436+
let url = append_url(&self.url, service.as_str());
437+
let static_headers = &[
438+
Cow::Borrowed(self.user_agent_header),
439+
Cow::Owned(format!("Content-Type: application/x-{}-request", service.as_str())),
440+
format!("Accept: application/x-{}-result", service.as_str()).into(),
441+
];
442+
let mut dynamic_headers = Vec::new();
443+
self.add_basic_auth_if_present(&mut dynamic_headers)?;
444+
if self.actual_version != Protocol::V1 {
445+
dynamic_headers.push(Cow::Owned(format!(
446+
"Git-Protocol: version={}",
447+
self.actual_version as usize
448+
)));
449+
}
450+
451+
let PostResponse {
452+
headers,
453+
body,
454+
post_body,
455+
} = self.http.post(
456+
&url,
457+
&self.url,
458+
static_headers.iter().chain(&dynamic_headers),
459+
write_mode.into(),
460+
)?;
461+
let line_provider = self
462+
.line_provider
463+
.as_mut()
464+
.expect("handshake to have been called first");
465+
line_provider.replace(body);
466+
Ok(RequestWriter::new_from_bufread(
467+
post_body,
468+
Box::new(HeadersThenBody::<H, _> {
469+
service,
470+
headers: Some(headers),
471+
body: line_provider.as_read_without_sidebands(),
472+
}),
473+
write_mode,
474+
on_into_read,
475+
trace,
476+
))
477+
}
478478
}
479479

480480
struct HeadersThenBody<H: Http, B: Unpin> {

gix-transport/src/client/blocking_io/traits.rs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use bstr::BString;
44

55
use crate::{
66
client::{
7-
blocking_io::{ExtendedBufRead, ReadlineBufRead},
7+
blocking_io::{request::RequestWriter, ExtendedBufRead, ReadlineBufRead},
88
Capabilities, Error, MessageKind, TransportWithoutIO, WriteMode,
99
},
1010
Protocol, Service,
@@ -42,6 +42,18 @@ pub trait Transport: TransportWithoutIO {
4242
service: Service,
4343
extra_parameters: &'a [(&'a str, Option<&'a str>)],
4444
) -> Result<SetServiceResponse<'_>, Error>;
45+
46+
/// Get a writer for sending data and obtaining the response. It can be configured in various ways
47+
/// to support the task at hand.
48+
/// `write_mode` determines how calls to the `write(…)` method are interpreted, and `on_into_read` determines
49+
/// which message to write when the writer is turned into the response reader using [`into_read()`][RequestWriter::into_read()].
50+
/// If `trace` is `true`, then all packetlines written and received will be traced using facilities provided by the `gix_trace` crate.
51+
fn request(
52+
&mut self,
53+
write_mode: WriteMode,
54+
on_into_read: MessageKind,
55+
trace: bool,
56+
) -> Result<RequestWriter<'_>, Error>;
4557
}
4658

4759
// Would be nice if the box implementation could auto-forward to all implemented traits.
@@ -53,6 +65,15 @@ impl<T: Transport + ?Sized> Transport for Box<T> {
5365
) -> Result<SetServiceResponse<'_>, Error> {
5466
self.deref_mut().handshake(service, extra_parameters)
5567
}
68+
69+
fn request(
70+
&mut self,
71+
write_mode: WriteMode,
72+
on_into_read: MessageKind,
73+
trace: bool,
74+
) -> Result<RequestWriter<'_>, Error> {
75+
self.deref_mut().request(write_mode, on_into_read, trace)
76+
}
5677
}
5778

5879
impl<T: Transport + ?Sized> Transport for &mut T {
@@ -63,6 +84,15 @@ impl<T: Transport + ?Sized> Transport for &mut T {
6384
) -> Result<SetServiceResponse<'_>, Error> {
6485
self.deref_mut().handshake(service, extra_parameters)
6586
}
87+
88+
fn request(
89+
&mut self,
90+
write_mode: WriteMode,
91+
on_into_read: MessageKind,
92+
trace: bool,
93+
) -> Result<RequestWriter<'_>, Error> {
94+
self.deref_mut().request(write_mode, on_into_read, trace)
95+
}
6696
}
6797

6898
/// An extension trait to add more methods to everything implementing [`Transport`].

gix-transport/src/client/git/async_io.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,6 @@ where
5151
R: AsyncRead + Unpin,
5252
W: AsyncWrite + Unpin,
5353
{
54-
fn request(
55-
&mut self,
56-
write_mode: client::WriteMode,
57-
on_into_read: client::MessageKind,
58-
trace: bool,
59-
) -> Result<RequestWriter<'_>, client::Error> {
60-
Ok(RequestWriter::new_from_bufread(
61-
&mut self.writer,
62-
Box::new(self.line_provider.as_read_without_sidebands()),
63-
write_mode,
64-
on_into_read,
65-
trace,
66-
))
67-
}
68-
6954
fn to_url(&self) -> Cow<'_, BStr> {
7055
self.state.custom_url.as_ref().map_or_else(
7156
|| {
@@ -123,6 +108,21 @@ where
123108
refs,
124109
})
125110
}
111+
112+
fn request(
113+
&mut self,
114+
write_mode: client::WriteMode,
115+
on_into_read: client::MessageKind,
116+
trace: bool,
117+
) -> Result<RequestWriter<'_>, client::Error> {
118+
Ok(RequestWriter::new_from_bufread(
119+
&mut self.writer,
120+
Box::new(self.line_provider.as_read_without_sidebands()),
121+
write_mode,
122+
on_into_read,
123+
trace,
124+
))
125+
}
126126
}
127127

128128
impl<R, W> Connection<R, W>

gix-transport/src/client/git/blocking_io.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,6 @@ where
4949
R: std::io::Read,
5050
W: std::io::Write,
5151
{
52-
fn request(
53-
&mut self,
54-
write_mode: client::WriteMode,
55-
on_into_read: client::MessageKind,
56-
trace: bool,
57-
) -> Result<RequestWriter<'_>, client::Error> {
58-
Ok(RequestWriter::new_from_bufread(
59-
&mut self.writer,
60-
Box::new(self.line_provider.as_read_without_sidebands()),
61-
write_mode,
62-
on_into_read,
63-
trace,
64-
))
65-
}
66-
6752
fn to_url(&self) -> Cow<'_, BStr> {
6853
self.state.custom_url.as_ref().map_or_else(
6954
|| {
@@ -118,6 +103,21 @@ where
118103
refs,
119104
})
120105
}
106+
107+
fn request(
108+
&mut self,
109+
write_mode: client::WriteMode,
110+
on_into_read: client::MessageKind,
111+
trace: bool,
112+
) -> Result<RequestWriter<'_>, client::Error> {
113+
Ok(RequestWriter::new_from_bufread(
114+
&mut self.writer,
115+
Box::new(self.line_provider.as_read_without_sidebands()),
116+
write_mode,
117+
on_into_read,
118+
trace,
119+
))
120+
}
121121
}
122122

123123
impl<R, W> Connection<R, W>

0 commit comments

Comments
 (0)