ffi

How do I handle an FFI unsized type that could be owned or borrowed?

旧城冷巷雨未停 提交于 2019-12-31 01:54:31
问题 c_strange_t is an opaque C type that is only seen behind a pointer. When wrapping this type, there are times when it is our responsibility to free memory using c_free_strange_t(*c_strange_t) , and other times when we are not responsible for freeing the data, we are only responsible for accurately controlling the lifetime. It would be ergonomic if this type could be mapped into 2 types in Rust that work in a similar way to str and String , where there is impl Deref<Target=str> for String . The

Rust FFI passing trait object as context to call callbacks on

心不动则不痛 提交于 2019-12-30 11:05:14
问题 Okay, I'm trying to achieve the following: C calls into rust rust calls back into c and registers a callback on a user defined trait object c calls into rust with the context rust calls the callback on the context (trait object) I've been playing around with it quite a bit. I got quite far, but still not quite there. The C bit: #include <dlfcn.h> #include <stdio.h> void *global_ctx; void c_function(void* ctx) { printf("Called c_function\n"); global_ctx = ctx; } int main(void) { void *thing =

Rust FFI passing trait object as context to call callbacks on

大憨熊 提交于 2019-12-30 11:04:03
问题 Okay, I'm trying to achieve the following: C calls into rust rust calls back into c and registers a callback on a user defined trait object c calls into rust with the context rust calls the callback on the context (trait object) I've been playing around with it quite a bit. I got quite far, but still not quite there. The C bit: #include <dlfcn.h> #include <stdio.h> void *global_ctx; void c_function(void* ctx) { printf("Called c_function\n"); global_ctx = ctx; } int main(void) { void *thing =

Raw pointer turns null passing from Rust to C

核能气质少年 提交于 2019-12-29 08:57:25
问题 I'm attempting to retrieve a raw pointer from on C function in rust, and use that same raw pointer as an argument in another C function from another library. When I pass the raw pointer, I end up with a NULL pointer on the C side. I have tried to make a simplified version of my issue, but when I do it works as I would expect it to - C Code - struct MyStruct { int value; }; struct MyStruct * get_struct() { struct MyStruct * priv_struct = (struct MyStruct*) malloc( sizeof(struct MyStruct));

How to dereference a memory location from python ctypes?

瘦欲@ 提交于 2019-12-28 06:01:34
问题 I want to replicate the following c code in python ctypes: main() { long *ptr = (long *)0x7fff96000000; printf("%lx",*ptr); } I can figure out how to call this memory location as a function pointer but not just do a normal dereference: from ctypes import * """ >>> fptr = CFUNCTYPE(None, None) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.6/ctypes/__init__.py", line 104, in CFUNCTYPE class CFunctionType(_CFuncPtr): TypeError: Error when calling

How do I convert a Rust closure to a C-style callback?

余生颓废 提交于 2019-12-25 05:49:12
问题 I'm trying to write a Rusty wrapper for a piece of C API. There is one C construct I struggle with: typedef bool (*listener_t) (int, int); bool do_it(int x1, int y1, int x2, int y2, listener_t listener) The function does its job for a range of numbers unless the listener returns false. In that case it aborts computation. I want to have a Rust wrapper like this: fn do_with_callback<F>(start: (i32, i32), end: (i32, i32), callback: F) where F: Fn(i32, i32) -> bool rust-bindgen created this for

How do I convert a Rust closure to a C-style callback?

落爺英雄遲暮 提交于 2019-12-25 05:49:03
问题 I'm trying to write a Rusty wrapper for a piece of C API. There is one C construct I struggle with: typedef bool (*listener_t) (int, int); bool do_it(int x1, int y1, int x2, int y2, listener_t listener) The function does its job for a range of numbers unless the listener returns false. In that case it aborts computation. I want to have a Rust wrapper like this: fn do_with_callback<F>(start: (i32, i32), end: (i32, i32), callback: F) where F: Fn(i32, i32) -> bool rust-bindgen created this for

Node ffi pointer to struct

荒凉一梦 提交于 2019-12-24 18:59:04
问题 FIrst of all, I'm asking here because there's neither a fast answer to usage of pointers in node ffi neither about pointers to structs, this is going to help Here's my node ffi: const struct_in_addr = Struct({ 's_addr': 'ulong', }); const struct_sockaddr_in = Struct({ 'sin_family': 'short', 'sin_port' : 'ushort', 'in_addr' : struct_in_addr, 'sin_zero' : 'char', }); var redir = ffi.Library('./libredir', { //'main' : [ 'int' , [ 'int', 'char* []' ] ], //'parse_args' : [ 'void', [ 'int', 'char*

Adding lifetime constraints to non-reference types

青春壹個敷衍的年華 提交于 2019-12-24 12:41:26
问题 I am trying to figure out how to apply Rust lifetimes to add some compile-time enforcement to Erlang NIF modules. NIF modules are shared libraries normally written in C that provide extensions. A simplified prototype of the callback you would write in C looks like this: Handle my_nif_function(Heap *heap, Handle handle); You are provided a handle and a pointer to the heap that owns it. In your callback you may inspect the input handle, create more handles on the heap, and return one of them as

Getting an exception when calling Syscall function

北城以北 提交于 2019-12-24 10:00:00
问题 I am using Go's syscall package to call a DLL that is written in C++. C++ method signature looks like this. init(int* buffer, int argc, char* argv[], const char* fileName, const char* key, const char* prefix, const char* version) this is the function I am using to call above method in Go. func init( buffer uintptr, argsCount int, args []string, fileName string, key string, prefix string, version string ) uintptr { // libHandle is handle to the loaded DLL methodAddress := getProcAddress