Skip to content

Commit 4594c1e

Browse files
committed
f: Convert BufReader to single-byte-increments.
TODO: Squash commit if this looks reasonable.
1 parent 5272bb0 commit 4594c1e

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

lightning/src/util/ser.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,22 @@ impl<W: Write> Writer for W {
6767
/// Wrap buffering support for implementations of Read.
6868
/// A [`Read`]er which keeps an internal buffer to avoid hitting the underlying stream directly for
6969
/// every read, implementing [`BufRead`].
70-
pub struct BufReader<'a, R: Read> {
70+
///
71+
/// In order to avoid reading bytes past the first object, and those bytes then ending up getting
72+
/// dropped, this BufReader operates in one-byte-increments.
73+
struct BufReader<'a, R: Read> {
7174
inner: &'a mut R,
72-
buf: [u8; 4096],
73-
pos: usize,
74-
limit: usize,
75+
buf: [u8; 1],
76+
is_consumed: bool
7577
}
7678

7779
impl<'a, R: Read> BufReader<'a, R> {
7880
/// Creates a [`BufReader`] which will read from the given `inner`.
7981
pub fn new(inner: &'a mut R) -> Self {
8082
BufReader {
8183
inner,
82-
buf: [0; 4096],
83-
pos: 0,
84-
limit: 0,
84+
buf: [0; 1],
85+
is_consumed: true
8586
}
8687
}
8788
}
@@ -100,21 +101,24 @@ impl<'a, R: Read> Read for BufReader<'a, R> {
100101
impl<'a, R: Read> BufRead for BufReader<'a, R> {
101102
#[inline]
102103
fn fill_buf(&mut self) -> io::Result<&[u8]> {
103-
if self.pos < self.limit {
104-
Ok(&self.buf[self.pos..self.limit])
105-
} else {
104+
if self.is_consumed {
106105
let count = self.inner.read(&mut self.buf[..])?;
107-
debug_assert!(count <= self.buf.len(), "read gave us a garbage length");
108-
self.pos = 0;
109-
self.limit = cmp::min(count, self.buf.len());
110-
Ok(&self.buf[..self.limit])
106+
debug_assert!(count <= 1, "read gave us a garbage length");
107+
108+
// upon hitting EOF, assume the byte is already consumed
109+
self.is_consumed = count == 0;
111110
}
111+
112+
Ok(&self.buf[..])
112113
}
113114

114115
#[inline]
115116
fn consume(&mut self, amount: usize) {
116-
debug_assert!(self.pos.saturating_add(amount) <= self.limit, "Cannot consume more than was provided");
117-
self.pos += amount;
117+
if amount >= 1 {
118+
debug_assert_eq!(amount, 1, "Can only consume one byte");
119+
debug_assert!(!self.is_consumed, "Cannot consume more than had been read");
120+
self.is_consumed = true;
121+
}
118122
}
119123
}
120124

0 commit comments

Comments
 (0)