perl-to-rust

This commit is contained in:
Hao Wu
2015-04-24 15:51:08 -07:00
parent 858886c0d1
commit 134007b116
4 changed files with 42 additions and 0 deletions

9
perl-to-rust/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "perl-to-rust"
version = "0.1.0"
#modified based on python version
authors = ["Hao Wu <echowuhao@gmail.com>"]
[lib]
name = "double_input"
crate-type = ["dylib"]

15
perl-to-rust/Makefile Normal file
View File

@@ -0,0 +1,15 @@
ifeq ($(shell uname),Darwin)
EXT := dylib
else
EXT := so
endif
all: target/libdouble_input.so
perl src/main.pl
target/libdouble_input.so: src/lib.rs Cargo.toml
cargo build
(cd target && ln -nsf debug/libdouble_input-*$(EXT) libdouble_input.so)
clean:
rm -rf target

4
perl-to-rust/src/lib.rs Normal file
View File

@@ -0,0 +1,4 @@
#[no_mangle]
pub extern fn double_input(input: i32) -> i32 {
input * 2
}

14
perl-to-rust/src/main.pl Normal file
View File

@@ -0,0 +1,14 @@
use v5.10;
use FFI::Raw;
my $double_input = FFI::Raw->new(
"target/libdouble_input.so",
'double_input',
FFI::Raw::int, # return value
FFI::Raw::int # arg #1
);
my $input = 4;
my $output = $double_input->call($input);
say $input . " * 2 = " . $output;