How to write bindings fot this style of API correctly?


#1

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?


#2

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.


#3

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 rust_lang@discoursemail.com 于 2017年12月9日周六 16:09写道:


#4

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.


#5

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

Daniel Collin rust_lang@discoursemail.com 于 2017年12月9日周六 20:54写道:


#6

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


#7

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 rust_lang@discoursemail.com 于 2017年12月9日周六 22:19写道:


#8

Pretty much yes.