History

[git-packetline] more docs

Sebastian Thiel committed 5 years ago

Commit 4591e461 parent ec5a54e1 file · +12 −6Browse files

Unified · Split

git-packetline/src/read/mod.rs+12−6
@@ -8,8 +8,10 @@ pub use sidebands::WithSidebands; · 7 unmodified lines
88 /// Read pack lines one after another, without consuming more than needed from the underlying
99 /// [`Read`][io::Read]. [`Flush`][PacketLine::Flush] lines cause the reader to stop producing lines forever,
1010 /// leaving [`Read`][io::Read] at the start of whatever comes next.
11+///
12+/// This implementation tries hard not to allocate at all which leads to quite some added complexity and plenty of extra memory copies.
1113 pub struct StreamingPeekReader<T> {
12− inner: T,
14+ read: T,
1315 peek_buf: Vec<u8>,
1416 fail_on_err_lines: bool,
1517 buf: Vec<u8>,
@@ -25,7 +27,7 @@ where · 9 unmodified lines
2527 /// Return a new instance from `read` which will stop decoding packet lines when receiving one of the given `delimiters`.
2628 pub fn new(read: T, delimiters: &'static [PacketLine<'static>]) -> Self {
2729 StreamingPeekReader {
28− inner: read,
30+ read,
2931 buf: vec![0; MAX_LINE_LEN],
3032 peek_buf: Vec::new(),
3133 delimiters,
@@ -43,7 +45,7 @@ where · 11 unmodified lines
4345
4446 /// Replace the reader used with the given `read`, resetting all other iteration state as well.
4547 pub fn replace(&mut self, read: T) -> T {
46− let prev = std::mem::replace(&mut self.inner, read);
48+ let prev = std::mem::replace(&mut self.read, read);
4749 self.reset();
4850 self.fail_on_err_lines = false;
4951 prev
@@ -106,7 +108,7 @@ where · 56 unmodified lines
106108 } else if self.buf.len() != MAX_LINE_LEN {
107109 self.buf.resize(MAX_LINE_LEN, 0);
108110 }
109− match Self::read_line_inner(&mut self.inner, &mut self.buf) {
111+ match Self::read_line_inner(&mut self.read, &mut self.buf) {
110112 Ok(Ok(line)) => {
111113 if self.delimiters.contains(&line) {
112114 self.is_done = true;
@@ -128,7 +130,11 @@ where · 15 unmodified lines
128130 }
129131 }
130132
131− /// Modify the peek buffer, overwriting the byte at `position` with the given byte to `replace_with`.
133+ /// Modify the peek buffer, overwriting the byte at `position` with the given byte to `replace_with` while truncating
134+ /// it to contain only bytes until the newly replaced `position`.
135+ ///
136+ /// This is useful if you would want to remove 'special bytes' hidden behind, say a NULL byte to disappear and allow
137+ /// standard line readers to read the next line as usual.
132138 ///
133139 /// **Note** that `position` does not include the 4 bytes prefix (they are invisible outside the reader)
134140 pub fn peek_buffer_replace_and_truncate(&mut self, position: usize, replace_with: u8) {
@@ -149,7 +155,7 @@ where · 14 unmodified lines
149155 }
150156 Some(if self.peek_buf.is_empty() {
151157 self.peek_buf.resize(MAX_LINE_LEN, 0);
152− match Self::read_line_inner(&mut self.inner, &mut self.peek_buf) {
158+ match Self::read_line_inner(&mut self.read, &mut self.peek_buf) {
153159 Ok(Ok(line)) => {
154160 if self.delimiters.contains(&line) {
155161 self.is_done = true;