Skip to content

Instantly share code, notes, and snippets.

@eddyb
Last active November 13, 2015 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddyb/2e7fa23f604304c471a3 to your computer and use it in GitHub Desktop.
Save eddyb/2e7fa23f604304c471a3 to your computer and use it in GitHub Desktop.
[package]
authors = ["Mika Attila <radiantstatue@gmail.com>"]
name = "plugintest"
version = "0.1.0"
[dependencies]
dylib = "0.0.2"
[lib]
name = "guest"
crate-type = ["dylib"]
extern crate dylib;
use dylib::DynamicLibrary;
use std::path::Path;
#[inline(never)]
pub fn drop_it_host(_: Box<u8>) {}
// Change this to switch between host and plugin drops.
const DROP_IN_HOST: bool = true;
fn main() {
let dl = DynamicLibrary::open(Some(&Path::new("target/debug/libguest.so"))).unwrap();
unsafe {
let gimme: fn() -> Box<u8> = std::mem::transmute(dl.symbol::<()>("gimme").unwrap());
let drop_it: fn(Box<u8>) -> String = std::mem::transmute(dl.symbol::<()>("drop_it").unwrap());
loop {
let x = gimme();
println!("{}", x);
if DROP_IN_HOST {
drop_it_host(x);
} else {
drop_it(x);
}
}
}
}
#[no_mangle]
pub fn gimme() -> Box<u8> {
Box::new(42)
}
#[inline(never)]
#[no_mangle]
pub fn drop_it(_: Box<u8>) {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment