Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

factor out codecs and core traits #325

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 34 additions & 21 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
[package]
name = "async-compression"
version = "0.4.18"
[workspace]
members = ["crates/*"]
resolver = "2"

[workspace.package]
authors = ["Wim Looman <wim@nemo157.com>", "Allen Bui <fairingrey@gmail.com>"]
edition = "2018"
license = "MIT OR Apache-2.0"
keywords = ["compression", "gzip", "zstd", "brotli", "async"]
categories = ["compression", "asynchronous"]
repository = "https://github.com/Nullus157/async-compression"

[workspace.dependencies]
compression-codecs = { version = "0.1.0", path = "crates/compression-codecs" }
compression-core = { version = "0.1.0", path = "crates/compression-core" }

[package]
name = "async-compression"
version = "0.4.18"
description = """
Adaptors between compression crates and Rust's modern asynchronous IO types.
"""
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
keywords = ["compression", "gzip", "zstd", "brotli", "async"]
categories = ["compression", "asynchronous"]

[package.metadata.docs.rs]
all-features = true
Expand All @@ -22,28 +36,27 @@ all-implementations = ["futures-io", "tokio"]
all-algorithms = ["brotli", "bzip2", "deflate", "gzip", "lzma", "xz", "zlib", "zstd", "deflate64"]

# algorithms
deflate = ["flate2"]
gzip = ["flate2"]
lzma = ["xz2"]
xz = ["xz2"]
zlib = ["flate2"]
zstd = ["libzstd", "zstd-safe"]
zstdmt = ["zstd", "zstd-safe/zstdmt"]
deflate64 = ["dep:deflate64"]
brotli = ["compression-codecs/brotli"]
bzip2 = ["compression-codecs/bzip2"]
deflate = ["compression-codecs/deflate", "flate2"]
deflate64 = ["compression-codecs/deflate64"]
flate2 = ["compression-codecs/flate2"]
gzip = ["compression-codecs/gzip", "flate2"]
lzma = ["compression-codecs/lzma", "xz2"]
xz = ["compression-codecs/xz", "xz2"]
xz2 = ["compression-codecs/xz2"]
zlib = ["compression-codecs/zlib", "flate2"]
zstd = ["compression-codecs/zstd"]
zstdmt = ["compression-codecs/zstdmt"]
libzstd = ["compression-codecs/libzstd"]
zstd-safe = ["compression-codecs/zstd-safe"]

[dependencies]
brotli = { version = "7.0", optional = true }
bzip2 = { version = "0.5.0", optional = true }
flate2 = { version = "1.0.13", optional = true }
futures-core = { version = "0.3", default-features = false }
futures-io = { version = "0.3", default-features = false, features = ["std"], optional = true }
libzstd = { package = "zstd", version = "0.13.1", optional = true, default-features = false }
memchr = "2"
pin-project-lite = "0.2"
tokio = { version = "1.24.2", optional = true, default-features = false }
xz2 = { version = "0.1.6", optional = true }
zstd-safe = { version = "7", optional = true, default-features = false }
deflate64 = { version = "0.1.5", optional = true }
compression-codecs.workspace = true

[dev-dependencies]
bytes = "1"
Expand Down
39 changes: 39 additions & 0 deletions crates/compression-codecs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
[package]
name = "compression-codecs"
version = "0.1.0"
description = """
Adaptors for various compression algorithms.
"""
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
keywords = ["compression", "gzip", "zstd", "brotli"]
categories = ["compression"]

[dependencies]
# Workspace dependencies.
compression-core.workspace = true

# Compression algorithms.
brotli = { version = "7.0", optional = true }
bzip2 = { version = "0.5.0", optional = true }
deflate64 = { version = "0.1.5", optional = true }
flate2 = { version = "1.0.13", optional = true }
libzstd = { package = "zstd", version = "0.13.1", optional = true, default-features = false }
memchr = "2"
xz2 = { version = "0.1.6", optional = true }
zstd-safe = { version = "7", optional = true, default-features = false }

