Skip to content

Commit 7289a04

Browse files
committed
refactor
1 parent bce8468 commit 7289a04

File tree

15 files changed

+142
-153
lines changed

15 files changed

+142
-153
lines changed

crate-status.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ A utility crate with types and functionality related to shallow-file handling.
415415
* [x] decode (zero-copy)
416416
* [x] [error line](https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt#L28:L28)
417417
* [x] [V2 additions](https://github.com/git/git/blob/master/Documentation/technical/protocol-v2.txt#L35:L36)
418-
* [x] [side-band mode](https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt#L467:L467)
418+
* [x] [sideband mode](https://github.com/git/git/blob/master/Documentation/technical/pack-protocol.txt#L467:L467)
419419
* [x] `Read` from packet line with (optional) progress support via sidebands
420420
* [x] `Write` with built-in packet line encoding
421421
* [x] `async` support

gix-packetline/src/async_io/encode.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,14 @@ pub async fn flush_to_write(mut out: impl AsyncWrite + Unpin) -> io::Result<usiz
209209
Ok(4)
210210
}
211211

212-
/// Write `data` of `kind` to `out` using side-band encoding.
212+
/// Write `data` of `kind` to `out` using sideband encoding.
213213
pub async fn band_to_write(kind: Channel, data: &[u8], out: impl AsyncWrite + Unpin) -> io::Result<usize> {
214214
prefixed_data_to_write(&[kind as u8], data, out).await
215215
}
216216

217-
/// Serialize this instance to `out`, returning the amount of bytes written.
217+
/// Serialize `band` to `out`, returning the amount of bytes written.
218218
///
219-
/// The data written to `out` can be decoded with [`Borrowed::decode_band()]`.
219+
/// The data written to `out` can be decoded with [`crate::PacketLineRef::decode_band()`].
220220
pub async fn write_band(band: &BandRef<'_>, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
221221
match band {
222222
BandRef::Data(d) => band_to_write(Channel::Data, d, out),
@@ -226,19 +226,19 @@ pub async fn write_band(band: &BandRef<'_>, out: impl AsyncWrite + Unpin) -> io:
226226
.await
227227
}
228228

229-
/// Serialize this instance to `out`, appending a newline if there is none, returning the amount of bytes written.
229+
/// Serialize `band` to `out`, appending a newline if there is none, returning the amount of bytes written.
230230
pub async fn write_text(text: &TextRef<'_>, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
231231
text_to_write(text.0, out).await
232232
}
233233

234-
/// Serialize this line as error to `out`.
234+
/// Serialize `error` to `out`.
235235
///
236-
/// This includes a marker to allow decoding it outside of a side-band channel, returning the amount of bytes written.
236+
/// This includes a marker to allow decoding it outside a sideband channel, returning the amount of bytes written.
237237
pub async fn write_error(error: &ErrorRef<'_>, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
238238
error_to_write(error.0, out).await
239239
}
240240

241-
/// Serialize this instance to `out` in git `packetline` format, returning the amount of bytes written to `out`.
241+
/// Serialize `line` to `out` in git `packetline` format, returning the amount of bytes written to `out`.
242242
pub async fn write_packet_line(line: &PacketLineRef<'_>, out: impl AsyncWrite + Unpin) -> io::Result<usize> {
243243
match line {
244244
PacketLineRef::Data(d) => data_to_write(d, out).await,

gix-packetline/src/async_io/read.rs

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ use futures_lite::AsyncReadExt;
1010
pub use super::sidebands::WithSidebands;
1111
use crate::{
1212
decode,
13-
read::{ExhaustiveOutcome, ProgressAction},
14-
PacketLineRef, StreamingPeekableIterState, MAX_LINE_LEN, U16_HEX_BYTES,
13+
read::{ExhaustiveOutcome, ProgressAction, StreamingPeekableIterState},
14+
PacketLineRef, MAX_LINE_LEN, U16_HEX_BYTES,
1515
};
1616

1717
/// Read pack lines one after another, without consuming more than needed from the underlying
18-
/// [`AsyncRead`]. [`Flush`][PacketLineRef::Flush] lines cause the reader to stop producing lines forever,
18+
/// [`AsyncRead`]. [`Flush`](PacketLineRef::Flush) lines cause the reader to stop producing lines forever,
1919
/// leaving [`AsyncRead`] at the start of whatever comes next.
2020
///
2121
/// This implementation tries hard not to allocate at all which leads to quite some added complexity and plenty of extra memory copies.
@@ -36,7 +36,6 @@ where
3636
}
3737
}
3838

39-
#[allow(clippy::needless_lifetimes)] // TODO: remove once this is clippy false positive is fixed
4039
async fn read_line_inner<'a>(
4140
reader: &mut T,
4241
buf: &'a mut [u8],
@@ -127,33 +126,32 @@ where
127126
/// Returns `None` if the end of iteration is reached because of one of the following:
128127
///
129128
/// * natural EOF
130-
/// * ERR packet line encountered if [`fail_on_err_lines()`][StreamingPeekableIter::fail_on_err_lines()] is true.
129+
/// * ERR packet line encountered if [`fail_on_err_lines()`](StreamingPeekableIterState::fail_on_err_lines()) is true.
131130
/// * A `delimiter` packet line encountered
132131
pub async fn read_line(&mut self) -> Option<io::Result<Result<PacketLineRef<'_>, decode::Error>>> {
133-
if self.state.is_done {
132+
let state = &mut self.state;
133+
if state.is_done {
134134
return None;
135135
}
136-
if !self.state.peek_buf.is_empty() {
137-
std::mem::swap(&mut self.state.peek_buf, &mut self.state.buf);
138-
self.state.peek_buf.clear();
139-
Some(Ok(Ok(
140-
crate::decode(&self.state.buf).expect("only valid data in peek buf")
141-
)))
136+
if !state.peek_buf.is_empty() {
137+
std::mem::swap(&mut state.peek_buf, &mut state.buf);
138+
state.peek_buf.clear();
139+
Some(Ok(Ok(crate::decode(&state.buf).expect("only valid data in peek buf"))))
142140
} else {
143-
if self.state.buf.len() != MAX_LINE_LEN {
144-
self.state.buf.resize(MAX_LINE_LEN, 0);
141+
if state.buf.len() != MAX_LINE_LEN {
142+
state.buf.resize(MAX_LINE_LEN, 0);
145143
}
146144
let (is_done, stopped_at, res) = Self::read_line_inner_exhaustive(
147-
&mut self.state.read,
148-
&mut self.state.buf,
149-
self.state.delimiters,
150-
self.state.fail_on_err_lines,
145+
&mut state.read,
146+
&mut state.buf,
147+
state.delimiters,
148+
state.fail_on_err_lines,
151149
false,
152-
self.state.trace,
150+
state.trace,
153151
)
154152
.await;
155-
self.state.is_done = is_done;
156-
self.state.stopped_at = stopped_at;
153+
state.is_done = is_done;
154+
state.stopped_at = stopped_at;
157155
res
158156
}
159157
}
@@ -163,56 +161,55 @@ where
163161
///
164162
/// Multiple calls to peek will return the same packet line, if there is one.
165163
pub async fn peek_line(&mut self) -> Option<io::Result<Result<PacketLineRef<'_>, decode::Error>>> {
166-
if self.state.is_done {
164+
let state = &mut self.state;
165+
if state.is_done {
167166
return None;
168167
}
169-
if self.state.peek_buf.is_empty() {
170-
self.state.peek_buf.resize(MAX_LINE_LEN, 0);
168+
if state.peek_buf.is_empty() {
169+
state.peek_buf.resize(MAX_LINE_LEN, 0);
171170
let (is_done, stopped_at, res) = Self::read_line_inner_exhaustive(
172-
&mut self.state.read,
173-
&mut self.state.peek_buf,
174-
self.state.delimiters,
175-
self.state.fail_on_err_lines,
171+
&mut state.read,
172+
&mut state.peek_buf,
173+
state.delimiters,
174+
state.fail_on_err_lines,
176175
true,
177-
self.state.trace,
176+
state.trace,
178177
)
179178
.await;
180-
self.state.is_done = is_done;
181-
self.state.stopped_at = stopped_at;
179+
state.is_done = is_done;
180+
state.stopped_at = stopped_at;
182181
res
183182
} else {
184-
Some(Ok(Ok(
185-
crate::decode(&self.state.peek_buf).expect("only valid data here")
186-
)))
183+
Some(Ok(Ok(crate::decode(&state.peek_buf).expect("only valid data here"))))
187184
}
188185
}
189186

190-
/// Same as [`as_read_with_sidebands(…)`][StreamingPeekableIter::as_read_with_sidebands()], but for channels without side band support.
187+
/// Same as [`as_read_with_sidebands(…)`](StreamingPeekableIter::as_read_with_sidebands()), but for channels without side band support.
191188
///
192189
/// Due to the preconfigured function type this method can be called without 'turbofish'.
193190
#[allow(clippy::type_complexity)]
194191
pub fn as_read(&mut self) -> WithSidebands<'_, T, fn(bool, &[u8]) -> ProgressAction> {
195192
WithSidebands::new(self)
196193
}
197194

198-
/// Return this instance as implementor of [`Read`][io::Read] assuming side bands to be used in all received packet lines.
199-
/// Each invocation of [`read_line()`][io::BufRead::read_line()] returns a packet line.
195+
/// Return this instance as implementor of [`Read`](io::Read) assuming sidebands to be used in all received packet lines.
196+
/// Each invocation of [`read_line()`](io::BufRead::read_line()) returns a packet line.
200197
///
201198
/// Progress or error information will be passed to the given `handle_progress(is_error, text)` function, with `is_error: bool`
202199
/// being true in case the `text` is to be interpreted as error.
203200
///
204-
/// _Please note_ that side bands need to be negotiated with the server.
201+
/// _Please note_ that sidebands need to be negotiated with the server.
205202
pub fn as_read_with_sidebands<F: FnMut(bool, &[u8]) -> ProgressAction + Unpin>(
206203
&mut self,
207204
handle_progress: F,
208205
) -> WithSidebands<'_, T, F> {
209206
WithSidebands::with_progress_handler(self, handle_progress)
210207
}
211208

212-
/// Same as [`as_read_with_sidebands(…)`][StreamingPeekableIter::as_read_with_sidebands()], but for channels without side band support.
209+
/// Same as [`as_read_with_sidebands(…)`](StreamingPeekableIter::as_read_with_sidebands()), but for channels without side band support.
213210
///
214211
/// The type parameter `F` needs to be configured for this method to be callable using the 'turbofish' operator.
215-
/// Use [`as_read()`][StreamingPeekableIter::as_read()].
212+
/// Use [`as_read()`](StreamingPeekableIter::as_read()).
216213
pub fn as_read_without_sidebands<F: FnMut(bool, &[u8]) -> ProgressAction + Unpin>(
217214
&mut self,
218215
) -> WithSidebands<'_, T, F> {

gix-packetline/src/async_io/sidebands.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use super::read::StreamingPeekableIter;
1010
use crate::{decode, read::ProgressAction, BandRef, PacketLineRef, TextRef, U16_HEX_BYTES};
1111

1212
type ReadLineResult<'a> = Option<std::io::Result<Result<PacketLineRef<'a>, decode::Error>>>;
13-
/// An implementor of [`AsyncBufRead`] yielding packet lines on each call to [`read_line()`][AsyncBufRead::read_line()].
14-
/// It's also possible to hide the underlying packet lines using the [`Read`][AsyncRead] implementation which is useful
13+
/// An implementor of [`AsyncBufRead`] yielding packet lines on each call to `read_line()`.
14+
/// It's also possible to hide the underlying packet lines using the [`Read`](AsyncRead) implementation which is useful
1515
/// if they represent binary data, like the one of a pack file.
1616
pub struct WithSidebands<'a, T, F>
1717
where
@@ -32,7 +32,6 @@ where
3232
parent
3333
.as_mut()
3434
.expect("parent is always available if we are idle")
35-
.state
3635
.reset();
3736
}
3837
}
@@ -101,25 +100,23 @@ where
101100
}
102101
}
103102

104-
/// Forwards to the parent [`StreamingPeekableIter::reset_with()`]
103+
/// Forwards to the parent [`StreamingPeekableIter::reset_with()`](crate::read::StreamingPeekableIterState::reset_with()).
105104
pub fn reset_with(&mut self, delimiters: &'static [PacketLineRef<'static>]) {
106105
if let State::Idle { ref mut parent } = self.state {
107106
parent
108107
.as_mut()
109108
.expect("parent is always available if we are idle")
110-
.state
111109
.reset_with(delimiters);
112110
}
113111
}
114112

115-
/// Forwards to the parent [`StreamingPeekableIter::stopped_at()`]
113+
/// Forwards to the parent [`StreamingPeekableIterState::stopped_at()`](crate::read::StreamingPeekableIterState::stopped_at()).
116114
pub fn stopped_at(&self) -> Option<PacketLineRef<'static>> {
117115
match self.state {
118116
State::Idle { ref parent } => {
119117
parent
120118
.as_ref()
121119
.expect("parent is always available if we are idle")
122-
.state
123120
.stopped_at
124121
}
125122
_ => None,
@@ -132,7 +129,7 @@ where
132129
}
133130

134131
/// Effectively forwards to the parent [`StreamingPeekableIter::peek_line()`], allowing to see what would be returned
135-
/// next on a call to [`read_line()`][io::BufRead::read_line()].
132+
/// next on a call to `read_line()`.
136133
///
137134
/// # Warning
138135
///
@@ -331,9 +328,7 @@ where
331328
}
332329
let range = self.pos..self.cap;
333330
match &self.get_mut().state {
334-
State::Idle { parent } => {
335-
Poll::Ready(Ok(&parent.as_ref().expect("parent always available").state.buf[range]))
336-
}
331+
State::Idle { parent } => Poll::Ready(Ok(&parent.as_ref().expect("parent always available").buf[range])),
337332
State::ReadLine { .. } => unreachable!("at least in theory"),
338333
}
339334
}

