Skip to content

Instantly share code, notes, and snippets.

@mzumi
Last active November 2, 2016 03:45
Show Gist options
  • Save mzumi/a1fc670bb0267b228904f29f62a286f5 to your computer and use it in GitHub Desktop.
Save mzumi/a1fc670bb0267b228904f29f62a286f5 to your computer and use it in GitHub Desktop.
#![feature(libc)]
extern crate libc;
use libc::*;
use std::ffi::{CStr, CString};
#[repr(C)]
pub struct SampleStruct {
i: i32,
b: bool,
s: *const c_char
}
#[no_mangle]
pub extern fn create_sample_struct() -> *mut SampleStruct {
let b = Box::new(SampleStruct {
i: 1,
b: true,
s: CString::new("hello").unwrap().into_raw()
});
Box::into_raw(b)
}
#[no_mangle]
pub extern fn free_sample_struct(s: *mut SampleStruct) {
unsafe {
drop(Box::from_raw(s));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment