mem.rs 1 KB
Newer Older
qinsoon's avatar
qinsoon committed
1
pub extern crate memmap;
2 3
pub extern crate memsec;

4 5
use Word;
#[allow(unused_imports)]
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
use byteorder::{LittleEndian, BigEndian, ReadBytesExt, WriteBytesExt, ByteOrder};

#[cfg(target_arch = "x86_64")]
pub fn u64_to_raw(val: u64) -> Word {
    let mut ret = vec![];
    ret.write_u64::<LittleEndian>(val).unwrap();
    
    as_word(ret)
}

#[cfg(target_arch = "x86_64")]
pub fn f32_to_raw(val: f32) -> Word {
    let mut ret = vec![];
    ret.write_f32::<LittleEndian>(val).unwrap();
    as_word(ret)
}

#[cfg(target_arch = "x86_64")]
pub fn f64_to_raw(val: f64) -> Word {
    let mut ret = vec![];
    ret.write_f64::<LittleEndian>(val).unwrap();
    as_word(ret)
}

#[cfg(target_arch = "x86_64")]
pub fn as_word(mut u8_array: Vec<u8>) -> Word {
    LittleEndian::read_uint(&mut u8_array, 8) as Word
}

#[cfg(test)]
mod tests{
    use super::*;
38
    use Word;
39 40 41 42 43 44 45 46
    
    #[test]
    fn test_primitive_to_raw() {
        let a : Word = 0xabcd;
        let raw = u64_to_raw(a as u64);
        
        assert_eq!(raw, a);
    }
47
}