Skip to content

Commit

Permalink
Merge pull request rust-lang#115 from AndyGauge/examples
Browse files Browse the repository at this point in the history
crate level examples, including FlateReadExt
  • Loading branch information
alexcrichton authored May 28, 2017
2 parents 1b1cd65 + 1538c03 commit 2b324f6
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/deflatedecoder-bufread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
println!("{}", decode_reader(bytes).unwrap());
}

// Uncompresses a Gz Encoded vector of bytes and returns a string or error
// Uncompresses a Deflate Encoded vector of bytes and returns a string or error
// Here &[u8] implements Read
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {
let mut deflater = DeflateDecoder::new(&bytes[..]);
Expand Down
2 changes: 1 addition & 1 deletion examples/deflatedecoder-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
println!("{}", decode_reader(bytes).unwrap());
}

// Uncompresses a Gz Encoded vector of bytes and returns a string or error
// Uncompresses a Deflate Encoded vector of bytes and returns a string or error
// Here &[u8] implements Read
fn decode_reader(bytes: Vec<u8>) -> io::Result<String> {
let mut deflater = DeflateDecoder::new(&bytes[..]);
Expand Down
2 changes: 1 addition & 1 deletion examples/deflateencoder-read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn main() {
println!("{:?}", deflateencoder_read_hello_world().unwrap());
}

// Return a vector containing the GZ compressed version of hello world
// Return a vector containing the Defalte compressed version of hello world
fn deflateencoder_read_hello_world() -> io::Result<Vec<u8>> {
let mut ret_vec = [0;100];
let c = b"hello world";
Expand Down
22 changes: 22 additions & 0 deletions examples/flatereadext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extern crate flate2;

use flate2::{FlateReadExt, Compression};
use std::io::prelude::*;
use std::io;
use std::fs::File;

fn main() {
println!("{}", run().unwrap());
}

fn run() -> io::Result<String> {
let f = File::open("examples/hello_world.txt")?;

//gz_encode method comes from FlateReadExt and applies to a std::fs::File
let data = f.gz_encode(Compression::Default);
let mut buffer = String::new();

//gz_decode method comes from FlateReadExt and applies to a &[u8]
&data.gz_decode()?.read_to_string(&mut buffer)?;
Ok(buffer)
}
2 changes: 1 addition & 1 deletion src/deflate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub struct EncoderWriter<W: Write> {
/// # println!("{:?}", deflateencoder_read_hello_world().unwrap());
/// # }
/// #
/// // Return a vector containing the GZ compressed version of hello world
/// // Return a vector containing the Deflate compressed version of hello world
/// fn deflateencoder_read_hello_world() -> io::Result<Vec<u8>> {
/// let mut ret_vec = [0;100];
/// let c = b"hello world";
Expand Down
40 changes: 40 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@
//! of data. All types in the [`write`] module work on instances of [`Write`],
//! whereas all types in the [`read`] module work on instances of [`Read`].
//!
//! ```
//! use flate2::write::GzEncoder;
//! use flate2::Compression;
//! use std::io;
//! use std::io::prelude::*;
//!
//! # fn main() { let _ = run(); }
//! # fn run() -> io::Result<()> {
//! let mut encoder = GzEncoder::new(Vec::new(), Compression::Default);
//! encoder.write(b"Example")?;
//! # Ok(())
//! # }
//! ```
//!
//!
//! Other various types are provided at the top-level of the crate for
//! management and dealing with encoders/decoders.
//!
Expand All @@ -33,6 +48,31 @@
//! [`FlateReadExt`]: trait.FlateReadExt.html
//! [`FlateWriteExt`]: trait.FlateWriteExt.html
//!
//! ```
//! use flate2::{FlateReadExt, Compression};
//! use std::io::prelude::*;
//! use std::io;
//! use std::fs::File;
//!
//! # fn main() {
//! # println!("{}", run().unwrap());
//! # }
//! #
//! // Read contents of file with a compression stream, then decompress with GZ
//!
//! # fn run() -> io::Result<String> {
//! let f = File::open("examples/hello_world.txt")?;
//!
//! //gz_encode method comes from FlateReadExt and applies to a std::fs::File
//! let data = f.gz_encode(Compression::Default);
//! let mut buffer = String::new();
//!
//! //gz_decode method comes from FlateReadExt and applies to a &[u8]
//! &data.gz_decode()?.read_to_string(&mut buffer)?;
//! # Ok(buffer)
//! # }
//! ```
//!
//! # Async I/O
//!
//! This crate optionally can support async I/O streams with the [Tokio stack] via
Expand Down

0 comments on commit 2b324f6

Please sign in to comment.