Skip to content

Commit bfd8c04

Browse files
committed
fix!: rename WithSidebands::read_line() to *::read_line_to_string().
Also, we remove the existing blocking `read_line()` override as it's not representable in the async implementation anyway, to unify both implementations once again. The new name also makes sure that we are not accidentally calling a built-in implementation on the `BufRead` trait as it's clear our method has to be called under a different name.
1 parent ac34d2e commit bfd8c04

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

gix-packetline/src/read/sidebands/async_io.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ where
170170
}
171171

172172
/// Read a packet line as string line.
173-
pub fn read_line<'b>(&'b mut self, buf: &'b mut String) -> ReadLineFuture<'a, 'b, T, F> {
173+
pub fn read_line_to_string<'b>(&'b mut self, buf: &'b mut String) -> ReadLineFuture<'a, 'b, T, F> {
174174
ReadLineFuture { parent: self, buf }
175175
}
176176

gix-packetline/src/read/sidebands/blocking_io.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ where
109109
);
110110
self.parent.read_line()
111111
}
112+
113+
/// Like `BufRead::read_line()`, but will only read one packetline at a time.
114+
///
115+
/// It will also be easier to call as sometimes it's unclear which implementation we get on a type like this with
116+
/// plenty of generic parameters.
117+
pub fn read_line_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
118+
assert_eq!(
119+
self.cap, 0,
120+
"we don't support partial buffers right now - read-line must be used consistently"
121+
);
122+
let line = std::str::from_utf8(self.fill_buf()?).map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
123+
buf.push_str(line);
124+
let bytes = line.len();
125+
self.cap = 0;
126+
Ok(bytes)
127+
}
112128
}
113129

114130
impl<'a, T, F> BufRead for WithSidebands<'a, T, F>
@@ -168,18 +184,6 @@ where
168184
fn consume(&mut self, amt: usize) {
169185
self.pos = std::cmp::min(self.pos + amt, self.cap);
170186
}
171-
172-
fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
173-
assert_eq!(
174-
self.cap, 0,
175-
"we don't support partial buffers right now - read-line must be used consistently"
176-
);
177-
let line = std::str::from_utf8(self.fill_buf()?).map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;
178-
buf.push_str(line);
179-
let bytes = line.len();
180-
self.cap = 0;
181-
Ok(bytes)
182-
}
183187
}
184188

185189
impl<'a, T, F> io::Read for WithSidebands<'a, T, F>

gix-packetline/tests/read/sideband.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#[cfg(feature = "blocking-io")]
2-
use std::io::{BufRead, Read};
2+
use std::io::Read;
33

44
use bstr::{BString, ByteSlice};
55
#[cfg(all(not(feature = "blocking-io"), feature = "async-io"))]
@@ -106,16 +106,16 @@ async fn read_line_trait_method_reads_one_packet_line_at_a_time() -> crate::Resu
106106

107107
let mut out = String::new();
108108
let mut r = rd.as_read();
109-
r.read_line(&mut out).await?;
109+
r.read_line_to_string(&mut out).await?;
110110
assert_eq!(out, "808e50d724f604f69ab93c6da2919c014667bedb HEAD\0multi_ack thin-pack side-band side-band-64k ofs-delta shallow deepen-since deepen-not deepen-relative no-progress include-tag multi_ack_detailed symref=HEAD:refs/heads/master object-format=sha1 agent=git/2.28.0\n");
111111
out.clear();
112-
r.read_line(&mut out).await?;
112+
r.read_line_to_string(&mut out).await?;
113113
assert_eq!(out, "808e50d724f604f69ab93c6da2919c014667bedb refs/heads/master\n");
114114
out.clear();
115-
r.read_line(&mut out).await?;
115+
r.read_line_to_string(&mut out).await?;
116116
assert_eq!(out, "", "flush means empty lines…");
117117
out.clear();
118-
r.read_line(&mut out).await?;
118+
r.read_line_to_string(&mut out).await?;
119119
assert_eq!(out, "", "…which can't be overcome unless the reader is reset");
120120
assert_eq!(
121121
r.stopped_at(),
@@ -127,18 +127,18 @@ async fn read_line_trait_method_reads_one_packet_line_at_a_time() -> crate::Resu
127127
rd.reset();
128128

129129
let mut r = rd.as_read();
130-
r.read_line(&mut out).await?;
130+
r.read_line_to_string(&mut out).await?;
131131
assert_eq!(out, "NAK\n");
132132

133133
drop(r);
134134

135135
let mut r = rd.as_read_with_sidebands(|_, _| ());
136136
out.clear();
137-
r.read_line(&mut out).await?;
137+
r.read_line_to_string(&mut out).await?;
138138
assert_eq!(out, "&");
139139

140140
out.clear();
141-
r.read_line(&mut out).await?;
141+
r.read_line_to_string(&mut out).await?;
142142
assert_eq!(out, "");
143143

144144
Ok(())
@@ -199,7 +199,7 @@ async fn peek_past_an_actual_eof_is_an_error() -> crate::Result {
199199
assert_eq!(res.expect("one line")??, b"ERR e");
200200

201201
let mut buf = String::new();
202-
reader.read_line(&mut buf).await?;
202+
reader.read_line_to_string(&mut buf).await?;
203203
assert_eq!(
204204
buf, "ERR e",
205205
"by default ERR lines won't propagate as failure but are merely text"
@@ -223,7 +223,7 @@ async fn peek_past_a_delimiter_is_no_error() -> crate::Result {
223223
assert_eq!(res.expect("one line")??, b"hello");
224224

225225
let mut buf = String::new();
226-
reader.read_line(&mut buf).await?;
226+
reader.read_line_to_string(&mut buf).await?;
227227
assert_eq!(buf, "hello");
228228

229229
let res = reader.peek_data_line().await;

0 commit comments

Comments
 (0)