[features]
all = ["brotli", "bzip2", "deflate", "gzip", "lzma", "xz", "zlib", "zstd", "deflate64"]

# Individual algorithms.
deflate = ["flate2"]
deflate64 = ["dep:deflate64"]
gzip = ["flate2"]
lzma = ["xz2"]
xz = ["xz2"]
zlib = ["flate2"]
zstd = ["libzstd", "zstd-safe"]
zstdmt = ["zstd", "zstd-safe/zstdmt"]
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{codec::Decode, util::PartialBuffer};
use crate::{Decode, PartialBuffer};
use std::{fmt, io};

use brotli::{enc::StandardAlloc, BrotliDecompressStream, BrotliResult, BrotliState};
Expand All @@ -9,7 +9,7 @@ pub struct BrotliDecoder {
}

impl BrotliDecoder {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Self {
state: Box::new(BrotliState::new(
StandardAlloc::default(),
Expand All @@ -25,7 +25,7 @@ impl BrotliDecoder {
output: &mut PartialBuffer<impl AsRef<[u8]> + AsMut<[u8]>>,
) -> io::Result<BrotliResult> {
let in_buf = input.unwritten();
let mut out_buf = output.unwritten_mut();
let out_buf = output.unwritten_mut();

let mut input_len = 0;
let mut output_len = 0;
Expand Down Expand Up @@ -53,6 +53,12 @@ impl BrotliDecoder {
}
}

impl Default for BrotliDecoder {
fn default() -> Self {
Self::new()
}
}

impl Decode for BrotliDecoder {
fn reinit(&mut self) -> io::Result<()> {
self.state = Box::new(BrotliState::new(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{codec::Encode, util::PartialBuffer};
use crate::{Encode, PartialBuffer};
use std::{fmt, io};

use brotli::enc::{
Expand All @@ -12,7 +12,7 @@ pub struct BrotliEncoder {
}

impl BrotliEncoder {
pub(crate) fn new(params: BrotliEncoderParams) -> Self {
pub fn new(params: BrotliEncoderParams) -> Self {
let mut state = BrotliEncoderStateStruct::new(StandardAlloc::default());
state.params = params;
Self { state }
Expand All @@ -25,7 +25,7 @@ impl BrotliEncoder {
op: BrotliEncoderOperation,
) -> io::Result<()> {
let in_buf = input.unwritten();
let mut out_buf = output.unwritten_mut();
let out_buf = output.unwritten_mut();

let mut input_len = 0;
let mut output_len = 0;
Expand Down
6 changes: 6 additions & 0 deletions crates/compression-codecs/src/brotli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub use brotli;

mod decoder;
mod encoder;

pub use self::{decoder::BrotliDecoder, encoder::BrotliEncoder};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{codec::Decode, util::PartialBuffer};
use crate::{Decode, PartialBuffer};
use std::{fmt, io};

use bzip2::{Decompress, Status};
Expand All @@ -19,7 +19,7 @@ impl fmt::Debug for BzDecoder {
}

impl BzDecoder {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Self {
decompress: Decompress::new(false),
}
Expand All @@ -45,6 +45,12 @@ impl BzDecoder {
}
}

impl Default for BzDecoder {
fn default() -> Self {
Self::new()
}
}

impl Decode for BzDecoder {
fn reinit(&mut self) -> io::Result<()> {
self.decompress = Decompress::new(false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{codec::Encode, util::PartialBuffer};
use crate::{Encode, PartialBuffer};
use std::{fmt, io};

use bzip2::{Action, Compress, Compression, Status};
Expand Down Expand Up @@ -39,7 +39,7 @@ impl BzEncoder {
///
/// Allowable values range from 0 to 250 inclusive. 0 is a special case,
/// equivalent to using the default value of 30.
pub(crate) fn new(level: Compression, work_factor: u32) -> Self {
pub fn new(level: Compression, work_factor: u32) -> Self {
Self {
compress: Compress::new(level, work_factor),
}
Expand Down
6 changes: 6 additions & 0 deletions crates/compression-codecs/src/bzip2/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub use bzip2;

mod decoder;
mod encoder;

pub use self::{decoder::BzDecoder, encoder::BzEncoder};
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
use crate::util::PartialBuffer;
use compression_core::Decode;

use crate::{flate::FlateDecoder, PartialBuffer};
use std::io::Result;

#[derive(Debug)]
pub struct DeflateDecoder {
inner: crate::codec::FlateDecoder,
inner: FlateDecoder,
}

impl DeflateDecoder {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Self {
inner: crate::codec::FlateDecoder::new(false),
inner: FlateDecoder::new(false),
}
}
}

impl crate::codec::Decode for DeflateDecoder {
impl Default for DeflateDecoder {
fn default() -> Self {
Self::new()
}
}

impl Decode for DeflateDecoder {
fn reinit(&mut self) -> Result<()> {
self.inner.reinit()?;
Ok(())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
use crate::{codec::Encode, util::PartialBuffer};
use crate::{flate::FlateEncoder, Encode, PartialBuffer};
use std::io::Result;

use flate2::Compression;

#[derive(Debug)]
pub struct DeflateEncoder {
inner: crate::codec::FlateEncoder,
inner: FlateEncoder,
}

impl DeflateEncoder {
pub(crate) fn new(level: Compression) -> Self {
pub fn new(level: Compression) -> Self {
Self {
inner: crate::codec::FlateEncoder::new(level, false),
inner: FlateEncoder::new(level, false),
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions crates/compression-codecs/src/deflate/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mod decoder;
mod encoder;

pub use self::{decoder::DeflateDecoder, encoder::DeflateEncoder};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{codec::Decode, util::PartialBuffer};
use crate::{Decode, PartialBuffer};
use std::io::{Error, ErrorKind, Result};

use deflate64::InflaterManaged;
Expand All @@ -9,7 +9,7 @@ pub struct Deflate64Decoder {
}

impl Deflate64Decoder {
pub(crate) fn new() -> Self {
pub fn new() -> Self {
Self {
inflater: Box::new(InflaterManaged::new()),
}
Expand All @@ -35,6 +35,12 @@ impl Deflate64Decoder {
}
}

impl Default for Deflate64Decoder {
fn default() -> Self {
Self::new()
}
}

impl Decode for Deflate64Decoder {
fn reinit(&mut self) -> Result<()> {
self.inflater = Box::new(InflaterManaged::new());
Expand Down
3 changes: 3 additions & 0 deletions crates/compression-codecs/src/deflate64/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mod decoder;

pub use self::decoder::Deflate64Decoder;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{codec::Decode, util::PartialBuffer};
use crate::{Decode, PartialBuffer};
use std::io;

use flate2::{Decompress, FlushDecompress, Status};
Expand All @@ -10,7 +10,7 @@ pub struct FlateDecoder {
}

impl FlateDecoder {
pub(crate) fn new(zlib_header: bool) -> Self {
pub fn new(zlib_header: bool) -> Self {
Self {
zlib_header,
decompress: Decompress::new(zlib_header),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{codec::Encode, util::PartialBuffer};
use crate::{Encode, PartialBuffer};
use std::io;

use flate2::{Compress, Compression, FlushCompress, Status};
Expand All @@ -10,14 +10,14 @@ pub struct FlateEncoder {
}

impl FlateEncoder {
pub(crate) fn new(level: Compression, zlib_header: bool) -> Self {
pub fn new(level: Compression, zlib_header: bool) -> Self {
Self {
compress: Compress::new(level, zlib_header),
flushed: true,
}
}

pub(crate) fn get_ref(&self) -> &Compress {
pub fn get_ref(&self) -> &Compress {
&self.compress
}

Expand Down
6 changes: 6 additions & 0 deletions crates/compression-codecs/src/flate/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub use flate2;

mod decoder;
mod encoder;

pub use self::{decoder::FlateDecoder, encoder::FlateEncoder};
Loading
Loading