ffi

How to lend a Rust object to C code for an arbitrary lifetime?

≡放荡痞女 提交于 2020-05-14 20:07:33
问题 I'm writing a library in Rust that has a C interface. C side must be able to create and destroy Rust objects (C side owns them and controls their lifetime). I've managed to "leak" an object to C, but I'm not sure how to properly free it: pub extern "C" fn create() -> *mut Foo { let obj = Foo; // oops, a bug let ptr = std::mem::transmute(&mut obj); // bad std::mem::forget(obj); // not needed return ptr; } pub extern "C" fn destroy(handle: *mut Foo) { // get Foo back and Drop it??? } I'm not

How do I use the output of a program from an earlier part of a Stack/Cabal build as source in a later part of the same build?

五迷三道 提交于 2020-05-14 16:44:25
问题 I have a very peculiar dependency situation that I would like to package up in a single Stack/Cabal package: I need to build and run my program to get the input to a code-generator which produces output that needs to be linked in to... my program. OK so in more concrete terms, here are the steps manually: stack build to install all dependencies, and build all non-Verilator-using executables. stack exec phase1 to run the first phase which generates, among other things, a Verilog file and a

How do I use the output of a program from an earlier part of a Stack/Cabal build as source in a later part of the same build?

橙三吉。 提交于 2020-05-14 16:43:05
问题 I have a very peculiar dependency situation that I would like to package up in a single Stack/Cabal package: I need to build and run my program to get the input to a code-generator which produces output that needs to be linked in to... my program. OK so in more concrete terms, here are the steps manually: stack build to install all dependencies, and build all non-Verilator-using executables. stack exec phase1 to run the first phase which generates, among other things, a Verilog file and a

Calling Haskell functions from Python

坚强是说给别人听的谎言 提交于 2020-03-30 18:32:09
问题 I want to use some Haskell libraries (e.g. Darcs, Pandoc) from Python, but it seems there’s no direct foreign function interface to Haskell in Python. Is there any way to do that? 回答1: Provided you can get your Python code to call C, you can call Haskell functions that have been exported via the FFI Another approach would be to write a standard IPC interface, in the case of darcs and pandoc just calling them as vanilla executables and parsing their output might be the way to go. As to

Calling Haskell functions from Python

房东的猫 提交于 2020-03-30 18:30:54
问题 I want to use some Haskell libraries (e.g. Darcs, Pandoc) from Python, but it seems there’s no direct foreign function interface to Haskell in Python. Is there any way to do that? 回答1: Provided you can get your Python code to call C, you can call Haskell functions that have been exported via the FFI Another approach would be to write a standard IPC interface, in the case of darcs and pandoc just calling them as vanilla executables and parsing their output might be the way to go. As to

Setting the include path with bindgen

痞子三分冷 提交于 2020-03-18 11:22:56
问题 I'm writing a Rust interface to a small C library, which has headers spread in a few locations. It's not a system library, and is normally used by some executables in the same package; I'm currently including it as a git submodule in my Cargo project. Building the library seems to be pretty easy; I've opted to use the gcc crate from build.rs : gcc::Config::new() .file("external/foo/dir1/file1.c") .file("external/foo/dir2/file2.c") .include("external/foo/dir1/") .include("external/foo/dir2/")

Converting jni::sys::JNIEnv to JNINativeInterface defined in ffi

眉间皱痕 提交于 2020-02-25 04:06:30
问题 I am following up on Casting a borrowed reference with a lifetime to a raw pointer in Rust, which solved the wrong problem. Please consider the following code: extern crate jni; extern crate ffi; use jni::JNIEnv; use jni::objects::JClass; use jni::sys::{jint, jlong, jobject}; struct CameraAppEngine { _env: *mut jni::sys::JNIEnv, _width: i32, _height: i32 } impl CameraAppEngine { pub fn new(_env: *mut jni::sys::JNIEnv, _width: i32, _height: i32) -> CameraAppEngine { CameraAppEngine { _env,

Problem loading a library with FFI in PHP 7.4

ε祈祈猫儿з 提交于 2020-02-05 14:59:52
问题 I'm having trouble using a third party .so library in PHP with the new FFI. When I run this little piece of code: <?php $ffi = FFI::cdef('typedef int (*NFE_Nome)(const char* sNome, int* esTamanho);', 'libacbrnfe64.so'); PHP outputs me this error: double free or corruption (out) Aborted (core dumped) Is this a problem with the library itself, my PHP configuration or something else? It's confusing to me, because I can use normally this same library with this C++ code: #include <iostream>

Problem loading a library with FFI in PHP 7.4

回眸只為那壹抹淺笑 提交于 2020-02-05 14:59:48
问题 I'm having trouble using a third party .so library in PHP with the new FFI. When I run this little piece of code: <?php $ffi = FFI::cdef('typedef int (*NFE_Nome)(const char* sNome, int* esTamanho);', 'libacbrnfe64.so'); PHP outputs me this error: double free or corruption (out) Aborted (core dumped) Is this a problem with the library itself, my PHP configuration or something else? It's confusing to me, because I can use normally this same library with this C++ code: #include <iostream>

Problem loading a library with FFI in PHP 7.4

我的梦境 提交于 2020-02-05 14:58:46
问题 I'm having trouble using a third party .so library in PHP with the new FFI. When I run this little piece of code: <?php $ffi = FFI::cdef('typedef int (*NFE_Nome)(const char* sNome, int* esTamanho);', 'libacbrnfe64.so'); PHP outputs me this error: double free or corruption (out) Aborted (core dumped) Is this a problem with the library itself, my PHP configuration or something else? It's confusing to me, because I can use normally this same library with this C++ code: #include <iostream>