2015-11-26 09:45:25 +00:00
|
|
|
use std::{mem, slice, ptr};
|
2015-11-25 20:55:30 +00:00
|
|
|
use libc;
|
|
|
|
|
|
|
|
pub type Duration = u32;
|
|
|
|
pub type Time = i64;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn now() -> Time {
|
|
|
|
let mut tv = libc::timespec { tv_sec: 0, tv_nsec: 0 };
|
|
|
|
unsafe { libc::clock_gettime(6, &mut tv); }
|
|
|
|
tv.tv_sec
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn time_rand() -> i64 {
|
|
|
|
let mut tv = libc::timespec { tv_sec: 0, tv_nsec: 0 };
|
|
|
|
unsafe { libc::clock_gettime(libc::CLOCK_MONOTONIC, &mut tv); }
|
|
|
|
tv.tv_sec ^ tv.tv_nsec
|
|
|
|
}
|
|
|
|
|
2015-11-19 18:29:42 +00:00
|
|
|
|
2015-11-20 12:34:54 +00:00
|
|
|
#[inline(always)]
|
2015-11-19 18:29:42 +00:00
|
|
|
pub unsafe fn as_bytes<T>(obj: &T) -> &[u8] {
|
|
|
|
slice::from_raw_parts(mem::transmute::<&T, *const u8>(obj), mem::size_of::<T>())
|
|
|
|
}
|
|
|
|
|
2015-11-20 12:34:54 +00:00
|
|
|
#[inline(always)]
|
2015-11-19 18:29:42 +00:00
|
|
|
pub unsafe fn as_obj<T>(data: &[u8]) -> &T {
|
|
|
|
assert!(data.len() >= mem::size_of::<T>());
|
|
|
|
mem::transmute(data.as_ptr())
|
|
|
|
}
|
2015-11-22 23:49:58 +00:00
|
|
|
|
2015-11-26 09:45:25 +00:00
|
|
|
#[inline(always)]
|
|
|
|
pub fn memcopy(src: &[u8], dst: &mut[u8]) {
|
|
|
|
assert!(dst.len() >= src.len());
|
|
|
|
unsafe { ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), src.len()) };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub struct Encoder;
|
|
|
|
|
|
|
|
impl Encoder {
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn read_u16(data: &[u8]) -> u16 {
|
|
|
|
((data[0] as u16) << 8) | data[1] as u16
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn write_u16(val: u16, data: &mut [u8]) {
|
|
|
|
data[0] = ((val >> 8) & 0xff) as u8;
|
|
|
|
data[1] = (val & 0xff) as u8;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn read_u64(data: &[u8]) -> u64 {
|
|
|
|
((data[0] as u64) << 56) | ((data[1] as u64) << 48) |
|
|
|
|
((data[2] as u64) << 40) | ((data[3] as u64) << 32) |
|
|
|
|
((data[4] as u64) << 24) | ((data[5] as u64) << 16) |
|
|
|
|
((data[6] as u64) << 8) | data[7] as u64
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn write_u64(val: u64, data: &mut [u8]) {
|
|
|
|
data[0] = ((val >> 56) & 0xff) as u8;
|
|
|
|
data[1] = ((val >> 48) & 0xff) as u8;
|
|
|
|
data[2] = ((val >> 40) & 0xff) as u8;
|
|
|
|
data[3] = ((val >> 32) & 0xff) as u8;
|
|
|
|
data[4] = ((val >> 24) & 0xff) as u8;
|
|
|
|
data[5] = ((val >> 16) & 0xff) as u8;
|
|
|
|
data[6] = ((val >> 8) & 0xff) as u8;
|
|
|
|
data[7] = (val & 0xff) as u8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-25 11:29:12 +00:00
|
|
|
macro_rules! fail {
|
|
|
|
($format:expr) => ( {
|
|
|
|
use std::process;
|
|
|
|
error!($format);
|
|
|
|
process::exit(-1);
|
|
|
|
} );
|
|
|
|
($format:expr, $( $arg:expr ),+) => ( {
|
|
|
|
use std::process;
|
|
|
|
error!($format, $( $arg ),+ );
|
|
|
|
process::exit(-1);
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! try_fail {
|
|
|
|
($val:expr, $format:expr) => ( {
|
|
|
|
match $val {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(err) => fail!($format, err)
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
($val:expr, $format:expr, $( $arg:expr ),+) => ( {
|
|
|
|
match $val {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(err) => fail!($format, $( $arg ),+, err)
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|