Skip to content

Commit

Permalink
Update Readme and doctest it
Browse files Browse the repository at this point in the history
  • Loading branch information
HeroicKatora committed Sep 20, 2020
1 parent 33fd3a9 commit 3cd4ed3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
14 changes: 6 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,18 @@ This library provides all functions necessary to de- and encode GIF files.

The high level interface consists of the two types
[`Encoder`](https://docs.rs/gif/0.10.1/gif/struct.Encoder.html) and [`Decoder`](https://docs.rs/gif/0.10.1/gif/struct.Decoder.html).
They as builders for the actual en- and decoders and can be used to set various
options beforehand.

### Decoding GIF files

```rust
// Open the file
use std::fs::File;
use gif::SetParameter;
let mut decoder = gif::Decoder::new(File::open("tests/samples/sample_1.gif").unwrap());
let input = File::open("tests/samples/sample_1.gif").unwrap();
// Configure the decoder such that it will expand the image to RGBA.
decoder.set(gif::ColorOutput::RGBA);
let mut options = gif::DecodeOptions::new();
options.set_color_output(gif::ColorOutput::RGBA);
// Read the file header
let mut decoder = decoder.read_info().unwrap();
let mut decoder = options.read_info(input).unwrap();
while let Some(frame) = decoder.read_next_frame().unwrap() {
// Process every frame
}
Expand All @@ -34,7 +32,7 @@ while let Some(frame) = decoder.read_next_frame().unwrap() {
The encoder can be used to save simple computer generated images:

```rust
use gif::{Frame, Encoder, Repeat, SetParameter};
use gif::{Frame, Encoder, Repeat};
use std::fs::File;
use std::borrow::Cow;

Expand All @@ -57,7 +55,7 @@ let beacon_states = [[
]];
let mut image = File::create("target/beacon.gif").unwrap();
let mut encoder = Encoder::new(&mut image, width, height, color_map).unwrap();
encoder.set(Repeat::Infinite).unwrap();
encoder.set_repeat(Repeat::Infinite).unwrap();
for state in &beacon_states {
let mut frame = Frame::default();
frame.width = width;
Expand Down
11 changes: 9 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
//!
//! The high level interface consists of the two types
//! [`Encoder`](struct.Encoder.html) and [`Decoder`](struct.Decoder.html).
//! They as builders for the actual en- and decoders and can be used to set various
//! options beforehand.
//!
//! ### Decoding GIF files
//!
Expand Down Expand Up @@ -142,3 +140,12 @@ fn round_trip() {
}
assert_eq!(&data[..], &data2[..])
}

macro_rules! insert_as_doc {
{ $content:expr } => {
#[doc = $content] extern { }
}
}

// Provides the README.md as doc, to ensure the example works!
insert_as_doc!(include_str!("../README.md"));

0 comments on commit 3cd4ed3

Please sign in to comment.