Rust function with va_list?

My use case that I use C library, which API allows replacing default function with my own function:

typedef void (*LogCallback)(Domain, Level, const char *fmt, va_list args);
void registerCallback(LogCallback callback, bool preformatted);

I obviosly want to replace to use log crate and unify logging inside my program.

Obviosly the problem in va_list type.
As I see there is issue about this, but it is not part of stable yet, is it true?
But there is https://docs.rs/va_list/0.1.3/va_list/ , any idea how it works without rust compiler support?

Also there is preformatted argument in registerCallback, with it fmt will contain full string,
and va_list will be NULL. But I can not see it helps me or not.
Is it UB to define va_arg in some compatitable way and just ignore it?

That's correct. Rust doesn't support variadic C functions yet.

The va_list crate seems to support it "the hard way" by implementing the variadic function ABI for a few platforms.

1 Like

the most portable way would be to write a C stub function that converts the va_list to an array in a format that the Rust FFI understands and calls the rust function

Is it UB to define va_arg in some compatitable way and just ignore it?

ignoring a NULL pointer (as void* for example) would be perfectly safe, however I'm not sure that va_list is typedef'ed to a pointer on every platform, it might be a special structure that requires cleanup even if it contains no arguments.

1 Like