# Pre-RFC: #\[repr(rust)\] variadic functions

**URL:** <https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920>\
**Category:** language design\
**Created:** [March 2, 2020, 2:51pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920 "2020-03-02T14:51:31Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [March 3, 2020, 3:16pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/21 "2020-03-03T15:16:53Z")

</div>

I was referring to Iterators in general. It's likely that not all cases will involve IntoIter for arrays. For example, due to the current lack of genericity I tend to shun arrays in favor of `Vec` in extant code, even though that brings with it a heap allocation penalty. Perhaps that will someday be solved, but today is not that day, nor wil it soon come.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [March 3, 2020, 4:49pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/22 "2020-03-03T16:49:03Z")

</div>

I don't see what using `Vec<T>` has to do with variadic arguments. Iterators are based on top of what every data-structure they iterate over. It doesn't seem to me that the fact that the data happens to be on the heap is a concern.

---

<div class="post-metadata">

**Author:** ![jjpe](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jjpe/32/9779_2.png) [@jjpe](https://internals.rust-lang.org/u/jjpe)\
**Post date:** [March 3, 2020, 5:00pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/23 "2020-03-03T17:00:22Z")

</div>

Vec is an example. The point is that you have no control over, and don't even know what implements IntoIter in general, and how it does so. **All** you know is it adheres to the trait contract.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [March 3, 2020, 5:16pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/24 "2020-03-03T17:16:08Z")

</div>

Yes that is the point. To make the feature as general as possible. However, the alternative of allocating a known size array or Vec and then copying over all the elements is substantially more work in general.

Sebastian Malton

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [March 3, 2020, 5:18pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/25 "2020-03-03T17:18:30Z")

</div>

> [@storyfeet](#):
>
> I realize creating a macro that works over the second one would have a fair few challenges.

Well, solved ones:

> [@CAD97](#):
>
> [Here's a draft of the two macros for sugaring calls to a function taking `impl [Into]Iterator` ](https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=8908f2a33481fdd4b27d29a5531c7b2b):
> 
> ```rust
> macro_rules! ichain {
> ($front:expr $(,)?) => { IntoIterator::into_iter($front) };
> ($front:expr $(, $($rest:expr),* $(,)?)?) => {
> itertools::chain($front, ichain!($($($rest),*)?))
> };
> }
> 
> macro_rules! isplat {
> (...$front:expr $(,)?) => { IntoIterator::into_iter($front) };
> (...$front:expr, $($rest:tt)*) => {
> itertools::chain($front, isplat!($($rest)*))
> };
> ($front:expr $(,)?) => { IntoIterator::into_iter($front) };
> ($front:expr, $($rest:tt)*) => {
> itertools::chain(Some($front), isplat!($($rest)*))
> }
> }
> 
> ```

> [@jjpe](#):
>
> While de sugaring to Iterators makes sense in principle, using iterators often enough involves heap allocation, and it would be a real waste to have to allocate just to use variadic arguments.

> [@jjpe](#):
>
> For example, due to the current lack of genericity I tend to shun arrays in favor of `Vec` in extant code

This is why you're equating Iterator\<=\>allocation. The easiest (and it will be for a while) way to get an [into]iterator over `T` is a `Vec<T>`, because `[T; N]` isn't `IntoIterator` yet, even for `N <= 32`. There's work on future-compat lints to make adding this impl break less (poorly written) code that coerces to using `&[T] as IntoIterator` (is it in yet? I don't know).

If you take `Vec<T>` for your variadic because it's easy, it will require allocation. If you take `impl IntoIterator<Item=T>`, then you can take either the easy `Vec<T>` or the allocation-free combination of `iter::Chain` and `iter::Once` that I've provided macro sugar for above in `isplat!`.

And don't underestimate how clever the optimizer can be. If the function is generic and thus knows the exact size/contents of the iterator at compile time, it might even be able to optimize the function to passing them as individual arguments in the calling convention.

> [@jjpe](#):
>
> Vec is an example. The point is that you have no control over, and don't even know what implements IntoIter in general, and how it does so. **All** you know is it adheres to the trait contract.

And that's a bad thing how? There's no real reason to _prevent_ the use of a `Vec` for a variadic. Let the caller decide which is better for their case, the static `isplat` or the dynamic `Vec`. For huge arrays or cases optimizing for code size with calls of your function with many different lengths, it may be the the best option.

It's the same principle as `(&dyn Trait): Trait`: let the caller decide their tradeoffs. You write the generic version, but the caller is allowed to chose virtual dispatch (for variadics, a dynamic size collection such as `Vec`), because in some cases that's better than copy/pasting the generic function for every use.

To be completely fair: that is the main downside of using `impl IntoIterator` over `Vec` or even `[_; N]`: generic code duplication. In the `[_; N]` case every same-length call uses the same overload. In the `Vec` case, _every_ call uses the same overload. With `impl IntoIterator`, it's much more easy to have every call create a new unique overload, with the code duplication that comes along with it. The ones purely `iter::chain`ing together `iter::once`s will all be the same overload, but the moment you introduce a different iterator into the chain, it's likely a unique type and thus overload. And this one can't be handled via trait objects, because `IntoIterator` isn't object safe (yet? alloca?).

So maybe you could use `impl Iterator` instead of `impl IntoIterator`; after all, `ichain!`/`isplat!` return iterators directly, not just iterables. This would even help discourage the simple case of just passing a `Vec` or other collection, as it'd have to be `vec![...].into_iter()`, which would hopefully hint at the alternative allocation-free options.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [March 3, 2020, 5:44pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/26 "2020-03-03T17:44:09Z")

</div>

That is a very well written post, thank you.

One point though, why have both `ichain!` and `isplat!` since `isplat!` covers the cases where `ichain!` works.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [March 3, 2020, 6:58pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/27 "2020-03-03T18:58:17Z")

</div>

The main reason I wrote both is that `ichain` is something that might actually be upstreamed to itertools. `ichain` follows the existing pattern with `izip`; `isplat` is more unique/specialized, and would be a harder sell to include in itertools.

---

<div class="post-metadata">

**Author:** ![naim](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/naim/32/3522_2.png) [@naim](https://internals.rust-lang.org/u/naim)\
**Post date:** [March 3, 2020, 10:23pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/28 "2020-03-03T22:23:22Z")

</div>

What about interoperability with [`c_variadic`](https://doc.rust-lang.org/unstable-book/language-features/c-variadic.html)?

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [March 4, 2020, 1:47am UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/29 "2020-03-04T01:47:45Z")

</div>

I think that there need only be a macro (probably built-in) that converts from any iterator to whatever representation is needed for c variadics.

They are unsafe anyway.

---

<div class="post-metadata">

**Author:** ![kennytm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kennytm/32/161_2.png) [@kennytm](https://internals.rust-lang.org/u/kennytm)\
**Post date:** [March 4, 2020, 11:47am UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/30 "2020-03-04T11:47:34Z")

</div>

`(args: ...)` and `(args: ...T)` are two different things.

---

<div class="post-metadata">

**Author:** ![crlf0710](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/crlf0710/32/1987_2.png) [@crlf0710](https://internals.rust-lang.org/u/crlf0710)\
**Post date:** [March 4, 2020, 1:01pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/31 "2020-03-04T13:01:01Z")

</div>

I always feel there's a need for some "tuple\_slice" thing, which is an abstraction over ordered heterogeneous sequence. Not sure how to make it zero cost though.

---

<div class="post-metadata">

**Author:** ![PoignardAzur](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/poignardazur/32/6464_2.png) [@PoignardAzur](https://internals.rust-lang.org/u/PoignardAzur)\
**Post date:** [March 4, 2020, 3:02pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/32 "2020-03-04T15:02:20Z")

</div>

Variadic generics.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [March 5, 2020, 11:42pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/33 "2020-03-05T23:42:48Z")

</div>

So the only real concern that I see for a feature like this is the lack of the ability to use non-boxed trait objects. However, since they are not supported at all I don't think that this is/should be a blocker for such a feature.

There is also no need for a "spread" operator, like in `golang`, since the way that it would be implemented seems to be with iterators and `IntoIterator<T>`.

It would be nice if there was interoperability between this feature and `c_variadic`'s, but I need to do some more investigation as to how this would work. If my understanding of the spec for C style variadic functions the type `T` would have to be `Sized` and the `va_arg`, `va_start`, `va_end`, and `va_copy` seem to be a sort of iterator, which, hopefully, makes it "easy" to put on top of rust's iterator.

---

<div class="post-metadata">

**Author:** ![PoignardAzur](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/poignardazur/32/6464_2.png) [@PoignardAzur](https://internals.rust-lang.org/u/PoignardAzur)\
**Post date:** [March 6, 2020, 9:42am UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/34 "2020-03-06T09:42:46Z")

</div>

> [@Nokel81](#):
>
> It would be nice if there was interoperability between this feature and `c_variadic` 's,

I don't think there would much to gain from that. C variadics are kind of awful, and extremely type-unsafe, even more than regular C.

If you want to call eg C's printf from Rust, you'd need a fairly heavy Rust wrapper to enforce type-safety, at which point you might as well use a native implementation.

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [March 6, 2020, 10:17am UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/35 "2020-03-06T10:17:45Z")

</div>

More fundamentally, the c\_variadic feature in Rust is an FFI feature. Unless your project includes some C code declaring or calling a C variadic function, IIUC there's no benefit to using c\_variadic in your Rust code that you can't achieve far more easily and safely with standard Rust containers, slices, trait objects and/or variadic macros. So even if stronger interop was actually possible, I don't see how we'd get any significant value out of it.

---

<div class="post-metadata">

**Author:** ![Nokel81](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/nokel81/32/3966_2.png) [@Nokel81](https://internals.rust-lang.org/u/Nokel81)\
**Post date:** [March 12, 2020, 7:57pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/36 "2020-03-12T19:57:51Z")

</div>

I have written up the beginnings of an RFC, here is the rendered link: [https://github.com/Nokel81/rfcs/blob/be1af954c7c013f4e6735cc2d20e360dd70f57db/text/0000-rust-variadics.md](https://github.com/Nokel81/rfcs/blob/be1af954c7c013f4e6735cc2d20e360dd70f57db/text/0000-rust-variadics.md).

It would be great if it could be read it over before I put it up against the rfc repo.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [March 12, 2020, 10:22pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/37 "2020-03-12T22:22:42Z")

</div>

State of the art is to open a self-PR (i.e. from your/branch to your/master) so people can leave comments in the thread there. I've got a few minor notes I'd leave there.

But the TL;DR of my notes is that you need to show a meaningful reason this would be better than just taking `impl [Into]Iterator`.

And one other big note: what about a `T` that implements `IntoIterator<T>`?

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [March 12, 2020, 11:30pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/38 "2020-03-12T23:30:46Z")

</div>

I agree with CAD97 here. A language change for the homogeneous case doesn't seem worth it when people are already working on making `[T; N]: IntoIterator<Item=T>`, at which point just calling `foobar(MyType::new(), [1, 2, 3])` seems plenty good enough.

I do think a language change will be worth it at some point to allow _heterogeneous_ variadic functions -- which includes the homogenious case but will also open up a bunch of interesting possibilities for `Fn` and tuples and friends, like const generics are doing for arrays.

---

<div class="post-metadata">

**Author:** ![Aloso](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/aloso/32/5039_2.png) [@Aloso](https://internals.rust-lang.org/u/Aloso)\
**Post date:** [March 13, 2020, 9:45pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/39 "2020-03-13T21:45:15Z")

</div>

I like the whole idea!

Note that `std::iter::once` and `Iterator::chain` have some **memory overhead** , that can't be optimized away unless the function is inlined (see this [godbolt link](https://rust.godbolt.org/z/J4237w)).

`Once<T>` is internally represented as `Option<T>`, and `Chain<A, B>` is defined as

```rust
pub struct Chain<A, B> {
    a: A,
    b: B,
    state: ChainState,
}

```

If `sum_all(0, 1, 2, 3, 4)` desugars to

```rust
sum_all(0, once(1).chain(once(2)).chain(once(3)).chain(once(4)))

```

then the concrete type of the iterator is

```rust
Chain<Chain<Chain<Once<i32>, Once<i32>>, Once<i32>>, Once<i32>>

```

So it might be better to use something like `ArrayIter<[i32; 4]>` instead to minimise memory overhead. Such an iterator doesn't exist yet, but I believe there are plans to implement `IntoIterator` for `[T; N]`.

So `sum_all(0, 1, 2, rest...)` could desugar to

```rust
sum_all(0, [1, 2].into_iter().chain(rest))

```

## Syntax

I'd prefer a slightly more explicit syntax:

```rust
fn sum_all(start: usize, items: ...impl Iterator<Item = usize>) -> usize;

```

This has several benetfits:

- The type of `items` is visible, so less "magic" is happening
- You can add lifetimes or trait bounds, e.g.

```rust
items: ...impl Iterator<Item = Foo> + 'a

```

- You can use a generic type and specify bounds in a **where** clause, giving you more flexibility
- We can later add support for other types of variadics, some examples that come to mind:
  - `impl DoubleEndedIterator<Item = Foo>`
  - `impl Stream<Item = Foo>`
  - `Box<dyn Iterator<Item = Foo>>`
  - `Vec<Foo>`

---

<div class="post-metadata">

**Author:** ![RustyYato](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rustyyato/32/13627_2.png) [@RustyYato](https://internals.rust-lang.org/u/RustyYato)\
**Post date:** [March 13, 2020, 10:51pm UTC](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920/40 "2020-03-13T22:51:20Z")

</div>

We could just do this now

```rust
fn sum_all(start: usize, items: impl IntoIterator<Item = usize>) -> usize;

```

and call it like

```rust
sum_all(0, [1, 2]); // assuming arrays implement `IntoIterator` sometime in the future

// in the meantime, we can use something like `arrayvec` or once chains

```

So there is no real need for homogeneous variadic functions

[Previous page](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920.md?page=1)

[Next page](https://internals.rust-lang.org/t/pre-rfc-repr-rust-variadic-functions/11920.md?page=3)
