Last active
January 16, 2016 03:03
-
-
Save psychoss/ceeb7bf4b913ce17681d to your computer and use it in GitHub Desktop.
Rust Foreign Function Interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ptr; | |
pub type c_int = i32; | |
pub type time_t = i64; | |
pub type c_long = i64; | |
pub type c_char = i8; | |
pub type suseconds_t = i64; | |
#[repr(u8)] | |
enum c_void { | |
__variant1, | |
__variant2, | |
} | |
#[repr(C)] | |
pub struct tm { | |
pub tm_sec: ::c_int, | |
pub tm_min: ::c_int, | |
pub tm_hour: ::c_int, | |
pub tm_mday: ::c_int, | |
pub tm_mon: ::c_int, | |
pub tm_year: ::c_int, | |
pub tm_wday: ::c_int, | |
pub tm_yday: ::c_int, | |
pub tm_isdst: ::c_int, | |
pub tm_gmtoff: ::c_long, | |
pub tm_zone: *mut ::c_char, | |
} | |
#[repr(C)] | |
pub struct timeval { | |
pub tv_sec: ::time_t, | |
pub tv_usec: ::suseconds_t, | |
} | |
pub struct timezone { | |
pub tz_minuteswest: ::c_int, | |
pub tz_dsttime: ::c_int, | |
} | |
#[link_name = "c"] | |
extern "C" { | |
fn time(timer: *const ()) -> time_t; | |
fn localtime(timer: *const time_t) -> *const tm; | |
fn gmtime(timer: *const time_t) -> *const tm; | |
fn gettimeofday(tp: *mut ::timeval, | |
tz: *mut ::c_void) -> ::c_int; | |
} | |
fn main() { | |
let timer: *const time_t = unsafe { &time(ptr::null()) as *const time_t }; | |
println!("{:?}", unsafe { | |
let t = localtime(timer); | |
((*t).tm_mon+1,(*t).tm_mday,(*t).tm_year+1900) | |
}); | |
let mut tv = timeval { tv_sec: 0, tv_usec: 0 }; | |
unsafe { gettimeofday(&mut tv, ptr::null_mut()) }; | |
println!("{:?}",tv.tv_sec); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std::ops::{Deref, DerefMut}; | |
use std::slice; | |
use std::mem; | |
#[cfg(any(target_arch = "x86", | |
target_arch = "arm", | |
target_arch = "mips", | |
target_arch = "mipsel", | |
target_arch = "powerpc", | |
target_arch = "le32"))] | |
#[allow(non_camel_case_types)] | |
type size_t = u32; | |
#[cfg(any(target_arch = "x86_64", | |
target_arch = "aarch64"))] | |
#[allow(non_camel_case_types)] | |
type size_t = u64; | |
#[repr(u8)] | |
#[allow(non_camel_case_types)] | |
enum c_void { | |
__variant1, | |
__variant2, | |
} | |
#[link_name = "c"] | |
extern { | |
fn calloc(num: size_t, size: size_t) -> *mut c_void; | |
fn free(ptr: *mut c_void); | |
fn abort(); | |
} | |
impl <T: Copy> Drop for ZeroArray<T> { | |
fn drop(&mut self) { | |
unsafe { free(self.ptr as *mut c_void); } | |
} | |
} | |
impl <T: Copy> Deref for ZeroArray<T> { | |
type Target = [T]; | |
fn deref(&self) -> &[T] { | |
unsafe { slice::from_raw_parts(self.ptr, self.size) } | |
} | |
} | |
impl <T: Copy> DerefMut for ZeroArray<T> { | |
fn deref_mut(&mut self) -> &mut [T] { | |
unsafe { slice::from_raw_parts_mut(self.ptr, self.size) } | |
} | |
} | |
pub struct ZeroArray<T> where T: Copy { | |
ptr: *mut T, | |
size: usize, | |
} | |
impl <T: Copy> ZeroArray<T> { | |
pub unsafe fn new(size: u32) -> ZeroArray<T> { | |
let p = calloc(size as size_t, mem::size_of::<T>() as size_t); | |
if p.is_null() { // OOM | |
abort(); | |
} | |
ZeroArray { | |
ptr: p as *mut T, | |
size: size as usize | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment