cursive_core/utils/reader.rs
1use crate::utils::Counter;
2use std::io::{self, Read};
3
4/// Wrapper around a `Read` that reports the progress made.
5///
6/// Used to monitor a file downloading or other slow IO task
7/// in a progress bar.
8///
9/// # Examples
10///
11/// ```rust,no_run
12/// use cursive_core::utils::{Counter, ProgressReader};
13/// use std::io::Read;
14///
15/// // Read a file and report the progress
16/// let file = std::fs::File::open("large_file").unwrap();
17/// let counter = Counter::new(0);
18/// let mut reader = ProgressReader::new(counter.clone(), file);
19///
20/// std::thread::spawn(move || {
21/// // Left as an exercise: use an AtomicBool for a stop condition!
22/// loop {
23/// let progress = counter.get();
24/// println!("Read {} bytes so far", progress);
25/// }
26/// });
27///
28/// // As we read data, the counter will be updated and the control thread
29/// // will monitor the progress.
30/// let mut buffer = Vec::new();
31/// reader.read_to_end(&mut buffer).unwrap();
32/// ```
33#[derive(Clone, Debug)]
34pub struct ProgressReader<R: Read> {
35 reader: R,
36 counter: Counter,
37}
38
39impl<R: Read> ProgressReader<R> {
40 /// Creates a new `ProgressReader` around `reader`.
41 ///
42 /// `counter` will be updated with the number of bytes read.
43 ///
44 /// You should make sure the progress bar knows how
45 /// many bytes should be received.
46 pub fn new(counter: Counter, reader: R) -> Self {
47 ProgressReader { reader, counter }
48 }
49
50 /// Unwraps this `ProgressReader`, returning the reader and counter.
51 pub fn deconstruct(self) -> (R, Counter) {
52 (self.reader, self.counter)
53 }
54}
55
56impl<R: Read> Read for ProgressReader<R> {
57 fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
58 let result = self.reader.read(buf)?;
59 self.counter.tick(result);
60 Ok(result)
61 }
62}