vpncloud/src/util.rs

60 lines
1.3 KiB
Rust
Raw Normal View History

use std::{mem, slice};
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
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)
}
} );
}