From fa49e0bf71ccd4bc98df0fa83546250c1b849e2e Mon Sep 17 00:00:00 2001 From: stuebinm Date: Sun, 11 Apr 2021 03:37:32 +0200 Subject: simple ffi with rust and haskell so far, only calling rust from haskell, not the other way round. --- rusty-haskell/haskell/.gitignore | 1 + rusty-haskell/haskell/rust-haskell-ffi.cabal | 26 ++++++++++++++++++++++++++ rusty-haskell/haskell/src/FLib.hs | 8 ++++++++ rusty-haskell/haskell/src/Main.hs | 16 ++++++++++++++++ rusty-haskell/rust/.gitignore | 1 + rusty-haskell/rust/Cargo.lock | 6 ++++++ rusty-haskell/rust/Cargo.toml | 13 +++++++++++++ rusty-haskell/rust/src/lib.rs | 11 +++++++++++ 8 files changed, 82 insertions(+) create mode 100644 rusty-haskell/haskell/.gitignore create mode 100644 rusty-haskell/haskell/rust-haskell-ffi.cabal create mode 100644 rusty-haskell/haskell/src/FLib.hs create mode 100644 rusty-haskell/haskell/src/Main.hs create mode 100644 rusty-haskell/rust/.gitignore create mode 100644 rusty-haskell/rust/Cargo.lock create mode 100644 rusty-haskell/rust/Cargo.toml create mode 100644 rusty-haskell/rust/src/lib.rs (limited to 'rusty-haskell') diff --git a/rusty-haskell/haskell/.gitignore b/rusty-haskell/haskell/.gitignore new file mode 100644 index 0000000..b5e3679 --- /dev/null +++ b/rusty-haskell/haskell/.gitignore @@ -0,0 +1 @@ +dist-newstyle/* diff --git a/rusty-haskell/haskell/rust-haskell-ffi.cabal b/rusty-haskell/haskell/rust-haskell-ffi.cabal new file mode 100644 index 0000000..c540b57 --- /dev/null +++ b/rusty-haskell/haskell/rust-haskell-ffi.cabal @@ -0,0 +1,26 @@ +cabal-version: >=1.10 +-- Initial package description 'rust-haskell-ffi.cabal' generated by 'cabal +-- init'. For further documentation, see +-- http://haskell.org/cabal/users-guide/ + +name: rust-haskell-ffi +version: 0.1.0.0 +-- synopsis: +-- description: +-- bug-reports: +-- license: +license-file: LICENSE +author: stuebinm +maintainer: stuebinm@disroot.org +-- copyright: +-- category: +build-type: Simple +extra-source-files: CHANGELOG.md + +executable rust-haskell-ffi + main-is: src/Main.hs + other-extensions: ForeignFunctionInterface + build-depends: base >= 4.7 && < 5 + default-language: Haskell2010 + extra-libraries: rustlib, pthread + extra-lib-dirs: ../rust/target/release diff --git a/rusty-haskell/haskell/src/FLib.hs b/rusty-haskell/haskell/src/FLib.hs new file mode 100644 index 0000000..7fc1d61 --- /dev/null +++ b/rusty-haskell/haskell/src/FLib.hs @@ -0,0 +1,8 @@ +module FLib where + +import Foreign.C.Types +import Foreign.C.String + +foreign import ccall "double_input" doubleInput :: CInt -> CInt +--foreign import ccall unsafe "print_string" printString :: CString -> IO () +foreign import ccall "print_hello" printHello :: IO () diff --git a/rusty-haskell/haskell/src/Main.hs b/rusty-haskell/haskell/src/Main.hs new file mode 100644 index 0000000..2c6a7ae --- /dev/null +++ b/rusty-haskell/haskell/src/Main.hs @@ -0,0 +1,16 @@ +module Main where + + +import Foreign.C.Types +import Foreign.C.String + +foreign import ccall "double_input" doubleInput :: CInt -> CInt +--foreign import ccall unsafe "print_string" printString :: CString -> IO () +foreign import ccall "print_hello" printHello :: IO () + + +main :: IO () +main = do + let b = doubleInput 2 + printHello + putStrLn $ show b diff --git a/rusty-haskell/rust/.gitignore b/rusty-haskell/rust/.gitignore new file mode 100644 index 0000000..e420ee4 --- /dev/null +++ b/rusty-haskell/rust/.gitignore @@ -0,0 +1 @@ +target/* diff --git a/rusty-haskell/rust/Cargo.lock b/rusty-haskell/rust/Cargo.lock new file mode 100644 index 0000000..3cae764 --- /dev/null +++ b/rusty-haskell/rust/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "rustlib" +version = "0.1.0" + diff --git a/rusty-haskell/rust/Cargo.toml b/rusty-haskell/rust/Cargo.toml new file mode 100644 index 0000000..0bef17d --- /dev/null +++ b/rusty-haskell/rust/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "rustlib" +version = "0.1.0" +authors = ["stuebinm "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +name = "rustlib" +crate-type = ["staticlib"] + +[dependencies] diff --git a/rusty-haskell/rust/src/lib.rs b/rusty-haskell/rust/src/lib.rs new file mode 100644 index 0000000..d954f2b --- /dev/null +++ b/rusty-haskell/rust/src/lib.rs @@ -0,0 +1,11 @@ + + +#[no_mangle] +pub extern fn double_input(x: i32) -> i32 { + 2 * x +} + +#[no_mangle] +pub extern fn print_hello() { + println!("hello, world, from rust!"); +} -- cgit v1.2.3