小编Vas*_*hov的帖子

为外部 C 库分配不透明缓冲区的正确 Rust 方法是什么?

我有一个带有如下界面的外部库(例如 libcisland.so):

size_t lib_handle_size();
typedef void* handle;
int lib_init(handle h);
int lib_store(handle h, int value);
int lib_restore(handle h, int *pvalue);
Run Code Online (Sandbox Code Playgroud)

该库的用户应执行以下操作:

// allocate some buffer in client address space
handle h = malloc(lib_handle_size());
// pass this buffer to library for initialization
if (lib_init(h)) { /* handle errors */ }
// library initializes this handle by some opaque fashion
// then user uses it
lib_store(h,42);
int r;
lib_restore(h,&r);
// after all work is done, user frees this handle
free(h);
Run Code Online (Sandbox Code Playgroud)

我无法弄清楚如何将此接口正确包装到 Rust。这就是我的结局: …

c ffi rust

5
推荐指数
1
解决办法
725
查看次数

标签 统计

c ×1

ffi ×1

rust ×1