interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / vendor / syn-2.0.119 / tests / repo / progress.rs

37 lines · 839 bytes Raw Blame History
1use std::io::{Read, Result};
2use std::time::{Duration, Instant};
3
4pub struct Progress<R> {
5 bytes: usize,
6 tick: Instant,
7 stream: R,
8}
9
10impl<R> Progress<R> {
11 pub fn new(stream: R) -> Self {
12 Progress {
13 bytes: 0,
14 tick: Instant::now() + Duration::from_secs(2),
15 stream,
16 }
17 }
18}
19
20impl<R: Read> Read for Progress<R> {
21 fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
22 let num = self.stream.read(buf)?;
23 self.bytes += num;
24 let now = Instant::now();
25 if now > self.tick {
26 self.tick = now + Duration::from_millis(500);
27 errorf!("downloading... {} bytes\n", self.bytes);
28 }
29 Ok(num)
30 }
31}
32
33impl<R> Drop for Progress<R> {
34 fn drop(&mut self) {
35 errorf!("done ({} bytes)\n", self.bytes);
36 }
37}