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

zstd-sys: impl wasm32-unknown-unknown support #139

Merged
merged 6 commits into from
Jan 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ readme = "Readme.md"
edition = "2018"

[package.metadata.docs.rs]
features = ["experimental", "zstdmt", "doc-cfg"]
features = ["experimental", "zstdmt", "zdict_builder", "doc-cfg"]

[badges]
travis-ci = { repository = "gyscos/zstd-rs" }
Expand All @@ -28,7 +28,7 @@ partial-io = "0.5"
walkdir = "2.2"

[features]
default = ["legacy", "arrays"]
default = ["legacy", "arrays", "zdict_builder"]

bindgen = ["zstd-safe/bindgen"]
debug = ["zstd-safe/debug"]
Expand All @@ -41,3 +41,4 @@ thin = ["zstd-safe/thin"]
arrays = ["zstd-safe/arrays"]
no_asm = ["zstd-safe/no_asm"]
doc-cfg = []
zdict_builder = ["zstd-safe/zdict_builder"]
9 changes: 6 additions & 3 deletions examples/train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ fn main() {

let files: Vec<_> = matches.values_of("FILE").unwrap().collect();

let dict = zstd::dict::from_files(&files, size).unwrap();
#[cfg(feature = "zdict_builder")]
{
let dict = zstd::dict::from_files(&files, size).unwrap();

let mut dict_reader: &[u8] = &dict;
io::copy(&mut dict_reader, &mut io::stdout()).unwrap();
let mut dict_reader: &[u8] = &dict;
io::copy(&mut dict_reader, &mut io::stdout()).unwrap();
}
Copy link
Owner

@gyscos gyscos Jan 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can specify that an example requires some features. In Cargo.toml:

[[example]]
name = "train"
required-features = ["zdict_builder"]

This way, in the example itself, you can just assume the feature is set.

}
15 changes: 10 additions & 5 deletions src/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@
//! [`Encoder::with_dictionary`]: ../struct.Encoder.html#method.with_dictionary
//! [`Decoder::with_dictionary`]: ../struct.Decoder.html#method.with_dictionary

use crate::map_error_code;
use std::fs;

#[cfg(feature = "zdict")]
use std::io::{self, Read};
use std::path;

pub use zstd_safe::{CDict, DDict};

Expand Down Expand Up @@ -99,11 +96,14 @@ impl<'a> DecoderDictionary<'a> {
///
/// This is the most efficient way to train a dictionary,
/// since this is directly fed into `zstd`.
#[cfg(feature = "zdict")]
pub fn from_continuous(
sample_data: &[u8],
sample_sizes: &[usize],
max_size: usize,
) -> io::Result<Vec<u8>> {
use crate::map_error_code;

// Complain if the lengths don't add up to the entire data.
if sample_sizes.iter().sum::<usize>() != sample_data.len() {
return Err(io::Error::new(
Expand All @@ -127,6 +127,7 @@ pub fn from_continuous(
/// [`from_continuous`] directly uses the given slice.
///
/// [`from_continuous`]: ./fn.from_continuous.html
#[cfg(feature = "zdict")]
pub fn from_samples<S: AsRef<[u8]>>(
samples: &[S],
max_size: usize,
Expand All @@ -140,11 +141,14 @@ pub fn from_samples<S: AsRef<[u8]>>(
}

/// Train a dict from a list of files.
#[cfg(feature = "zdict")]
pub fn from_files<I, P>(filenames: I, max_size: usize) -> io::Result<Vec<u8>>
where
P: AsRef<path::Path>,
P: AsRef<std::path::Path>,
I: IntoIterator<Item = P>,
{
use std::fs;

let mut buffer = Vec::new();
let mut sizes = Vec::new();

Expand All @@ -158,6 +162,7 @@ where
}

#[cfg(test)]
#[cfg(feature = "zdict")]
mod tests {
use std::fs;
use std::io;
Expand Down
5 changes: 3 additions & 2 deletions zstd-safe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ readme = "Readme.md"
edition = "2018"

[package.metadata.docs.rs]
features = ["experimental", "arrays", "std", "doc-cfg"]
features = ["experimental", "arrays", "std", "zdict_builder", "doc-cfg"]

[dependencies]
zstd-sys = { path = "zstd-sys", version = "=1.6.3", default-features = false }
libc = "0.2.21"

[features]
default = ["legacy", "arrays"]
default = ["legacy", "arrays", "zdict_builder"]

bindgen = ["zstd-sys/bindgen"]
debug = ["zstd-sys/debug"]
Expand All @@ -32,3 +32,4 @@ thin = ["zstd-sys/thin"]
arrays = []
no_asm = ["zstd-sys/no_asm"]
doc-cfg = []
zdict_builder = ["zstd-sys/zdict_builder"]
2 changes: 2 additions & 0 deletions zstd-safe/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2041,6 +2041,7 @@ pub fn cctx_set_pledged_src_size(
}

/// Wraps the `ZDICT_trainFromBuffer()` function.
#[cfg(feature = "zdict_builder")]
pub fn train_from_buffer<C: WriteBuf + ?Sized>(
dict_buffer: &mut C,
samples_buffer: &[u8],
Expand All @@ -2062,6 +2063,7 @@ pub fn train_from_buffer<C: WriteBuf + ?Sized>(
}

/// Wraps the `ZSTD_getDictID_fromDict()` function.
#[cfg(feature = "zdict_builder")]
pub fn get_dict_id(dict_buffer: &[u8]) -> Option<u32> {
let id = unsafe {
zstd_sys::ZDICT_getDictID(ptr_void(dict_buffer), dict_buffer.len())
Expand Down
4 changes: 2 additions & 2 deletions zstd-safe/zstd-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ features = ["parallel"]
libc = "0.2.45"

[features]
default = ["legacy", "zdict"]
default = ["legacy", "zdict_builder"]

debug = [] # Enable zstd debug logs
experimental = [] # Expose experimental ZSTD API
Expand All @@ -71,4 +71,4 @@ std = [] # Use std types instead of libc in bindgen
zstdmt = [] # Enable multi-thread support (with pthread)
thin = [] # Optimize binary by size
no_asm = [] # Disable ASM files (only on amd64 for decompression)
zdict = []
zdict_builder = []
10 changes: 5 additions & 5 deletions zstd-safe/zstd-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{env, fs};
fn generate_bindings(defs: Vec<&str>, headerpaths: Vec<PathBuf>) {
let bindings = bindgen::Builder::default()
.header("zstd.h");
#[cfg(feature = "zdict")]
#[cfg(feature = "zdict_builder")]
let bindings = bindings.header("zdict.h");
let bindings = bindings
.blocklist_type("max_align_t")
Expand All @@ -22,7 +22,7 @@ fn generate_bindings(defs: Vec<&str>, headerpaths: Vec<PathBuf>) {

#[cfg(feature = "experimental")]
let bindings = bindings.clang_arg("-DZSTD_STATIC_LINKING_ONLY");
#[cfg(all(feature = "experimental", feature = "zdict"))]
#[cfg(all(feature = "experimental", feature = "zdict_builder"))]
let bindings = bindings.clang_arg("-DZDICT_STATIC_LINKING_ONLY");

#[cfg(not(feature = "std"))]
Expand Down Expand Up @@ -87,7 +87,7 @@ fn compile_zstd() {
"zstd/lib/common",
"zstd/lib/compress",
"zstd/lib/decompress",
#[cfg(feature = "zdict")]
#[cfg(feature = "zdict_builder")]
"zstd/lib/dictBuilder",
#[cfg(feature = "legacy")]
"zstd/lib/legacy",
Expand Down Expand Up @@ -150,7 +150,7 @@ fn compile_zstd() {
config.flag("-fvisibility=hidden");
config.define("XXH_PRIVATE_API", Some(""));
config.define("ZSTDLIB_VISIBILITY", Some(""));
#[cfg(feature = "zdict")]
#[cfg(feature = "zdict_builder")]
config.define("ZDICTLIB_VISIBILITY", Some(""));
config.define("ZSTDERRORLIB_VISIBILITY", Some(""));

Expand Down Expand Up @@ -184,7 +184,7 @@ fn compile_zstd() {
fs::copy(src.join("zstd.h"), include.join("zstd.h")).unwrap();
fs::copy(src.join("zstd_errors.h"), include.join("zstd_errors.h"))
.unwrap();
#[cfg(feature = "zdict")]
#[cfg(feature = "zdict_builder")]
fs::copy(src.join("zdict.h"), include.join("zdict.h")).unwrap();
println!("cargo:root={}", dst.display());
}
Expand Down
8 changes: 4 additions & 4 deletions zstd-safe/zstd-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ include!("bindings_zstd.rs");
#[cfg(all(
not(feature = "std"),
not(feature = "experimental"),
feature = "zdict",
feature = "zdict_builder",
not(feature = "bindgen")
))]
include!("bindings_zdict.rs");
Expand All @@ -44,7 +44,7 @@ include!("bindings_zstd_experimental.rs");
#[cfg(all(
not(feature = "std"),
feature = "experimental",
feature = "zdict",
feature = "zdict_builder",
not(feature = "bindgen")
))]
include!("bindings_zdict_experimental.rs");
Expand All @@ -61,7 +61,7 @@ include!("bindings_zstd_std.rs");
#[cfg(all(
feature = "std",
not(feature = "experimental"),
feature = "zdict",
feature = "zdict_builder",
not(feature = "bindgen")
))]
include!("bindings_zdict_std.rs");
Expand All @@ -76,7 +76,7 @@ include!("bindings_zstd_std_experimental.rs");
#[cfg(all(
feature = "std",
feature = "experimental",
feature = "zdict",
feature = "zdict_builder",
not(feature = "bindgen")
))]
include!("bindings_zdict_std_experimental.rs");