summaryrefslogtreecommitdiffstats
path: root/vendor/gix-features/src/interrupt.rs
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/gix-features/src/interrupt.rs')
-rw-r--r--vendor/gix-features/src/interrupt.rs38
1 files changed, 37 insertions, 1 deletions
diff --git a/vendor/gix-features/src/interrupt.rs b/vendor/gix-features/src/interrupt.rs
index 1f78e613a..dc7a2db17 100644
--- a/vendor/gix-features/src/interrupt.rs
+++ b/vendor/gix-features/src/interrupt.rs
@@ -91,7 +91,7 @@ where
/// A wrapper for implementors of [`std::io::Read`] or [`std::io::BufRead`] with interrupt support.
///
-/// It fails a [read][`std::io::Read::read`] while an interrupt was requested.
+/// It fails a [read][std::io::Read::read] while an interrupt was requested.
pub struct Read<'a, R> {
/// The actual implementor of [`std::io::Read`] to which interrupt support will be added.
pub inner: R,
@@ -123,3 +123,39 @@ where
self.inner.consume(amt)
}
}
+
+/// A wrapper for implementors of [`std::io::Write`] with interrupt checks on each write call.
+///
+/// It fails a [write][std::io::Write::write] while an interrupt was requested.
+pub struct Write<'a, W> {
+ /// The actual implementor of [`std::io::Write`] to which interrupt support will be added.
+ pub inner: W,
+ /// The flag to trigger interruption
+ pub should_interrupt: &'a AtomicBool,
+}
+
+impl<W> io::Write for Write<'_, W>
+where
+ W: std::io::Write,
+{
+ fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
+ if self.should_interrupt.load(Ordering::Relaxed) {
+ return Err(std::io::Error::new(std::io::ErrorKind::Other, "Interrupted"));
+ }
+ self.inner.write(buf)
+ }
+
+ fn flush(&mut self) -> io::Result<()> {
+ // Don't interrupt here, allow flushes to happen to prefer disk consistency.
+ self.inner.flush()
+ }
+}
+
+impl<W> io::Seek for Write<'_, W>
+where
+ W: std::io::Seek,
+{
+ fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> {
+ self.inner.seek(pos)
+ }
+}