ffi

How to expose a Rust `Vec<T>` to FFI?

拥有回忆 提交于 2019-12-18 12:54:16
问题 I'm trying to construct a pair of elements: array: *mut T array_len: usize array is intended to own the data However, Box::into_raw will return *mut [T] . I cannot find any info on converting raw pointers to slices. What is its layout in memory? How do I use it from C? Should I convert to *mut T ? If so, how? 回答1: If you just want some C function to mutably borrow the Vec , you can do it like this: extern "C" { fn some_c_function(ptr: *mut i32, len: ffi::size_t); } fn safe_wrapper(a: &mut

Working with c_void in an FFI

假装没事ソ 提交于 2019-12-18 11:34:56
问题 I am struggling with passing a struct through an FFI that accepts void and reading it back on the other end. The library in question is libtsm, a terminal state machine. It allows you to feed input and then find out in which state a terminal would be after the input. It declares its draw function as: pub fn tsm_screen_draw(con: *tsm_screen, draw_cb: tsm_screen_draw_cb, data: *mut c_void) -> tsm_age_t; where tsm_screen_draw_cb is a callback to be implemented by the library user, with the

Error installing gems that use native extensions on Ubuntu, Ruby 1.9.2 via RVM

余生颓废 提交于 2019-12-18 10:55:18
问题 I get an error while trying to install the ffi gem: ~ - 16:54>gem i ffi Building native extensions. This could take a while... ERROR: Error installing ffi: ERROR: Failed to build gem native extension. rake RUBYARCHDIR=/home/mdemare/.rvm/gems/ruby-1.9.2-p136/gems/ffi-1.0.6/lib RUBYLIBDIR=/home/mdemare/.rvm/gems/ruby-1.9.2-p136/gems/ffi-1.0.6/lib /home/mdemare/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/site_ruby/1.9.1/rubygems.rb:370:in `bin_path': can't find gem rake ([">= 0"]) with executable rake

Difference between hsc2hs and c2hs?

假如想象 提交于 2019-12-18 10:52:52
问题 What is the difference between hsc2hs and c2hs? I know what hsc2hs is a preprocessor but what does it exactly do? And c2hs can make Haskell modules from C-code, but do I need hsc2hs for this? 回答1: They both have the same function: make it easier to write FFI bindings. You don't need to know about hsc2hs if you chose to use c2hs; they are independent. C2hs is more powerful, but also more complicated: Edward Z. Yang illustrates this point with a nice diagram in his c2hs tutorial: When should I

Statically linking a C library with a Haskell library

一世执手 提交于 2019-12-17 23:28:07
问题 I have a Haskell project that aims to create some C++ bindings. I've written the C wrappers and compiled them into a stand-alone statically linked library. I'd like to write the Haskell bindings to link statically to the C wrappers so that I don't have to distribute the C wrappers separately but I can't seem to get it working and would appreciate some help. I specify the C library as an extra library but my cabal build step doesn't seem to add it to compile command. I've created a small

ERROR: Error installing ffi: ERROR: Failed to build gem native extension

僤鯓⒐⒋嵵緔 提交于 2019-12-17 19:33:18
问题 Got the DevKit installed and re-ran the ffi install….got this as an output: C:\Documents and Settings\******>gem install ffi Temporarily enhancing PATH to include DevKit... Building native extensions. This could take a while... ERROR: Error installing ffi: ERROR: Failed to build gem native extension. C:/Ruby192/bin/ruby.exe extconf.rb checking for ffi.h... no checking for ffi.h in /usr/local/include... no checking for rb_thread_blocking_region()... yes checking for ruby_thread_has_gvl_p()...

Python 3 replacement for PyFile_AsFile

一笑奈何 提交于 2019-12-17 16:47:07
问题 The following code works in Python 2: from ctypes import * ## Setup python file -> c 'FILE *' conversion : class FILE(Structure): pass FILE_P = POINTER(FILE) PyFile_AsFile = pythonapi.PyFile_AsFile # problem here PyFile_AsFile.argtypes = [py_object] PyFile_AsFile.restype = FILE_P fp = open(filename,'wb') gd.gdImagePng(img, PyFile_AsFile(fp)) But in Python 3, there is no PyFile_AsFile in pythonapi. The code is an except from testPixelOps.py. 回答1: I just needed a way to convert a file object to

How do I convert a C string into a Rust string and back via FFI?

若如初见. 提交于 2019-12-17 06:29:22
问题 I'm trying to get a C string returned by a C library and convert it to a Rust string via FFI. mylib.c const char* hello(){ return "Hello World!"; } main.rs #![feature(link_args)] extern crate libc; use libc::c_char; #[link_args = "-L . -I . -lmylib"] extern { fn hello() -> *c_char; } fn main() { //how do I get a str representation of hello() here? } 回答1: The best way to work with C strings in Rust is to use structures from the std::ffi module, namely CStr and CString. CStr is a dynamically

Ruby VIPS image.write_to_memory yields different results in different environments

做~自己de王妃 提交于 2019-12-13 20:17:10
问题 We need to open an image and write its pixels to memory, so a C library can further process it. We're doing this with the lines below: image = Vips::Image.new_from_file(filename) pixels_pointer = image.write_to_memory However, it seems that what's written in memory differs per environment. We're testing this on a macOS High Sierra and on an Ubuntu Xenial via Docker, with vips-8.8.0 and ruby-vips (2.0.13) . We've MD5-ed the result of writing to memory: p Digest::MD5.hexdigest(pixels_pointer)

What's the right way to create OsStr(ing) from a FFI-returned slice?

大城市里の小女人 提交于 2019-12-13 14:33:41
问题 I have a function that accepts a callback with args data: *const u8, length: usize , that represents some path. What is the right way to create an OsStr(ing) from this? There's from_byte_slice in OsStrExt , but seems like it doesn't check if the data is correct WTF-8 or whatever, and it's not clear how to use it. 回答1: You can use from_raw_parts to go from the raw pointer to a slice, then OsStrExt::from_bytes: use std::slice; use std::ffi::OsStr; use std::os::unix::ffi::OsStrExt; // NOTE