Skip to content

Instantly share code, notes, and snippets.

@oakfang
Last active March 14, 2016 14:35
Show Gist options
  • Save oakfang/822ba6ec16bd003b81b0 to your computer and use it in GitHub Desktop.
Save oakfang/822ba6ec16bd003b81b0 to your computer and use it in GitHub Desktop.
Using facade.dll to run rust code via python
from facade import dll
import utils
print utils.multi_sum(3)
int multi_sum(int);
#![crate_type = "dylib"]
use std::thread;
use std::sync::mpsc;
#[no_mangle]
pub extern fn multi_sum(value: i32) -> i32 {
let (tx, rx) = mpsc::channel();
let mut answers: [i32; 10] = [0;10];
for i in 0..10 {
let tx = tx.clone();
thread::spawn(move || {
let answer = i * value;
tx.send((i, answer)).unwrap();
});
}
for _ in 0..10 {
match rx.recv() {
Ok((i, answer)) => answers[i as usize] = answer,
Err(error) => println!("{}", error),
}
}
return answers.into_iter().fold(0, |acc, n| {acc + n});
}
@oakfang
Copy link
Author

oakfang commented Mar 14, 2016

Usage:
Run rustc utils.rs -> results in utils.dll file
Runt python test.py -> results in 135

I would like to stress the fact that the utils.h is not required, but it does save me the python code: utils.declare('int multi_sum(int);') @ line 3 of test.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment