Skip to content

Commit

Permalink
Apply rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
gyscos committed Jul 11, 2016
1 parent d6a4f3d commit 6449243
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
12 changes: 6 additions & 6 deletions src/block/compressor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ impl Compressor {
ll::parse_code(code)
}

/// Compress a block of data, and return the compressed result in a `Vec<u8>`.
pub fn compress(&mut self, data: &[u8], level: i32) -> io::Result<Vec<u8>> {
/// Compresses a block of data and returns the compressed result.
pub fn compress(&mut self, data: &[u8], lvl: i32) -> io::Result<Vec<u8>> {
// We allocate a big buffer, slightly larger than the input data.
let buffer_len = unsafe { ll::ZSTD_compressBound(data.len()) };
let mut buffer = Vec::with_capacity(buffer_len);
unsafe {
// Use all capacity. Memory may not be initialized, but we won't read it.
// Use all capacity.
// Memory may not be initialized, but we won't read it.
buffer.set_len(buffer_len);
let len = try!(self.compress_to_buffer(&mut buffer[..],
data,
level));
let len =
try!(self.compress_to_buffer(&mut buffer[..], data, lvl));
buffer.set_len(len);
}

Expand Down
31 changes: 15 additions & 16 deletions src/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use self::decompressor::Decompressor;

use std::io;

/// Compress a single block of data to the given destination buffer.
/// Compresses a single block of data to the given destination buffer.
///
/// Returns the number of bytes written, or an error if something happened
/// (for instance if the destination buffer was too small).
Expand All @@ -22,7 +22,7 @@ pub fn compress_to_buffer(destination: &mut [u8], source: &[u8], level: i32)
Compressor::new().compress_to_buffer(destination, source, level)
}

/// Compress a block of data, and return the compressed result in a `Vec<u8>`.
/// Compresses a block of data and returns the compressed result.
pub fn compress(data: &[u8], level: i32) -> io::Result<Vec<u8>> {
Compressor::new().compress(data, level)
}
Expand All @@ -36,7 +36,7 @@ pub fn decompress_to_buffer(destination: &mut [u8], source: &[u8])
Decompressor::new().decompress_to_buffer(destination, source)
}

