| 1 | //! Utilities to cause interruptions in common traits, like Read/Write and Iterator. |
| 2 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{ |
| 3 | iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, |
| 4 | syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::{AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#, Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/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 Iterrust-analyzer cargo gix-features 0.48.0 interrupt/Iter#<'a, I> { |
| 10 | /// The actual iterator to yield elements from. |
| 11 | pub innerrust-analyzer cargo gix-features 0.48.0 interrupt/Iter#inner.: I, |
| 12 | should_interruptrust-analyzer cargo gix-features 0.48.0 interrupt/Iter#should_interrupt.: &'a AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#, |
| 13 | } |
| 14 | |
| 15 | impl<'a, I> Iterrust-analyzer cargo gix-features 0.48.0 interrupt/Iter#<'a, I> |
| 16 | where |
| 17 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/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 newrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Iter<'a, I>`]new().(inner: I, should_interrupt: &'a AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#) -> Selfrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Iter<'a, I>`] { |
| 25 | Iterrust-analyzer cargo gix-features 0.48.0 interrupt/Iter# { |
| 26 | innerrust-analyzer cargo gix-features 0.48.0 interrupt/Iter#inner., |
| 27 | should_interruptrust-analyzer cargo gix-features 0.48.0 interrupt/Iter#should_interrupt., |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | impl<I> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for Iterrust-analyzer cargo gix-features 0.48.0 interrupt/Iter#<'_, I> |
| 33 | where |
| 34 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 35 | { |
| 36 | type Itemrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Iter<'_, I>`][Iterator]Item# = I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#; |
| 37 | |
| 38 | fn nextrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Iter<'_, I>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Iter<'_, I>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 39 | if self.should_interruptrust-analyzer cargo gix-features 0.48.0 interrupt/Iter#should_interrupt..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#) { |
| 40 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 41 | } |
| 42 | self.innerrust-analyzer cargo gix-features 0.48.0 interrupt/Iter#inner..nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// A wrapper for an inner iterator which will check for interruptions on each iteration. |
| 47 | pub struct IterWithErrrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#<'a, I, EFN> { |
| 48 | /// The actual iterator to yield elements from. |
| 49 | pub innerrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#inner.: I, |
| 50 | make_errrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#make_err.: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<EFN>, |
| 51 | should_interruptrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#should_interrupt.: &'a AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#, |
| 52 | } |
| 53 | |
| 54 | impl<'a, I, EFN, E> IterWithErrrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#<'a, I, EFN> |
| 55 | where |
| 56 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 57 | EFN: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/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 newrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`IterWithErr<'a, I, EFN>`]new().(inner: I, make_err: EFN, should_interrupt: &'a AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#) -> Selfrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`IterWithErr<'a, I, EFN>`] { |
| 62 | IterWithErrrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr# { |
| 63 | innerrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#inner., |
| 64 | make_errrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#make_err.: Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(make_err), |
| 65 | should_interruptrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#should_interrupt., |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | impl<I, EFN, E> Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for IterWithErrrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#<'_, I, EFN> |
| 71 | where |
| 72 | I: Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#, |
| 73 | EFN: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#() -> E, |
| 74 | { |
| 75 | type Itemrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`IterWithErr<'_, I, EFN>`][Iterator]Item# = Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<I::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#, E>; |
| 76 | |
| 77 | fn nextrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`IterWithErr<'_, I, EFN>`][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`IterWithErr<'_, I, EFN>`][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 78 | self.make_errrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#make_err..as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]as_ref().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 79 | if self.should_interruptrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#should_interrupt..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#) { |
| 80 | return self.make_errrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#make_err..takerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]take().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(|f| Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(f())); |
| 81 | } |
| 82 | match self.innerrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#inner..nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#next().() { |
| 83 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(next) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(next)), |
| 84 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# => { |
| 85 | self.make_errrust-analyzer cargo gix-features 0.48.0 interrupt/IterWithErr#make_err. = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 86 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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 Readrust-analyzer cargo gix-features 0.48.0 interrupt/Read#<'a, R> { |
| 96 | /// The actual implementor of [`std::io::Read`] to which interrupt support will be added. |
| 97 | pub innerrust-analyzer cargo gix-features 0.48.0 interrupt/Read#inner.: R, |
| 98 | /// The flag to trigger interruption |
| 99 | pub should_interruptrust-analyzer cargo gix-features 0.48.0 interrupt/Read#should_interrupt.: &'a AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#, |
| 100 | } |
| 101 | |
| 102 | impl<R> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# for Readrust-analyzer cargo gix-features 0.48.0 interrupt/Read#<'_, R> |
| 103 | where |
| 104 | R: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, |
| 105 | { |
| 106 | fn readrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Read<'_, R>`][Read]read().(&mut self, buf: &mut [u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 107 | if self.should_interruptrust-analyzer cargo gix-features 0.48.0 interrupt/Read#should_interrupt..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#) { |
| 108 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]other().("Interrupted")); |
| 109 | } |
| 110 | self.innerrust-analyzer cargo gix-features 0.48.0 interrupt/Read#inner..readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read().(buf) |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl<R> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead# for Readrust-analyzer cargo gix-features 0.48.0 interrupt/Read#<'_, R> |
| 115 | where |
| 116 | R: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#, |
| 117 | { |
| 118 | fn fill_bufrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Read<'_, R>`][BufRead]fill_buf().(&mut self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<&[u8]> { |
| 119 | self.innerrust-analyzer cargo gix-features 0.48.0 interrupt/Read#inner..fill_bufrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#fill_buf().() |
| 120 | } |
| 121 | |
| 122 | fn consumerust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Read<'_, R>`][BufRead]consume().(&mut self, amt: usize) { |
| 123 | self.innerrust-analyzer cargo gix-features 0.48.0 interrupt/Read#inner..consumerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead#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 Writerust-analyzer cargo gix-features 0.48.0 interrupt/Write#<'a, W> { |
| 131 | /// The actual implementor of [`std::io::Write`] to which interrupt support will be added. |
| 132 | pub innerrust-analyzer cargo gix-features 0.48.0 interrupt/Write#inner.: W, |
| 133 | /// The flag to trigger interruption |
| 134 | pub should_interruptrust-analyzer cargo gix-features 0.48.0 interrupt/Write#should_interrupt.: &'a AtomicBoolrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/AtomicBool#, |
| 135 | } |
| 136 | |
| 137 | impl<W> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# for Writerust-analyzer cargo gix-features 0.48.0 interrupt/Write#<'_, W> |
| 138 | where |
| 139 | W: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#, |
| 140 | { |
| 141 | fn writerust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Write<'_, W>`][Write]write().(&mut self, buf: &[u8]) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 142 | if self.should_interruptrust-analyzer cargo gix-features 0.48.0 interrupt/Write#should_interrupt..loadrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/impl#[AtomicBool]load().(Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::Relaxedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#Relaxed#) { |
| 143 | return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::otherrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]other().("Interrupted")); |
| 144 | } |
| 145 | self.innerrust-analyzer cargo gix-features 0.48.0 interrupt/Write#inner..writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write().(buf) |
| 146 | } |
| 147 | |
| 148 | fn flushrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Write<'_, W>`][Write]flush().(&mut self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 149 | // Don't interrupt here, allow flushes to happen to prefer disk consistency. |
| 150 | self.innerrust-analyzer cargo gix-features 0.48.0 interrupt/Write#inner..flushrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#flush().() |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | impl<W> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek# for Writerust-analyzer cargo gix-features 0.48.0 interrupt/Write#<'_, W> |
| 155 | where |
| 156 | W: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, |
| 157 | { |
| 158 | fn seekrust-analyzer cargo gix-features 0.48.0 interrupt/impl#[`Write<'_, W>`][Seek]seek().(&mut self, pos: iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<u64> { |
| 159 | self.innerrust-analyzer cargo gix-features 0.48.0 interrupt/Write#inner..seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#seek().(pos) |
| 160 | } |
| 161 | } |