Skip to content

Commit

Permalink
Remove uses of unsafe from file_format_common
Browse files Browse the repository at this point in the history
  • Loading branch information
KamilaBorowska authored and calibra-opensource committed Jun 24, 2019
1 parent a030880 commit 5f89337
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions language/vm/src/file_format_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
//! It's used to compress mostly indexes into the main binary tables.
use byteorder::ReadBytesExt;
use failure::*;
use std::{
io::Cursor,
mem::{size_of, transmute},
};
use std::{io::Cursor, mem::size_of};

/// Constant values for the binary format header.
///
Expand Down Expand Up @@ -170,26 +167,17 @@ pub fn write_u32_as_uleb128(binary: &mut Vec<u8>, value: u32) {

/// Write a `u16` in Little Endian format.
pub fn write_u16(binary: &mut Vec<u8>, value: u16) {
let bytes: [u8; 2] = unsafe { transmute(value.to_le()) };
for byte in &bytes {
binary.push(*byte);
}
binary.extend(&value.to_le_bytes());
}

/// Write a `u32` in Little Endian format.
pub fn write_u32(binary: &mut Vec<u8>, value: u32) {
let bytes: [u8; 4] = unsafe { transmute(value.to_le()) };
for byte in &bytes {
binary.push(*byte);
}
binary.extend(&value.to_le_bytes());
}

/// Write a `u64` in Little Endian format.
pub fn write_u64(binary: &mut Vec<u8>, value: u64) {
let bytes: [u8; 8] = unsafe { transmute(value.to_le()) };
for byte in &bytes {
binary.push(*byte);
}
binary.extend(&value.to_le_bytes());
}

/// Reads a `u16` in ULEB128 format from a `binary`.
Expand Down

0 comments on commit 5f89337

Please sign in to comment.