/// Decompress a block of data, and return the decompressed result in a `Vec<u8>`.
/// Decompresses a block of data and returns the decompressed result.
///
/// The decompressed data should be less than `capacity` bytes,
/// or an error will be returned.
Expand All @@ -47,19 +47,18 @@ pub fn decompress(data: &[u8], capacity: usize) -> io::Result<Vec<u8>> {
#[test]
fn test_direct() {
// hipsum.co
let text = "Pork belly art party wolf XOXO, neutra scenester ugh \
thundercats tattooed squid skateboard beard readymade kogi. \
VHS cardigan schlitz, meditation chartreuse kogi tilde \
church-key. Actually direct trade hammock, aesthetic VHS \
semiotics organic narwhal lo-fi heirloom flexitarian master \
cleanse polaroid man bun. Flannel helvetica mustache, \
bicycle rights small batch slow-carb neutra tilde \
williamsburg meh poutine humblebrag. Four dollar toast \
butcher actually franzen, gastropub mustache tofu cardigan. \
90's fingerstache forage brooklyn meditation single-origin \
coffee tofu actually, ramps pabst farm-to-table art party \
kombucha artisan fanny pack. Flannel salvia ennui viral \
leggings selfies.";
let text =
"Pork belly art party wolf XOXO, neutra scenester ugh thundercats \
tattooed squid skateboard beard readymade kogi. VHS cardigan \
schlitz, meditation chartreuse kogi tilde church-key. Actually \
direct trade hammock, aesthetic VHS semiotics organic narwhal lo-fi \
heirloom flexitarian master cleanse polaroid man bun. Flannel \
helvetica mustache, bicycle rights small batch slow-carb neutra \
tilde williamsburg meh poutine humblebrag. Four dollar toast \
butcher actually franzen, gastropub mustache tofu cardigan. 90's \
fingerstache forage brooklyn meditation single-origin coffee tofu \
actually, ramps pabst farm-to-table art party kombucha artisan \
fanny pack. Flannel salvia ennui viral leggings selfies.";

let compressed = compress(text.as_bytes(), 1).unwrap();

Expand Down
6 changes: 3 additions & 3 deletions src/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ pub fn from_samples<S: AsRef<[u8]>>(samples: &[S], max_size: usize)
-> io::Result<Vec<u8>> {
// Copy every sample to a big chunk of memory
let data: Vec<_> = samples.iter()
.flat_map(|s| s.as_ref())
.cloned()
.collect();
.flat_map(|s| s.as_ref())
.cloned()
.collect();
let sizes: Vec<_> = samples.iter().map(|s| s.as_ref().len()).collect();

from_continuous(&data, &sizes, max_size)
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod stream;
pub mod block;
pub mod dict;

pub use stream::encoder::{Encoder, AutoFinishEncoder};
pub use stream::encoder::{AutoFinishEncoder, Encoder};
pub use stream::decoder::Decoder;

use std::io;
Expand Down Expand Up @@ -68,7 +68,7 @@ fn test_cycle() {
let compressed = encode_all(text.as_bytes(), 1).unwrap();

let decompressed = String::from_utf8(decode_all(&compressed).unwrap())
.unwrap();
.unwrap();

assert_eq!(text, &decompressed);
}
3 changes: 2 additions & 1 deletion src/stream/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ impl<R: Read> Read for Decoder<R> {

written += out_size;
if res == 0 {
self.offset = self.buffer.capacity() + 1; // End-of-frame marker.
// End-of-frame marker.
self.offset = self.buffer.capacity() + 1;
break;
}
self.offset += in_size;
Expand Down
19 changes: 13 additions & 6 deletions src/stream/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ pub struct AutoFinishEncoder<W: Write> {
}

impl<W: Write> AutoFinishEncoder<W> {
fn new<F: 'static + FnMut(io::Result<W>)>(encoder: Encoder<W>, on_finish: F) -> Self {
fn new<F: 'static + FnMut(io::Result<W>)>(encoder: Encoder<W>,
on_finish: F)
-> Self {
AutoFinishEncoder {
encoder: Some(encoder),
on_finish: Some(Box::new(on_finish)),
Expand Down Expand Up @@ -114,18 +116,23 @@ impl<W: Write> Encoder<W> {
///
/// Panics if an error happens when finishing the stream.
pub fn auto_finish(self) -> AutoFinishEncoder<W> {
self.on_finish(|result| { result.unwrap(); })
self.on_finish(|result| {
result.unwrap();
})
}

/// Returns an encoder that will finish the stream on drop.
///
/// Calls the given callback with the result from `finish()`.
pub fn on_finish<F: 'static + FnMut(io::Result<W>)>(self, f: F) -> AutoFinishEncoder<W> {
pub fn on_finish<F: 'static + FnMut(io::Result<W>)>
(self, f: F)
-> AutoFinishEncoder<W> {
AutoFinishEncoder::new(self, f)
}

fn with_context(writer: W, context: EncoderContext) -> io::Result<Self> {
// This is the output buffer size, for compressed data we get from zstd.
// This is the output buffer size,
// for compressed data we get from zstd.
let buffer_size = unsafe { ll::ZBUFF_recommendedCOutSize() };

Ok(Encoder {
Expand Down Expand Up @@ -199,8 +206,8 @@ impl<W: Write> Write for Encoder<W> {
let mut out_size = self.buffer.capacity();
unsafe {
let code = ll::ZBUFF_compressFlush(self.context.c,
self.buffer.as_mut_ptr(),
&mut out_size);
self.buffer.as_mut_ptr(),
&mut out_size);
self.buffer.set_len(out_size);
let _ = try!(ll::parse_code(code));
}
Expand Down

0 comments on commit 6449243

Please sign in to comment.