compression fix (#170)

* compression fix

* use miniz_oxide

* fix Error::Compression
This commit is contained in:
Toka 2021-06-14 18:06:53 +09:00 committed by GitHub
parent 2b0976132a
commit ad80df5bbb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 21 deletions

View File

@ -63,7 +63,7 @@ postcard = { version = "0.5.1", features = ["alloc"] } # no_std compatible serde
static_assertions = "1.1.0"
ctor = "0.1.20"
serde_json = { version = "1.0", optional = true, default-features = false, features = ["alloc"] } # an easy way to debug print SerdeAnyMap
compression = { version = "0.1.5" }
miniz_oxide = "0.4.4"
core_affinity = { version = "0.5", git = "https://github.com/s1341/core_affinity_rs" }
num_enum = "0.5.1"
hostname = "^0.3" # Is there really no gethostname in the stdlib?

View File

@ -4,8 +4,10 @@
#[cfg(feature = "llmp_compression")]
use crate::Error;
use alloc::vec::Vec;
use compression::prelude::*;
use core::fmt::Debug;
use miniz_oxide::{
deflate::compress_to_vec, deflate::CompressionLevel, inflate::decompress_to_vec,
};
/// Compression for your stream compression needs.
#[derive(Debug)]
@ -30,11 +32,7 @@ impl GzipCompressor {
pub fn compress(&self, buf: &[u8]) -> Result<Option<Vec<u8>>, Error> {
if buf.len() >= self.threshold {
//compress if the buffer is large enough
let compressed = buf
.iter()
.copied()
.encode(&mut GZipEncoder::new(), Action::Finish)
.collect::<Result<Vec<_>, _>>()?;
let compressed = compress_to_vec(buf, CompressionLevel::BestSpeed as u8);
Ok(Some(compressed))
} else {
Ok(None)
@ -45,11 +43,12 @@ impl GzipCompressor {
/// Flag is used to indicate if it's compressed or not
#[allow(clippy::unused_self)]
pub fn decompress(&self, buf: &[u8]) -> Result<Vec<u8>, Error> {
Ok(buf
.iter()
.copied()
.decode(&mut GZipDecoder::new())
.collect::<Result<Vec<_>, _>>()?)
let decompressed = decompress_to_vec(buf);
match decompressed {
Ok(buf) => Ok(buf),
Err(_) => Err(Error::Compression),
}
}
}

View File

@ -52,7 +52,7 @@ pub enum Error {
Serialize(String),
/// Compression error
#[cfg(feature = "llmp_compression")]
Compression(String),
Compression,
/// File related error
#[cfg(feature = "std")]
File(io::Error),
@ -83,7 +83,7 @@ impl fmt::Display for Error {
match self {
Self::Serialize(s) => write!(f, "Error in Serialization: `{0}`", &s),
#[cfg(feature = "llmp_compression")]
Self::Compression(s) => write!(f, "Error in Compression: `{0}`", &s),
Self::Compression => write!(f, "Error in decompression"),
#[cfg(feature = "std")]
Self::File(err) => write!(f, "File IO failed: {:?}", &err),
Self::EmptyOptional(s) => write!(f, "Optional value `{0}` was not set", &s),
@ -109,13 +109,6 @@ impl From<postcard::Error> for Error {
}
}
#[cfg(feature = "llmp_compression")]
impl From<compression::prelude::CompressionError> for Error {
fn from(err: compression::prelude::CompressionError) -> Self {
Self::Compression(format!("{:?}", err))
}
}
/// Stringify the json serializer error
#[cfg(feature = "std")]
impl From<serde_json::Error> for Error {