gix_odb/
sink.rs

1use std::{
2    cell::RefCell,
3    io::{self, Write},
4};
5
6use gix_features::zlib::stream::deflate;
7
8use crate::Sink;
9
10impl Sink {
11    /// Enable or disable compression. Compression is disabled by default
12    pub fn compress(mut self, enable: bool) -> Self {
13        if enable {
14            self.compressor = Some(RefCell::new(deflate::Write::new(io::sink())));
15        } else {
16            self.compressor = None;
17        }
18        self
19    }
20}
21
22impl gix_object::Write for Sink {
23    fn write_stream(
24        &self,
25        kind: gix_object::Kind,
26        mut size: u64,
27        from: &mut dyn io::Read,
28    ) -> Result<gix_hash::ObjectId, gix_object::write::Error> {
29        let mut buf = [0u8; u16::MAX as usize];
30        let header = gix_object::encode::loose_header(kind, size);
31
32        let possibly_compress = |buf: &[u8]| -> io::Result<()> {
33            if let Some(compressor) = self.compressor.as_ref() {
34                compressor.try_borrow_mut().expect("no recursion").write_all(buf)?;
35            }
36            Ok(())
37        };
38
39        let mut hasher = gix_features::hash::hasher(self.object_hash);
40        hasher.update(&header);
41        possibly_compress(&header).map_err(Box::new)?;
42
43        while size != 0 {
44            let bytes = (size as usize).min(buf.len());
45            from.read_exact(&mut buf[..bytes]).map_err(Box::new)?;
46            hasher.update(&buf[..bytes]);
47            possibly_compress(&buf[..bytes]).map_err(Box::new)?;
48            size -= bytes as u64;
49        }
50        if let Some(compressor) = self.compressor.as_ref() {
51            let mut c = compressor.borrow_mut();
52            c.flush().map_err(Box::new)?;
53            c.reset();
54        }
55
56        Ok(hasher.digest().into())
57    }
58}