You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As of today, Rust does not support variadic functions unless they are extern, free ones. I've been thinking about different ways to solve this, each one of them with their advantages and disadvantages, let's say we have an extern function like this:
extern"C"{fnfoo_bar(foo:&Foo,a:A, ...);}
Then we could either:
Use macros
One option would be to add a variadic_call macro to the generated bindings:
variadic_call!(foo.bar(a, v0, v1, v2))// desugars to bar(&foo, v0, v1, v2)
This isn't great as we wouldn't be able to do all the deref tricks such as handling &mut foo&foo and so on. As usual, macros would also make the code less readable and we wouldn't win anything tbh.
Use tuples
This would be way more convoluted but it would allow the users to do
implFoo{/// Call `bar` with variadic arguments using tuples.////// A call with no variadic arguments would use `()` as: `foo.bar(a, ())`.unsafefnbar(&self,a:A,varargs:implVarArgs){
varargs.call_foo_bar(self, a)}}
Then the definition and implementations of VarArgs would be
traitVarArgs{/// There should be one method in this trait for each variadic method.#[doc(hidden)]unsafefncall_foo_bar(self,foo:&Foo,a:A);}// This would be either macro generated or generated by bindgen up to a certain tuple length.implVarArgsfor(){unsafefncall_foo_bar(self,foo:&Foo,a:A){foo_bar(foo, a)}}impl<T1>VarArgsfor(T1,){unsafefncall_foo_bar(self,foo:&Foo,a:A){foo_bar(foo, a,self.0)}}impl<T1,T2>VarArgsfor(T1,T2){unsafefncall_foo_bar(self,foo:&Foo,a:A){foo_bar(foo, a,self.0,self.1)}}
The disadvantage here is having to expose this VarArgs trait but I think with some good documentation it would work nicely.
In rust you can't define variadic methods, at least as of right now.
If that changes, we would be able to generate the convenience methods we use for the rest of methods. See #402 (comment).
cc @kkimdev
The text was updated successfully, but these errors were encountered: