]> git.proxmox.com Git - rustc.git/blob - vendor/gix-odb/src/sink.rs
New upstream version 1.75.0+dfsg1
[rustc.git] / vendor / gix-odb / src / sink.rs
1 use std::{
2 cell::RefCell,
3 io::{self, Write},
4 };
5
6 use gix_features::zlib::stream::deflate;
7
8 use crate::Sink;
9
10 impl 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
22 impl crate::traits::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, crate::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 }