Skip to content

Commit b6eab59

Browse files
committed
Initial commit
0 parents  commit b6eab59

File tree

13 files changed

+196
-0
lines changed

13 files changed

+196
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
*/target
2+
*/Cargo.lock

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Super rough examples of calling Rust functions from Python via FFI.
2+
3+
Everything uses Python 3, the "cffi" (`pip install cffi`) library, and rustc 1.0.0-nightly (12b846ab8 2015-03-09).
4+
5+
After running `cargo build` be sure to update the call to ffi.dlopen in the python files.

burn/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[package]
2+
3+
name = "burn"
4+
version = "0.0.1"
5+
authors = ["Dan Callahan <dan.callahan@gmail.com>"]
6+
7+
[lib]
8+
name = "burn"
9+
crate-type = ["dylib"]

burn/burn.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
from cffi import FFI
5+
ffi = FFI()
6+
7+
ffi.cdef("""
8+
long triple(long long x);
9+
""")
10+
11+
C = ffi.dlopen("target/debug/libburn-ea11bffea0950ae2.dylib")
12+
13+
print(C.triple(3))

burn/gil.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
from threading import Thread
5+
6+
7+
def count(i):
8+
print("Thread {} starting".format(i))
9+
10+
x = 0
11+
while x < 2**23:
12+
x += 1
13+
14+
print("Thread {} returned".format(i))
15+
16+
17+
def main():
18+
thread_list = []
19+
20+
for i in range(10):
21+
t = Thread(target=count, args=(i,))
22+
thread_list.append(t)
23+
24+
for thread in thread_list:
25+
thread.start()
26+
27+
for thread in thread_list:
28+
thread.join()
29+
30+
print("Completely done")
31+
32+
33+
if __name__ == '__main__':
34+
main()

burn/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#![feature(core)]
2+
3+
extern crate core;
4+
5+
use std::thread;
6+
use core::num::Int;
7+
8+
#[no_mangle]
9+
pub extern fn triple(x: i64) -> i64 {
10+
11+
let _: Vec<_> = (0..10).map(|i| {
12+
thread::scoped(move || {
13+
let mut x = 0;
14+
println!("Thread {} running", i);
15+
while x < 2.pow(23) {
16+
x += 1
17+
}
18+
println!("Thread {} returning", i);
19+
})
20+
}).collect();
21+
22+
x * 3
23+
}
24+
25+
#[test]
26+
fn it_works() {
27+
assert!(triple(3) == 9);
28+
}

calc/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
target
2+
Cargo.lock

calc/Cargo.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
[package]
2+
3+
name = "calc"
4+
version = "0.0.1"
5+
authors = ["Dan Callahan <dan.callahan@gmail.com>"]
6+
7+
[lib]
8+
name = "calc"
9+
crate-type = ["dylib"]
10+
11+
[dependencies]
12+
13+
libc = "0.1.3"

calc/calc.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
from cffi import FFI
5+
6+
ffi = FFI()
7+
ffi.cdef("""
8+
long long calculate(const char *script);
9+
""")
10+
C = ffi.dlopen("target/release/libcalc-35d9862849daffe3.dylib")
11+
12+
def main():
13+
result = C.calculate(b"+ * * + * - /")
14+
print(result)
15+
16+
if __name__ == '__main__':
17+
main()
18+

calc/src/lib.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// {{{ We'll look at this later...
2+
#![allow(dead_code)]
3+
4+
extern crate libc;
5+
6+
use libc::c_char;
7+
use std::ffi::CStr;
8+
use std::str;
9+
10+
// {{{ fn calculate(c_buf: *const c_car) -> i64 {...}
11+
#[no_mangle]
12+
pub extern fn calculate(c_buf: *const c_char) -> i64 {
13+
let buf = unsafe { CStr::from_ptr(c_buf).to_bytes() };
14+
let slice = str::from_utf8(buf).unwrap();
15+
calc(slice)
16+
} // }}}
17+
// }}}
18+
19+
fn calc(script: &str) -> i64 {
20+
let mut accumulator = 0;
21+
22+
for c in script.chars() {
23+
match c {
24+
'+' => accumulator += 1,
25+
'-' => accumulator -= 1,
26+
'*' => accumulator *= 2,
27+
'/' => accumulator /= 2,
28+
_ => { /* ignore other characters */ }
29+
}
30+
}
31+
32+
accumulator
33+
}
34+
35+
#[test]
36+
fn it_works() {
37+
assert!(calc("+ + * - /") == 1)
38+
}
39+
40+
// vim: foldmethod=marker:foldenable

0 commit comments

Comments
 (0)