Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop unsafe blocks from common.rs #61

Merged
merged 1 commit into from
Aug 31, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
//! Common common used both by decoder and encoder
extern crate color_quant;

use std::mem;
use std::borrow::Cow;

/// Disposal method
Expand All @@ -21,10 +20,12 @@ pub enum DisposalMethod {
impl DisposalMethod {
/// Converts `u8` to `Option<Self>`
pub fn from_u8(n: u8) -> Option<DisposalMethod> {
if n <= 3 {
Some(unsafe { mem::transmute(n) })
} else {
None
match n {
0 => Some(DisposalMethod::Any),
1 => Some(DisposalMethod::Keep),
2 => Some(DisposalMethod::Background),
3 => Some(DisposalMethod::Previous),
_ => None
}
}
}
Expand All @@ -45,9 +46,9 @@ impl Block {
/// Converts `u8` to `Option<Self>`
pub fn from_u8(n: u8) -> Option<Block> {
match n {
0x2C | 0x21 | 0x3B => {
Some(unsafe { mem::transmute(n) })
}
0x2C => Some(Block::Image),
0x21 => Some(Block::Extension),
0x3B => Some(Block::Trailer),
_ => None
}
}
Expand All @@ -72,9 +73,10 @@ impl Extension {
/// Converts `u8` to `Option<Self>`
pub fn from_u8(n: u8) -> Option<Extension> {
match n {
0x01 | 0xF9 | 0xFE | 0xFF => {
Some(unsafe { mem::transmute(n) })
}
0x01 => Some(Extension::Text),
0xF9 => Some(Extension::Control),
0xFE => Some(Extension::Comment),
0xFF => Some(Extension::Application),
_ => None
}
}
Expand Down