Skip to content

Commit 45045dd

Browse files
committed
add Inspect to inspect written bytes on writers
1 parent bd6f46a commit 45045dd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

libsql-wal/src/io/mod.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,33 @@ impl<T: Io> Io for Arc<T> {
108108
self.as_ref().hard_link(src, dst)
109109
}
110110
}
111+
112+
pub struct Inspect<W, F> {
113+
inner: W,
114+
f: F,
115+
}
116+
117+
impl<W, F> Inspect<W, F> {
118+
pub fn new(inner: W, f: F) -> Self {
119+
Self { inner, f }
120+
}
121+
122+
pub(crate) fn into_inner(self) -> W {
123+
self.inner
124+
}
125+
}
126+
127+
impl<W, F> io::Write for Inspect<W, F>
128+
where
129+
W: io::Write,
130+
for<'a> F: FnMut(&'a [u8]),
131+
{
132+
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
133+
(self.f)(buf);
134+
self.inner.write(buf)
135+
}
136+
137+
fn flush(&mut self) -> io::Result<()> {
138+
self.inner.flush()
139+
}
140+
}

0 commit comments

Comments
 (0)