Calling C FFI with VaList

Hello,

I try to call a C function which take a pointer to a va_list as argument.
Does Rust support calling a C function like:

extern "stdcall" void c_fct(arguments: *mut core::ffi::VaList);

I found the core::ffi::VaList and core::ffi::VaListImpl but I really don't understand how to use it.

fn calling_c_va_li-st<T,U>(args1: T, args2: U){
      // Create VaList called va_list_args. How to do this?
     c_fct(&mut va_list_args as *mut core::ffi::VaList);
}

Is it possible today?

Thank you.

1 Like

Rust actually happens to have special support for variadic C functions:

// In Rust:
extern "stdcall" {
    fn c_fct(first_arg: FirstType, other_args: ...);
}

This is how you use variadics in rust, but it's only allowed for extern functions. The compiler deals with the VaList internally, so you (presumably) don't have to worry about it.

Example.

A function taking a valist and a variadic function are actually different things from a calling convention perspective, so just declaring the function as variadic doesn't work iirc.

1 Like

Well, if that is so, I am unaware of how to do this.

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=a2358a1453bb427e986ffcc6245bb583

VaList has the same calling convention as C's va_list.
VaListImpl is the storage behind it.

But note that this is still an unstable api and is subject to change, here's it's tracking issue.

1 Like

Great!
Thanks for your help !

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.