ffi

How to publish a constant string in the Rust FFI?

浪尽此生 提交于 2019-12-05 07:01:52
I want to have a Rust library expose a const char * static string to C, to be compatible with an existing interface (specifically librsync ). That is, the C header file has extern char const *my_string; In C, the library would simply have char const *my_string = "hi"; In Rust I've tried something like pub static my_string: *const libc::c_char = unsafe { "hi\0" as *const libc::c_char }; but this complains error: casting `&'static str` as `*const i8` is invalid It seems like I can't use CString etc because they won't be a compile-time constant expression. This gets a little weird, so bear with

Calling Haskell FFI Function Ptrs from C

老子叫甜甜 提交于 2019-12-05 06:27:59
I am trying to get the following code to work: sample_hs :: CInt -> (CInt -> CInt) sample_hs x = (x+) foreign export ccall sample_hs :: CInt -> (CInt -> CInt) I would like to be able to do something like this in c: pf = sample_hs(2); result = pf(3); //Should be 5; When I try to do this, however, I get an error message: error: too few arguments to function ‘sample_hs’ I am guessing that the interface between the language isn't working how I thought it would. Is there a way to do what I'm trying to do? It is possible, FFI Does allow Higher-Order functions to be exported. Some modifications to

How do I initialize an opaque C struct when using Rust FFI?

家住魔仙堡 提交于 2019-12-05 03:29:10
Here's what I would like to do in C code: #include <some_lib.h> int main() { some_lib_struct_t x; some_lib_func(&x); } How do I make use of the library in Rust? Here's what I've got so far: extern crate libc; // 0.2.51 struct some_lib_struct_t; #[link(name = "some_lib")] extern "C" { fn some_lib_func(x: *mut some_lib_struct_t); } fn main() { let mut x: some_lib_struct_t; unsafe { some_lib_func(&mut x); } } When compiling I get an error: error[E0381]: borrow of possibly uninitialized variable: `x` --> src/main.rs:13:23 | 13 | some_lib_func(&mut x); | ^^^^^^ use of possibly uninitialized `x`

Testing FFI Code (with “foreign import”s) with GHCi

不羁的心 提交于 2019-12-05 01:48:36
Good (your local time of day), everyone. I went through Real World Haskell's chapter on the Foreign Function Interface, and did some follow-up reading here . I'm now experimenting with binding to C functions, and I'd like some clarification on some things. The following is fairly clear: foreign import ccall unsafe "math.h sin" c_sin :: CDouble -> CDouble I can load this and code that uses it in ghci, and everything is fine. It even loads in the embedded ghci in emacs's Haskell mode. I find this great for testing. math is a system library so this is straight-forward. Now an example from Real

unable to install compass

孤者浪人 提交于 2019-12-05 00:59:46
Last week I made an update in cygwin because of the bash "vulnerable problem". After that I couldn't compile sass anymore. I tried to reinstall ruby, after some install fails and reinstalling cygwin I installed it and updated with "gem update --system" it run, but when I tried to install compass I got following message: > /usr/bin/ruby.exe -r ./siteconf20141006-7856-1td7wzb.rb extconf.rb > checking for ffi.h... *** extconf.rb failed *** Could not create > Makefile due to some reason, probably lack of necessary libraries > and/or headers. Check the mkmf.log file for more details. You may > need

What is the correct type for returning a C99 `bool` to Rust via the FFI?

两盒软妹~` 提交于 2019-12-05 00:48:02
A colleague and I have been scratching our heads over how to return a bool from <stdbool.h> (a.k.a. _Bool ) back to Rust via the FFI. We have our C99 code we want to use from Rust: bool myfunc(void) { ... } We let Rust know about myfunc using an extern C block: extern "C" { fn myfunc() -> T; } What concrete type should T be? Rust doesn't have a c_bool in the libc crate, and if you search the internet, you will find various GitHub issues and RFCs where people discuss this, but don't really come to any consensus as to what is both correct and portable: https://github.com/rust-lang/rfcs/issues

How do I use C preprocessor macros with Rust's FFI?

社会主义新天地 提交于 2019-12-05 00:24:18
I'm writing some code that interfaces an existing library written in C. In my Rust code I'd like to be able to use values from CPP macros. If I have a C include.h that looks like this: #define INIT_FLAG 0x00000001 I'd like to be able to use it in Rust like this: #[link(name="mylib")] extern { pub static init_flag: c_int = INIT_FLAG; } I've looked at other FFI code and I see a lot of people duplicating these values in Rust instead of getting them from the FFI. This seems a little brittle, and I'd also like to be able to handle more complicated things that are defined via CPP macros. Running cpp

Is it possible to create PHP extensions in Haskell?

荒凉一梦 提交于 2019-12-05 00:20:01
Is it possible to create PHP extensions with Haskell? Usually PHP extensions are written using C. Is using Haskell FFI to provide functionality for a stub C extension possible (or even a good idea)? What are the caveats to such an approach? Does Zend perform some magic in the background that would disrupt such a scheme? You can certainly do this, though I'm not sure anyone has tried. Haskell interoperates with C via its FFI mechanism , and you can certainly: Call Haskell from C so if you can call C from PHP, and that C calls Haskell, you're in business. Why would you want to do this? Faster,

How to catch a Haskell exception that is thrown in a Haskell callback function called by a C function?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 00:12:31
Is there any good way to catch a haskell exception, which is thrown in a haskell callback function called by a c function? For example, let me have a simple c function which just calls a given callback, void callmeback ( void (*callback) () ) { callback (); } and a haskell code which uses this function via ffi. {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE ForeignFunctionInterface #-} module Main (main) where import Foreign import Control.Exception import Data.Typeable foreign import ccall safe "wrapper" mkCallback :: IO () -> IO (FunPtr (IO ())) foreign import ccall safe "callmeback"

Storable empty data declaration

南笙酒味 提交于 2019-12-04 19:03:48
问题 I'm attempting to create a Haskell wrapper for a C library. The underlying structs are too complicated to express as explicit types, and I don't actually use them other than for passing between C functions, so I'm using EmptyDataDecls to let GHC work it out for me. What I need is a pointer to one of these data types, but when I attempt to create one with alloca it complains that the data is not of the type Storable . For example: {-# LANGUAGE ForeignFunctionInterface, EmptyDataDecls #-}