this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor(progress): use inc-based ProgressReader for shared bars

+16 -6
+16 -6
src/progress.rs
··· 111 111 } 112 112 } 113 113 114 - /// A reader that tracks progress of bytes read 114 + /// A reader that tracks progress of bytes read. Multiple readers may share 115 + /// the same `ProgressBar` clone to drive a single bar across several input 116 + /// streams (container formats iterate over many files); the bar is advanced 117 + /// via `inc`, which is atomic and relative. 115 118 pub struct ProgressReader<R> { 116 119 inner: R, 117 120 bar: Option<ProgressBar>, 118 - total_read: u64, 119 121 last_update: Instant, 120 122 bytes_since_update: u64, 121 123 bytes_per_update: u64, ··· 126 128 ProgressReader { 127 129 inner, 128 130 bar, 129 - total_read: 0, 130 131 last_update: Instant::now(), 131 132 bytes_since_update: 0, 132 133 bytes_per_update: 8192, // Start with 8KB, will adjust dynamically ··· 144 145 let now = Instant::now(); 145 146 let elapsed = now.duration_since(self.last_update); 146 147 147 - // Update the progress 148 - bar.set_position(self.total_read); 148 + bar.inc(self.bytes_since_update); 149 149 150 150 // Adjust bytes_per_update to target ~100ms between updates 151 151 if elapsed < Duration::from_millis(50) { ··· 161 161 } 162 162 } 163 163 164 + impl<R> Drop for ProgressReader<R> { 165 + fn drop(&mut self) { 166 + if let Some(ref bar) = self.bar 167 + && self.bytes_since_update > 0 168 + { 169 + bar.inc(self.bytes_since_update); 170 + self.bytes_since_update = 0; 171 + } 172 + } 173 + } 174 + 164 175 impl<R: Read> Read for ProgressReader<R> { 165 176 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { 166 177 let bytes_read = self.inner.read(buf)?; 167 178 if bytes_read > 0 { 168 - self.total_read += bytes_read as u64; 169 179 self.maybe_update_progress(bytes_read as u64); 170 180 } 171 181 Ok(bytes_read)