ffi

Segmentation fault when using C callback user data to store a boxed Rust closure

ぃ、小莉子 提交于 2020-08-05 09:59:05
问题 I am creating a Rust wrapper around a C API. One function in this C API sets a callback and accepts a void pointer which will be passed to the callback. It stores a reference to the callback and user data for later, so I am using the last code section from this answer. Here is my code. The Test::trigger_callback(...) function is meant to emulate the C library calling the callback. extern crate libc; use libc::c_void; use std::mem::transmute; struct Test { callback: extern "C" fn(data: i32,

dart/flutter: CustomPaint updates at a lower rate than ValueNotifier's value update

旧街凉风 提交于 2020-07-23 07:50:07
问题 I use dart FFI to retrieve data from native side, and show the data with flutter CustomPaint . I use ValueNotifier to control CustomPaint repaint. Code: Poll data at a rate With a state class, I poll data from native side periodically, and assign it to the ValueNotifier . class _ColorViewState extends State<ColorView> { ValueNotifier<NativeColor> _notifier; Timer _pollTimer; @override void initState() { // TODO: implement initState super.initState(); ffiInit(); // initialize notifier

dart/flutter: CustomPaint updates at a lower rate than ValueNotifier's value update

风格不统一 提交于 2020-07-23 07:49:11
问题 I use dart FFI to retrieve data from native side, and show the data with flutter CustomPaint . I use ValueNotifier to control CustomPaint repaint. Code: Poll data at a rate With a state class, I poll data from native side periodically, and assign it to the ValueNotifier . class _ColorViewState extends State<ColorView> { ValueNotifier<NativeColor> _notifier; Timer _pollTimer; @override void initState() { // TODO: implement initState super.initState(); ffiInit(); // initialize notifier

How to properly wrap a C function pointer in Rust? [duplicate]

自闭症网瘾萝莉.ら 提交于 2020-07-19 05:58:09
问题 This question already has answers here : Why does the address of an object change across methods? (2 answers) Closed 4 months ago . I have a C struct Foo with a function pointer. In my Rust bindings, I would like to allow users to set this function pointer, but I would like to avoid users having to deal with FFI types. foo.h struct Foo { void* internal; uint8_t a; void (*cb_mutate_a)(void*); }; struct Foo* foo_new(); void foo_free(struct Foo* foo); void foo_call(struct Foo* foo); foo.c struct

How to properly wrap a C function pointer in Rust? [duplicate]

走远了吗. 提交于 2020-07-19 05:58:08
问题 This question already has answers here : Why does the address of an object change across methods? (2 answers) Closed 4 months ago . I have a C struct Foo with a function pointer. In my Rust bindings, I would like to allow users to set this function pointer, but I would like to avoid users having to deal with FFI types. foo.h struct Foo { void* internal; uint8_t a; void (*cb_mutate_a)(void*); }; struct Foo* foo_new(); void foo_free(struct Foo* foo); void foo_call(struct Foo* foo); foo.c struct

Dart/Flutter ffi (Foreig Function Interface) native callbacks eg: sqlite3_exec

有些话、适合烂在心里 提交于 2020-06-29 04:19:04
问题 Hello I am using dart:ffi to build an interface with my native c/c++ library. and I needed a way to get a callback from c to dart as an example in sqlite: int sqlite3_exec( sqlite3*, /* An open database */ const char *sql, /* SQL to be evaluated */ int (*callback)(void*,int,char**,char**), /* Callback function */ void *, /* 1st argument to callback */ char **errmsg /* Error msg written here */ ); the third parameter in sqlite3_exec is function pointer to a callback. so if I called this

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

一个人想着一个人 提交于 2020-05-14 20:14:32
问题 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 to lend a Rust object to C code for an arbitrary lifetime?

元气小坏坏 提交于 2020-05-14 20:08:10
问题 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