Last active
March 14, 2016 14:35
-
-
Save oakfang/822ba6ec16bd003b81b0 to your computer and use it in GitHub Desktop.
Using facade.dll to run rust code via python
This file contains 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
from facade import dll | |
import utils | |
print utils.multi_sum(3) |
This file contains 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
int multi_sum(int); |
This file contains 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
#![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}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
Run
rustc utils.rs
-> results inutils.dll
fileRunt
python test.py
-> results in 135I 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 oftest.py