]>
git.proxmox.com Git - rustc.git/blob - vendor/gix-odb/src/sink.rs
6 use gix_features
::zlib
::stream
::deflate
;
11 /// Enable or disable compression. Compression is disabled by default
12 pub fn compress(mut self, enable
: bool
) -> Self {
14 self.compressor
= Some(RefCell
::new(deflate
::Write
::new(io
::sink())));
16 self.compressor
= None
;
22 impl crate::traits
::Write
for Sink
{
25 kind
: gix_object
::Kind
,
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
);
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
)?
;
39 let mut hasher
= gix_features
::hash
::hasher(self.object_hash
);
40 hasher
.update(&header
);
41 possibly_compress(&header
).map_err(Box
::new
)?
;
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
)?
;
50 if let Some(compressor
) = self.compressor
.as_ref() {
51 let mut c
= compressor
.borrow_mut();
52 c
.flush().map_err(Box
::new
)?
;
56 Ok(hasher
.digest().into())