compression fix (#170)
* compression fix * use miniz_oxide * fix Error::Compression
This commit is contained in:
parent
2b0976132a
commit
ad80df5bbb
@ -63,7 +63,7 @@ postcard = { version = "0.5.1", features = ["alloc"] } # no_std compatible serde
|
|||||||
static_assertions = "1.1.0"
|
static_assertions = "1.1.0"
|
||||||
ctor = "0.1.20"
|
ctor = "0.1.20"
|
||||||
serde_json = { version = "1.0", optional = true, default-features = false, features = ["alloc"] } # an easy way to debug print SerdeAnyMap
|
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" }
|
core_affinity = { version = "0.5", git = "https://github.com/s1341/core_affinity_rs" }
|
||||||
num_enum = "0.5.1"
|
num_enum = "0.5.1"
|
||||||
hostname = "^0.3" # Is there really no gethostname in the stdlib?
|
hostname = "^0.3" # Is there really no gethostname in the stdlib?
|
||||||
|
@ -4,8 +4,10 @@
|
|||||||
#[cfg(feature = "llmp_compression")]
|
#[cfg(feature = "llmp_compression")]
|
||||||
use crate::Error;
|
use crate::Error;
|
||||||
use alloc::vec::Vec;
|
use alloc::vec::Vec;
|
||||||
use compression::prelude::*;
|
|
||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
|
use miniz_oxide::{
|
||||||
|
deflate::compress_to_vec, deflate::CompressionLevel, inflate::decompress_to_vec,
|
||||||
|
};
|
||||||
|
|
||||||
/// Compression for your stream compression needs.
|
/// Compression for your stream compression needs.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -30,11 +32,7 @@ impl GzipCompressor {
|
|||||||
pub fn compress(&self, buf: &[u8]) -> Result<Option<Vec<u8>>, Error> {
|
pub fn compress(&self, buf: &[u8]) -> Result<Option<Vec<u8>>, Error> {
|
||||||
if buf.len() >= self.threshold {
|
if buf.len() >= self.threshold {
|
||||||
//compress if the buffer is large enough
|
//compress if the buffer is large enough
|
||||||
let compressed = buf
|
let compressed = compress_to_vec(buf, CompressionLevel::BestSpeed as u8);
|
||||||
.iter()
|
|
||||||
.copied()
|
|
||||||
.encode(&mut GZipEncoder::new(), Action::Finish)
|
|
||||||
.collect::<Result<Vec<_>, _>>()?;
|
|
||||||
Ok(Some(compressed))
|
Ok(Some(compressed))
|
||||||
} else {
|
} else {
|
||||||
Ok(None)
|
Ok(None)
|
||||||
@ -45,11 +43,12 @@ impl GzipCompressor {
|
|||||||
/// Flag is used to indicate if it's compressed or not
|
/// Flag is used to indicate if it's compressed or not
|
||||||
#[allow(clippy::unused_self)]
|
#[allow(clippy::unused_self)]
|
||||||
pub fn decompress(&self, buf: &[u8]) -> Result<Vec<u8>, Error> {
|
pub fn decompress(&self, buf: &[u8]) -> Result<Vec<u8>, Error> {
|
||||||
Ok(buf
|
let decompressed = decompress_to_vec(buf);
|
||||||
.iter()
|
|
||||||
.copied()
|
match decompressed {
|
||||||
.decode(&mut GZipDecoder::new())
|
Ok(buf) => Ok(buf),
|
||||||
.collect::<Result<Vec<_>, _>>()?)
|
Err(_) => Err(Error::Compression),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ pub enum Error {
|
|||||||
Serialize(String),
|
Serialize(String),
|
||||||
/// Compression error
|
/// Compression error
|
||||||
#[cfg(feature = "llmp_compression")]
|
#[cfg(feature = "llmp_compression")]
|
||||||
Compression(String),
|
Compression,
|
||||||
/// File related error
|
/// File related error
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
File(io::Error),
|
File(io::Error),
|
||||||
@ -83,7 +83,7 @@ impl fmt::Display for Error {
|
|||||||
match self {
|
match self {
|
||||||
Self::Serialize(s) => write!(f, "Error in Serialization: `{0}`", &s),
|
Self::Serialize(s) => write!(f, "Error in Serialization: `{0}`", &s),
|
||||||
#[cfg(feature = "llmp_compression")]
|
#[cfg(feature = "llmp_compression")]
|
||||||
Self::Compression(s) => write!(f, "Error in Compression: `{0}`", &s),
|
Self::Compression => write!(f, "Error in decompression"),
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
Self::File(err) => write!(f, "File IO failed: {:?}", &err),
|
Self::File(err) => write!(f, "File IO failed: {:?}", &err),
|
||||||
Self::EmptyOptional(s) => write!(f, "Optional value `{0}` was not set", &s),
|
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
|
/// Stringify the json serializer error
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
impl From<serde_json::Error> for Error {
|
impl From<serde_json::Error> for Error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user