How to write bindings fot this style of API correctly?


I’m working on bindings for VapourSynth, which is a video processing framework.
The C API it exposes is inside a giant struct. Currently I’m putting a *const VSAPI in every struct that uses the API, is there any way to avoid that?


One way would be to have one Rust struct like (VsApi) that has pointer to the VSAPI pointer. VsApi should then have wrapper functions (to provide a safe interface over the C API) so the rest of the code can use VsApi only and not needing to directly call the C code.


Actually I don’t mind having unsafe code everywhere, I’m just thinking if
there is a way to avoid storing a pointer to that so many times.

Daniel Collin 于 2017年12月9日周六 16:09写道:


I guess it depends on how you write the code. You would have the same problem in C. What you can do (in both Rust and C) is to pass down the struct pointer to the functions that needs it (as a input parameter) instead of storing it.


So simply require the user to pass the API pointer to whichever function
need it?

Daniel Collin 于 2017年12月9日周六 20:54写道:


Yeah pretty much. You could have a global variable as well but that has it’s sets of issues as well.


As the pointer passed in points to something like const VSAPI vs_internal_vsapi = {…} , is it OK to simply implement Copy on VsApi and
let it go?

Daniel Collin 于 2017年12月9日周六 22:19写道:


Pretty much yes.