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))