How to use VaList

Working on a Tensorflow Lite shard as there is a big library of TF Lite neural nets out there which are super useful. I have the shard working and am working on the error callback.

I need to handle this callback:

fun error_reporter = TfLiteInterpreterOptionsSetErrorReporter(
  options : InterpreterOptions,
  reporter : (Void*, LibC::Char*, VaList -> Void),
  user_data : Void*

So far my implementation looks like this:

but not sure how I should be parsing the VaList out into something useful.
Anyone with C-interop skills able to assist?

It’s basically the argument list for IO.printf and it would be good to actually format the string for some decent error messages.

You can use vasprintf to print to allocated String. Crystal doesn’t have wrapper for that, but it will be quite straight-forward to have them included. So something like below should work for your use case.

lib C
   fun vasprintf(strp : LibC::Char**, format : LibC::Char*, ap : Void*) : LibC::Int
C.vasprintf(out msg, raw_message, raw_args)
formatted_msg =

Please bear in mind that vasprintf allocates the memory and returns number of bytes printed or -1 on memory allocation error.

So you should be responsible for freeing the memory, when you are done with that.


nice! I think will allocate new memory when passed a LibC::Char* so can probably free the bytes straight away.

Edit: the solution for anyone interested in the future

1 Like