Skip to content

Instantly share code, notes, and snippets.

@dnaeon
Last active August 29, 2015 14:24
Show Gist options
  • Save dnaeon/496cf15f0e02fd5dfacf to your computer and use it in GitHub Desktop.
Save dnaeon/496cf15f0e02fd5dfacf to your computer and use it in GitHub Desktop.
test-rust-lib
extern crate libc;
use std::{boxed, ffi, ptr};
use libc::c_char;
#[repr(C)]
#[derive(Copy)]
pub struct Plugin {
key: *const c_char,
}
impl Clone for Plugin {
fn clone(&self) -> Self { *self }
}
impl Plugin {
pub fn new(key: Option<&str>) -> Plugin {
let c_key: *const c_char = match key {
Some(ref k) => ffi::CString::new(*k).unwrap().as_ptr(),
None => ptr::null(),
};
Plugin {
key: c_key,
}
}
}
#[no_mangle]
pub extern fn get_plugins() -> boxed::Box<[Plugin]> {
let plugins = [
Plugin::new(Some("my.plugin.foo")),
Plugin::new(Some("my.other.plugin.bar")),
Plugin::new(None),
];
boxed::Box::new(plugins)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment