Skip to content

Commit

Permalink
Merge pull request #37 from dragonmaus/master
Browse files Browse the repository at this point in the history
Make `Compression` API-compatible with flate2
  • Loading branch information
alexcrichton authored Dec 10, 2018
2 parents 34265a5 + f7213a1 commit 50c3d85
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 28 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bzip2"
version = "0.3.3"
version = "0.4.0"
authors = ["Alex Crichton <alex@alexcrichton.com>"]
license = "MIT/Apache-2.0"
readme = "README.md"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ A streaming compression/decompression library for rust with bindings to libbz2.
```toml
# Cargo.toml
[dependencies]
bzip2 = "0.3.2"
bzip2 = "0.4"
```


Expand Down
42 changes: 34 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
//! // Round trip some bytes from a byte source, into a compressor, into a
//! // decompressor, and finally into a vector.
//! let data = "Hello, World!".as_bytes();
//! let compressor = BzEncoder::new(data, Compression::Best);
//! let compressor = BzEncoder::new(data, Compression::best());
//! let mut decompressor = BzDecoder::new(compressor);
//!
//! let mut contents = String::new();
Expand All @@ -33,7 +33,7 @@
//! the `tokio` feature of this crate:
//!
//! ```toml
//! bzip2 = { version = "0.3", features = ["tokio"] }
//! bzip2 = { version = "0.4", features = ["tokio"] }
//! ```
//!
//! All methods are internally capable of working with streams that may return
Expand All @@ -48,7 +48,7 @@
//! these operations will be a noop.
#![deny(missing_docs)]
#![doc(html_root_url = "https://docs.rs/bzip2/0.3")]
#![doc(html_root_url = "https://docs.rs/bzip2/")]

extern crate bzip2_sys as ffi;
extern crate libc;
Expand All @@ -75,12 +75,38 @@ pub mod write;
/// When compressing data, the compression level can be specified by a value in
/// this enum.
#[derive(Copy, Clone, Debug)]
pub enum Compression {
pub struct Compression(u32);

impl Compression {
/// Create a new compression spec with a specific numeric level (0-9).
pub fn new(level: u32) -> Compression {
Compression(level)
}

/// Do not compress.
pub fn none() -> Compression {
Compression(0)
}

/// Optimize for the best speed of encoding.
Fastest = 1,
pub fn fast() -> Compression {
Compression(1)
}

/// Optimize for the size of data being encoded.
Best = 9,
/// Choose the default compression, a balance between speed and size.
Default = 6,
pub fn best() -> Compression {
Compression(9)
}

/// Return the compression level as an integer.
pub fn level(&self) -> u32 {
self.0
}
}

impl Default for Compression {
/// Choose the default compression, a balance between speed and size.
fn default() -> Compression {
Compression(6)
}
}
2 changes: 1 addition & 1 deletion src/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Compress {
pub fn new(lvl: Compression, work_factor: u32) -> Compress {
unsafe {
let mut raw = Box::new(mem::zeroed());
assert_eq!(ffi::BZ2_bzCompressInit(&mut *raw, lvl as c_int, 0,
assert_eq!(ffi::BZ2_bzCompressInit(&mut *raw, lvl.level() as c_int, 0,
work_factor as c_int), 0);
Compress {
inner: Stream { raw: raw, _marker: marker::PhantomData },
Expand Down
18 changes: 9 additions & 9 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ mod tests {
#[test]
fn smoke() {
let m: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8];
let mut c = BzEncoder::new(m, Compression::Default);
let mut c = BzEncoder::new(m, Compression::default());
let mut data = vec![];
c.read_to_end(&mut data).unwrap();
let mut d = BzDecoder::new(&data[..]);
Expand All @@ -193,7 +193,7 @@ mod tests {
#[test]
fn smoke2() {
let m: &[u8] = &[1, 2, 3, 4, 5, 6, 7, 8];
let c = BzEncoder::new(m, Compression::Default);
let c = BzEncoder::new(m, Compression::default());
let mut d = BzDecoder::new(c);
let mut data = vec![];
d.read_to_end(&mut data).unwrap();
Expand All @@ -203,7 +203,7 @@ mod tests {
#[test]
fn smoke3() {
let m = vec![3u8; 128 * 1024 + 1];
let c = BzEncoder::new(&m[..], Compression::Default);
let c = BzEncoder::new(&m[..], Compression::default());
let mut d = BzDecoder::new(c);
let mut data = vec![];
d.read_to_end(&mut data).unwrap();
Expand All @@ -213,7 +213,7 @@ mod tests {
#[test]
fn self_terminating() {
let m = vec![3u8; 128 * 1024 + 1];
let mut c = BzEncoder::new(&m[..], Compression::Default);
let mut c = BzEncoder::new(&m[..], Compression::default());

let mut result = Vec::new();
c.read_to_end(&mut result).unwrap();
Expand All @@ -233,7 +233,7 @@ mod tests {
#[test]
fn zero_length_read_at_eof() {
let m = Vec::new();
let mut c = BzEncoder::new(&m[..], Compression::Default);
let mut c = BzEncoder::new(&m[..], Compression::default());

let mut result = Vec::new();
c.read_to_end(&mut result).unwrap();
Expand All @@ -246,7 +246,7 @@ mod tests {
#[test]
fn zero_length_read_with_data() {
let m = vec![3u8; 128 * 1024 + 1];
let mut c = BzEncoder::new(&m[..], Compression::Default);
let mut c = BzEncoder::new(&m[..], Compression::default());

let mut result = Vec::new();
c.read_to_end(&mut result).unwrap();
Expand All @@ -258,7 +258,7 @@ mod tests {

#[test]
fn empty() {
let r = BzEncoder::new(&[][..], Compression::Default);
let r = BzEncoder::new(&[][..], Compression::default());
let mut r = BzDecoder::new(r);
let mut v2 = Vec::new();
r.read_to_end(&mut v2).unwrap();
Expand All @@ -270,7 +270,7 @@ mod tests {
::quickcheck::quickcheck(test as fn(_) -> _);

fn test(v: Vec<u8>) -> bool {
let r = BzEncoder::new(&v[..], Compression::Default);
let r = BzEncoder::new(&v[..], Compression::default());
let mut r = BzDecoder::new(r);
let mut v2 = Vec::new();
r.read_to_end(&mut v2).unwrap();
Expand All @@ -285,7 +285,7 @@ mod tests {
fn test(v: Vec<u8>,
encode_ops: PartialWithErrors<GenInterrupted>,
decode_ops: PartialWithErrors<GenInterrupted>) -> bool {
let r = BzEncoder::new(PartialRead::new(&v[..], encode_ops), Compression::Default);
let r = BzEncoder::new(PartialRead::new(&v[..], encode_ops), Compression::default());
let mut r = BzDecoder::new(PartialRead::new(r, decode_ops));
let mut v2 = Vec::new();
r.read_to_end(&mut v2).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ mod tests {
#[test]
fn smoke() {
let d = BzDecoder::new(Vec::new());
let mut c = BzEncoder::new(d, ::Compression::Default);
let mut c = BzEncoder::new(d, ::Compression::default());
c.write_all(b"12834").unwrap();
let s = repeat("12345").take(100000).collect::<String>();
c.write_all(s.as_bytes()).unwrap();
Expand All @@ -337,7 +337,7 @@ mod tests {
#[test]
fn write_empty() {
let d = BzDecoder::new(Vec::new());
let mut c = BzEncoder::new(d, ::Compression::Default);
let mut c = BzEncoder::new(d, ::Compression::default());
c.write(b"").unwrap();
let data = c.finish().unwrap().finish().unwrap();
assert_eq!(&data[..], b"");
Expand All @@ -349,7 +349,7 @@ mod tests {

fn test(v: Vec<u8>) -> bool {
let w = BzDecoder::new(Vec::new());
let mut w = BzEncoder::new(w, ::Compression::Default);
let mut w = BzEncoder::new(w, ::Compression::default());
w.write_all(&v).unwrap();
v == w.finish().unwrap().finish().unwrap()
}
Expand All @@ -363,7 +363,7 @@ mod tests {
encode_ops: PartialWithErrors<GenInterrupted>,
decode_ops: PartialWithErrors<GenInterrupted>) -> bool {
let w = BzDecoder::new(PartialWrite::new(Vec::new(), decode_ops));
let mut w = BzEncoder::new(PartialWrite::new(w, encode_ops), ::Compression::Default);
let mut w = BzEncoder::new(PartialWrite::new(w, encode_ops), ::Compression::default());
w.write_all(&v).unwrap();
v == w.finish().unwrap().into_inner().finish().unwrap().into_inner()
}
Expand Down
8 changes: 4 additions & 4 deletions tests/tokio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ fn tcp_stream_echo_pattern() {
assert_eq!(b.read(&mut buf).unwrap(), 0);
});

let mut a = write::BzEncoder::new(a, Compression::Default);
let mut a = write::BzEncoder::new(a, Compression::default());
for i in 0..N {
let buf = [i; M];
a.write_all(&buf).unwrap();
Expand All @@ -61,7 +61,7 @@ fn tcp_stream_echo_pattern() {
let copy = stream.and_then(|s| {
let (a, b) = s.split();
let a = read::BzDecoder::new(a);
let b = write::BzEncoder::new(b, Compression::Default);
let b = write::BzEncoder::new(b, Compression::default());
copy(a, b)
}).then(|result| {
let (amt, _a, b) = result.unwrap();
Expand Down Expand Up @@ -99,7 +99,7 @@ fn echo_random() {
assert_eq!(b.read(&mut buf).unwrap(), 0);
});

let mut a = write::BzEncoder::new(a, Compression::Default);
let mut a = write::BzEncoder::new(a, Compression::default());
a.write_all(&v2).unwrap();
a.finish().unwrap()
.shutdown(Shutdown::Write).unwrap();
Expand All @@ -112,7 +112,7 @@ fn echo_random() {
let copy = stream.and_then(|s| {
let (a, b) = s.split();
let a = read::BzDecoder::new(a);
let b = write::BzEncoder::new(b, Compression::Default);
let b = write::BzEncoder::new(b, Compression::default());
copy(a, b)
}).then(|result| {
let (amt, _a, b) = result.unwrap();
Expand Down

0 comments on commit 50c3d85

Please sign in to comment.