我有一个带有如下界面的外部库(例如 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。这就是我的结局: …