gix-packetline/src/blocking_io/encode.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,21 @@ pub fn error_to_write(message: &[u8], out: impl io::Write) -> io::Result<usize>
2626
prefixed_data_to_write(ERR_PREFIX, message, out)
2727
}
2828

29-
/// Serialize this line as error to `out`.
29+
/// Serialize `error` to `out`.
3030
///
31-
/// This includes a marker to allow decoding it outside a side-band channel, returning the amount of bytes written.
31+
/// This includes a marker to allow decoding it outside a sideband channel, returning the amount of bytes written.
3232
pub fn write_error(error: &ErrorRef<'_>, out: impl io::Write) -> io::Result<usize> {
3333
error_to_write(error.0, out)
3434
}
3535

36-
/// Write `data` of `kind` to `out` using side-band encoding.
36+
/// Write `data` of `kind` to `out` using sideband encoding.
3737
pub fn band_to_write(kind: Channel, data: &[u8], out: impl io::Write) -> io::Result<usize> {
3838
prefixed_data_to_write(&[kind as u8], data, out)
3939
}
4040

41-
/// Serialize [`BandRef`] to `out`, returning the amount of bytes written.
41+
/// Serialize `band` to `out`, returning the amount of bytes written.
4242
///
43-
/// The data written to `out` can be decoded with [`Borrowed::decode_band()]`.
43+
/// The data written to `out` can be decoded with [`PacketLineRef::decode_band()`].
4444
pub fn write_band(band: &BandRef<'_>, out: impl io::Write) -> io::Result<usize> {
4545
match band {
4646
BandRef::Data(d) => band_to_write(Channel::Data, d, out),
@@ -54,7 +54,7 @@ pub fn data_to_write(data: &[u8], out: impl io::Write) -> io::Result<usize> {
5454
prefixed_data_to_write(&[], data, out)
5555
}
5656

57-
/// Serialize this instance to `out` in git `packetline` format, returning the amount of bytes written to `out`.
57+
/// Serialize `line` to `out` in git `packetline` format, returning the amount of bytes written to `out`.
5858
pub fn write_packet_line(line: &PacketLineRef<'_>, out: impl io::Write) -> io::Result<usize> {
5959
match line {
6060
PacketLineRef::Data(d) => data_to_write(d, out),
@@ -69,7 +69,7 @@ pub fn text_to_write(text: &[u8], out: impl io::Write) -> io::Result<usize> {
6969
prefixed_and_suffixed_data_to_write(&[], text, b"\n", out)
7070
}
7171

72-
/// Serialize this instance to `out`, appending a newline if there is none, returning the amount of bytes written.
72+
/// Serialize `text` to `out`, appending a newline if there is none, returning the amount of bytes written.
7373
pub fn write_text(text: &TextRef<'_>, out: impl io::Write) -> io::Result<usize> {
7474
text_to_write(text.0, out)
7575
}

0 commit comments

Comments
 (0)