| 1 | //! Utilities to cause interruptions in common traits, like Read/Write and Iterator. |
| 2 | use std::{ |
| 3 | io, |
| 4 | sync::atomic::{AtomicBool, Ordering}, |
| 5 | }; |
| 6 | |
| 7 | /// A wrapper for an inner iterator which will check for interruptions on each iteration, stopping the iteration when |
| 8 | /// that is requested. |
| 9 | pub struct Iter<'a, I> { |
| 10 | /// The actual iterator to yield elements from. |
| 11 | pub inner: I, |
| 12 | should_interrupt: &'a AtomicBool, |
| 13 | } |
| 14 | |
| 15 | impl<'a, I> Iter<'a, I> |
| 16 | where |
| 17 | I: Iterator, |
| 18 | { |
| 19 | /// Create a new iterator over `inner` which checks for interruptions on each iteration on `should_interrupt`. |
| 20 | /// |
| 21 | /// Note that this means the consumer of the iterator data should also be able to access `should_interrupt` and |
| 22 | /// consider it when producing the final result to avoid claiming success even though the operation is only partially |
| 23 | /// complete. |
| 24 | pub fn new(inner: I, should_interrupt: &'a AtomicBool) -> Self { |
| 25 | Iter { |
| 26 | inner, |
| 27 | should_interrupt, |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl<I> Iterator for Iter<'_, I> |
| 33 | where |
| 34 | I: Iterator, |
| 35 | { |
| 36 | type Item = I::Item; |
| 37 | |
| 38 | fn next(&mut self) -> Option<Self::Item> { |
| 39 | if self.should_interrupt.load(Ordering::Relaxed) { |
| 40 | return None; |
| 41 | } |
| 42 | self.inner.next() |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// A wrapper for an inner iterator which will check for interruptions on each iteration. |
| 47 | pub struct IterWithErr<'a, I, EFN> { |
| 48 | /// The actual iterator to yield elements from. |
| 49 | pub inner: I, |
| 50 | make_err: Option<EFN>, |
| 51 | should_interrupt: &'a AtomicBool, |
| 52 | } |
| 53 | |
| 54 | impl<'a, I, EFN, E> IterWithErr<'a, I, EFN> |
| 55 | where |
| 56 | I: Iterator, |
| 57 | EFN: FnOnce() -> E, |
| 58 | { |
| 59 | /// Create a new iterator over `inner` which checks for interruptions on each iteration and calls `make_err()` to |
| 60 | /// signal an interruption happened, causing no further items to be iterated from that point on. |
| 61 | pub fn new(inner: I, make_err: EFN, should_interrupt: &'a AtomicBool) -> Self { |
| 62 | IterWithErr { |
| 63 | inner, |
| 64 | make_err: Some(make_err), |
| 65 | should_interrupt, |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl<I, EFN, E> Iterator for IterWithErr<'_, I, EFN> |
| 71 | where |
| 72 | I: Iterator, |
| 73 | EFN: FnOnce() -> E, |
| 74 | { |
| 75 | type Item = Result<I::Item, E>; |
| 76 | |
| 77 | fn next(&mut self) -> Option<Self::Item> { |
| 78 | self.make_err.as_ref()?; |
| 79 | if self.should_interrupt.load(Ordering::Relaxed) { |
| 80 | return self.make_err.take().map(|f| Err(f())); |
| 81 | } |
| 82 | match self.inner.next() { |
| 83 | Some(next) => Some(Ok(next)), |
| 84 | None => { |
| 85 | self.make_err = None; |
| 86 | None |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /// A wrapper for implementers of [`std::io::Read`] or [`std::io::BufRead`] with interrupt support. |
| 93 | /// |
| 94 | /// It fails a [read][std::io::Read::read] while an interrupt was requested. |
| 95 | pub struct Read<'a, R> { |
| 96 | /// The actual implementor of [`std::io::Read`] to which interrupt support will be added. |
| 97 | pub inner: R, |
| 98 | /// The flag to trigger interruption |
| 99 | pub should_interrupt: &'a AtomicBool, |
| 100 | } |
| 101 | |
| 102 | impl<R> io::Read for Read<'_, R> |
| 103 | where |
| 104 | R: io::Read, |
| 105 | { |
| 106 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
| 107 | if self.should_interrupt.load(Ordering::Relaxed) { |
| 108 | return Err(std::io::Error::other("Interrupted")); |
| 109 | } |
| 110 | self.inner.read(buf) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl<R> io::BufRead for Read<'_, R> |
| 115 | where |
| 116 | R: io::BufRead, |
| 117 | { |
| 118 | fn fill_buf(&mut self) -> io::Result<&[u8]> { |
| 119 | self.inner.fill_buf() |
| 120 | } |
| 121 | |
| 122 | fn consume(&mut self, amt: usize) { |
| 123 | self.inner.consume(amt); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | /// A wrapper for implementers of [`std::io::Write`] with interrupt checks on each write call. |
| 128 | /// |
| 129 | /// It fails a [write][std::io::Write::write] while an interrupt was requested. |
| 130 | pub struct Write<'a, W> { |
| 131 | /// The actual implementor of [`std::io::Write`] to which interrupt support will be added. |
| 132 | pub inner: W, |
| 133 | /// The flag to trigger interruption |
| 134 | pub should_interrupt: &'a AtomicBool, |
| 135 | } |
| 136 | |
| 137 | impl<W> io::Write for Write<'_, W> |
| 138 | where |
| 139 | W: std::io::Write, |
| 140 | { |
| 141 | fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 142 | if self.should_interrupt.load(Ordering::Relaxed) { |
| 143 | return Err(std::io::Error::other("Interrupted")); |
| 144 | } |
| 145 | self.inner.write(buf) |
| 146 | } |
| 147 | |
| 148 | fn flush(&mut self) -> io::Result<()> { |
| 149 | // Don't interrupt here, allow flushes to happen to prefer disk consistency. |
| 150 | self.inner.flush() |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl<W> io::Seek for Write<'_, W> |
| 155 | where |
| 156 | W: std::io::Seek, |
| 157 | { |
| 158 | fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> { |
| 159 | self.inner.seek(pos) |
| 160 | } |
| 161 | } |