|
13 | 13 | //! [`ChannelManager`]: crate::ln::channelmanager::ChannelManager |
14 | 14 | //! [`ChannelMonitor`]: crate::chain::channelmonitor::ChannelMonitor |
15 | 15 |
|
16 | | -use crate::io::{self, BufRead, Read, Write}; |
| 16 | +use crate::io::{self, Read, Write}; |
17 | 17 | use crate::io_extras::{copy, sink}; |
18 | 18 | use crate::ln::interactivetxs::{TxInMetadata, TxOutMetadata}; |
19 | 19 | use crate::ln::onion_utils::{HMAC_COUNT, HMAC_LEN, HOLD_TIME_LEN, MAX_HOPS}; |
@@ -77,72 +77,6 @@ impl<W: Write> Writer for W { |
77 | 77 | } |
78 | 78 | } |
79 | 79 |
|
80 | | -// TODO: Drop this entirely if rust-bitcoin releases a version bump with https://github.com/rust-bitcoin/rust-bitcoin/pull/3173 |
81 | | -/// Wrap buffering support for implementations of Read. |
82 | | -/// A [`Read`]er which keeps an internal buffer to avoid hitting the underlying stream directly for |
83 | | -/// every read, implementing [`BufRead`]. |
84 | | -/// |
85 | | -/// In order to avoid reading bytes past the first object, and those bytes then ending up getting |
86 | | -/// dropped, this BufReader operates in one-byte-increments. |
87 | | -struct BufReader<'a, R: Read> { |
88 | | - inner: &'a mut R, |
89 | | - buf: [u8; 1], |
90 | | - is_consumed: bool, |
91 | | -} |
92 | | - |
93 | | -impl<'a, R: Read> BufReader<'a, R> { |
94 | | - /// Creates a [`BufReader`] which will read from the given `inner`. |
95 | | - pub fn new(inner: &'a mut R) -> Self { |
96 | | - BufReader { inner, buf: [0; 1], is_consumed: true } |
97 | | - } |
98 | | -} |
99 | | - |
100 | | -impl<'a, R: Read> Read for BufReader<'a, R> { |
101 | | - #[inline] |
102 | | - fn read(&mut self, output: &mut [u8]) -> io::Result<usize> { |
103 | | - if output.is_empty() { |
104 | | - return Ok(0); |
105 | | - } |
106 | | - let mut offset = 0; |
107 | | - if !self.is_consumed { |
108 | | - output[0] = self.buf[0]; |
109 | | - self.is_consumed = true; |
110 | | - offset = 1; |
111 | | - } |
112 | | - self.inner.read(&mut output[offset..]).map(|len| len + offset) |
113 | | - } |
114 | | -} |
115 | | - |
116 | | -impl<'a, R: Read> BufRead for BufReader<'a, R> { |
117 | | - #[inline] |
118 | | - fn fill_buf(&mut self) -> io::Result<&[u8]> { |
119 | | - debug_assert!(false, "rust-bitcoin doesn't actually use this"); |
120 | | - if self.is_consumed { |
121 | | - let count = self.inner.read(&mut self.buf[..])?; |
122 | | - debug_assert!(count <= 1, "read gave us a garbage length"); |
123 | | - |
124 | | - // upon hitting EOF, assume the byte is already consumed |
125 | | - self.is_consumed = count == 0; |
126 | | - } |
127 | | - |
128 | | - if self.is_consumed { |
129 | | - Ok(&[]) |
130 | | - } else { |
131 | | - Ok(&self.buf[..]) |
132 | | - } |
133 | | - } |
134 | | - |
135 | | - #[inline] |
136 | | - fn consume(&mut self, amount: usize) { |
137 | | - debug_assert!(false, "rust-bitcoin doesn't actually use this"); |
138 | | - if amount >= 1 { |
139 | | - debug_assert_eq!(amount, 1, "Can only consume one byte"); |
140 | | - debug_assert!(!self.is_consumed, "Cannot consume more than had been read"); |
141 | | - self.is_consumed = true; |
142 | | - } |
143 | | - } |
144 | | -} |
145 | | - |
146 | 80 | pub(crate) struct WriterWriteAdaptor<'a, W: Writer + 'a>(pub &'a mut W); |
147 | 81 | impl<'a, W: Writer + 'a> Write for WriterWriteAdaptor<'a, W> { |
148 | 82 | #[inline] |
|
0 commit comments