Cyclic version of slice::windows()?

I'm familiar with iter::cycle() and slice::windows(), but AFAIK you can't take windows of a cycle. Before I go off and write something myself, is there a way?

For example, let's say I have need three buffers and want to repeatedly do something with them, the first time using buf1, buf2, buf3, the second time using buf2, buf3, buf1, the third time buf3, buf1, buf2, and so on. Is something like this out there already?

Check itertools::Itertools - Rust

slice::windows is very specific in that it exposes contiguous blocks of memory, not just abstract sequences, so it can't "wrap around" or create a combination that isn't already present in memory.

It shouldn't be too hard to create a custom iterator that cycles through 3 items.

2 Likes

Here's one way to do it (though a dedicated iterator struct or an impl-returning function would make for better clarity).

1 Like

The itertools crate has a circular_tuple_windows method.

4 Likes

Thanks, everyone! I was googling for "cycles", "cyclic", "ring", and the like, but not "circular". Itertools to the rescue again.