Rust style fixes

This commit is contained in:
Nigel Tao committed 2018-01-31 16:50:38 +11:00
1 parent d74b3a3e29
commit bcc4450624
1 file changed
+33 -44
+33 -44
View File
@@ -41,13 +41,7 @@ const FIRST_PIXEL: u8 = 1; // Top left pixel's red component is 0x01.
const LAST_PIXEL: u8 = 3; // Bottom right pixel's blue component is 0x03. const LAST_PIXEL: u8 = 3; // Bottom right pixel's blue component is 0x03.
fn main() { fn main() {
let mut dst: Vec<u8> = Vec::with_capacity(NUM_BYTES); let mut dst = [0; NUM_BYTES];
// This unsafe code is based on decode_lzw_test in github.com/Geal/gif.rs's
// parser.rs.
unsafe {
dst.set_len(NUM_BYTES);
}
let src = include_bytes!("../../../test/testdata/harvesters.gif"); let src = include_bytes!("../../../test/testdata/harvesters.gif");
let start = Instant::now(); let start = Instant::now();
@@ -61,12 +55,17 @@ fn main() {
let elapsed_nanos = elapsed.as_secs() * 1_000_000_000 + (elapsed.subsec_nanos() as u64); let elapsed_nanos = elapsed.as_secs() * 1_000_000_000 + (elapsed.subsec_nanos() as u64);
let total_pixels: u64 = ((WIDTH * HEIGHT) as u64) * (REPS as u64); let total_pixels: u64 = ((WIDTH * HEIGHT) as u64) * (REPS as u64);
let kb_per_s: u64 = total_pixels * 1_000_000 / elapsed_nanos; let kp_per_s: u64 = total_pixels * 1_000_000 / elapsed_nanos;
// Use eprint instead of print because the https://github.com/Geal/gif.rs // Use eprint instead of print because the https://github.com/Geal/gif.rs
// library can print its own debugging messages. When running this program, // library can print its own debugging messages (this is filed as
// it can be useful to redirect stdout (but not stderr) to /dev/null. // https://github.com/Geal/gif.rs/issues/4). When running this program, it
eprint!("{}.{:03} MB/s\n", kb_per_s / 1_000, kb_per_s % 1_000); // can be useful to redirect stdout (but not stderr) to /dev/null.
eprint!(
"{}.{:03} megapixels/second\n",
kp_per_s / 1_000,
kp_per_s % 1_000
);
} }
fn decode(dst: &mut [u8], src: &[u8]) { fn decode(dst: &mut [u8], src: &[u8]) {
@@ -74,39 +73,29 @@ fn decode(dst: &mut [u8], src: &[u8]) {
dst[0] = 0; dst[0] = 0;
dst[NUM_BYTES - 1] = 0; dst[NUM_BYTES - 1] = 0;
// This nested code is based on decode_lzw_test in github.com/Geal/gif.rs's let (_, colors) =
// parser.rs. gif::parser::color_table(&src[COLOR_TABLE_OFFSET..], COLOR_TABLE_ELEMENT_COUNT).unwrap();
match gif::parser::color_table(&src[COLOR_TABLE_OFFSET..], COLOR_TABLE_ELEMENT_COUNT) { let (_, block) = gif::parser::graphic_block(&src[GRAPHIC_BLOCK_OFFSET..]).unwrap();
Ok((_, colors)) => {
match gif::parser::graphic_block(&src[GRAPHIC_BLOCK_OFFSET..]) { let rendering = match block {
Ok((_, gif::parser::Block::GraphicBlock(_, rendering))) => { gif::parser::Block::GraphicBlock(_, x) => x,
match rendering { _ => panic!("not a graphic block"),
gif::parser::GraphicRenderingBlock::TableBasedImage(_, };
code_size,
blocks) => { let (code_size, blocks) = match rendering {
match gif::lzw::decode_lzw(&colors, code_size as usize, blocks, dst) { gif::parser::GraphicRenderingBlock::TableBasedImage(_, x, y) => (x, y),
Ok(num_bytes) => { _ => panic!("not a table based image"),
if num_bytes != NUM_BYTES { };
panic!("wrong num_bytes")
} let num_bytes = gif::lzw::decode_lzw(&colors, code_size as usize, blocks, dst).unwrap();
// A hard-coded sanity check that we
// decoded the image's pixel data properly. if num_bytes != NUM_BYTES {
if (dst[0] != FIRST_PIXEL) || panic!("wrong num_bytes")
(dst[NUM_BYTES - 1] != LAST_PIXEL) }
{
panic!("wrong dst pixels") // A hard-coded sanity check that we decoded the pixel data correctly.
} if (dst[0] != FIRST_PIXEL) || (dst[NUM_BYTES - 1] != LAST_PIXEL) {
} panic!("wrong dst pixels")
_ => panic!("could not decode"),
}
}
_ => panic!("plaintext extension"),
}
}
_ => panic!("cannot parse graphic block"),
}
}
_ => panic!("cannot parse global color table"),
} }
} }