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
use git_features::hash;
use git_object::HashKind;
use std::io;

pub(crate) struct Write<T> {
    pub hash: hash::Sha1,
    pub inner: T,
}

impl<T> io::Write for Write<T>
where
    T: io::Write,
{
    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
        self.hash.update(buf);
        self.inner.write(buf)
    }

    fn flush(&mut self) -> io::Result<()> {
        self.inner.flush()
    }
}

impl<T> Write<T>
where
    T: io::Write,
{
    pub fn new(inner: T, kind: HashKind) -> Self {
        match kind {
            HashKind::Sha1 => Write {
                inner,
                hash: hash::Sha1::default(),
            },
        }
    }
}