0

I wish to wrap a library where there is a function with the following type signature:

int fn_name(void (*cb)(*char, va_list));

I want the callback I pass to have access to some context in Rust. My idea was to create a closure with access to the context, and then convert this closure into an extern "C" raw function pointer. I don't think this is going to work.

Is it possible to do what I am trying to do, and if so how do I do it?

Note that the function uses va_list from stdarg.h, but I'm happy to treat it as a *void - handling that is out of scope of this question.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
derekdreery
  • 3,860
  • 4
  • 29
  • 38
  • Off topic, are you sure it's possible to treat `va_list` as a `void *`? My understanding is that it can be pretty much anything and doesn't even need to be a pointer. What it is on platforms supported by llvm+rust is a different matter, of course – trent Mar 04 '17 at 14:46
  • I just mean I'm not handling it. I think it's guaranteed to be pointer width for me. If it isn't then the program won't compile, so there's no chance of UB because of this. – derekdreery Mar 04 '17 at 16:38
  • @derekdreery I suggest you use the [`va_list` crate](https://github.com/thepowersgang/va_list-rs) instead of relying it to be `void*`. – kennytm Mar 04 '17 at 16:51
  • I could do this, but since in my case I just send it straight back to C (pass to vsnprintf), I can avoid an extra crate. Also, at the moment, va_list only supports platforms where `sizeof(va_list) == sizeof(void*)`. – derekdreery Mar 04 '17 at 16:54
  • Also if you are the owner of that crate, I've put in a PR for float support on x86_64 – derekdreery Mar 04 '17 at 16:56
  • 1
    @derekdreery I'm not the owner of the crate, you could file an issue though :) – kennytm Mar 04 '17 at 17:02

0 Answers0