1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::path::PathBuf;
use std::io::Read;
use std::fs::File;
pub struct ReaderWithSize<R: Read> {
total_size: usize,
total_read: usize,
inner: R
}
impl<R: Read> ReaderWithSize<R> {
pub fn new(total_size: usize, inner: R) -> Self {
ReaderWithSize{ total_size, total_read: 0, inner }
}
pub fn total_read(&self) -> usize {
self.total_read
}
pub fn assummed_total_size(&self) -> usize {
self.total_size
}
pub fn fraction(&self) -> f64 {
(self.total_read as f64)/(self.total_size as f64)
}
pub fn into_inner(self) -> R {
self.inner
}
pub fn inner(&self) -> &R {
&self.inner
}
}
impl ReaderWithSize<File> {
pub fn from_path(path: impl Into<PathBuf>) -> Result<Self, std::io::Error> {
let path: PathBuf = path.into();
let file = File::open(path)?;
let size = file.metadata()?.len() as usize;
Ok(Self::new(size, file))
}
}
impl<R> Read for ReaderWithSize<R> where R: Read {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let result = self.inner.read(buf);
if let Ok(bytes_read) = result {
self.total_read += bytes_read;
}
result
}
}