From e37bd54df43461cd84f0f11e369daf4d459b100f Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Sun, 19 Apr 2015 10:44:03 -0700 Subject: [PATCH] Add Python to Rust example --- python-to-rust/Cargo.toml | 8 ++++++++ python-to-rust/Makefile | 9 +++++++++ python-to-rust/src/lib.rs | 4 ++++ python-to-rust/src/main.py | 8 ++++++++ 4 files changed, 29 insertions(+) create mode 100644 python-to-rust/Cargo.toml create mode 100644 python-to-rust/Makefile create mode 100644 python-to-rust/src/lib.rs create mode 100644 python-to-rust/src/main.py diff --git a/python-to-rust/Cargo.toml b/python-to-rust/Cargo.toml new file mode 100644 index 0000000..e1f1675 --- /dev/null +++ b/python-to-rust/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "python-to-rust" +version = "0.1.0" +authors = ["Alex Crichton "] + +[lib] +name = "double_input" +crate-type = ["dylib"] diff --git a/python-to-rust/Makefile b/python-to-rust/Makefile new file mode 100644 index 0000000..583a843 --- /dev/null +++ b/python-to-rust/Makefile @@ -0,0 +1,9 @@ +all: target/libdouble_input.so + python src/main.py + +target/libdouble_input.so: src/lib.rs Cargo.toml + cargo build + (cd target && ln -nsf debug/libdouble_input-* libdouble_input.so) + +clean: + rm -rf target diff --git a/python-to-rust/src/lib.rs b/python-to-rust/src/lib.rs new file mode 100644 index 0000000..bf573d1 --- /dev/null +++ b/python-to-rust/src/lib.rs @@ -0,0 +1,4 @@ +#[no_mangle] +pub extern fn double_input(input: i32) -> i32 { + input * 2 +} diff --git a/python-to-rust/src/main.py b/python-to-rust/src/main.py new file mode 100644 index 0000000..19a02c6 --- /dev/null +++ b/python-to-rust/src/main.py @@ -0,0 +1,8 @@ +from ctypes import cdll + +lib = cdll.LoadLibrary('target/libdouble_input.so') +double_input = lib.double_input + +input = 4 +output = double_input(input) +print(str(input) + " * 2 = " + str(output))