Merge pull request #32 from Antiarchitect/add-rust-to-go

Add rust-to-go-dynamic example.
This commit is contained in:
Alex Crichton
2019-08-19 08:05:03 -05:00
committed by GitHub
4 changed files with 45 additions and 0 deletions

View File

@@ -0,0 +1,10 @@
[package]
name = "rust-to-go"
version = "0.1.0"
authors = ["Andrey Voronkov <voronkovaa@gmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
libc = "0.2"

View File

@@ -0,0 +1,13 @@
use std::process::Command;
fn main() {
println!(r"cargo:rustc-link-search=target/debug");
let os = Command::new("uname")
.output().unwrap();
let ext = match String::from_utf8_lossy(os.stdout.as_slice()).into_owned().trim_end().as_ref() {
"Darwin" => "dylib",
_ => "so"
};
Command::new("go").args(&["build", "-o", &format!("target/debug/libdouble_input.{}", ext), "-buildmode=c-shared", "src/double.go"])
.status().unwrap();
}

View File

@@ -0,0 +1,10 @@
package main
import "C"
//export DoubleInput
func DoubleInput(input int32) int32 {
return input * 2
}
func main() {}

View File

@@ -0,0 +1,12 @@
extern crate libc;
#[link(name = "double_input")]
extern {
fn DoubleInput(input: libc::c_int) -> libc::c_int;
}
fn main() {
let input = 2;
let output = unsafe { DoubleInput(input) };
println!("{} * 2 = {}", input, output);
}