dtolnay / dtolnay/seq-macro

Allow iterating backwards

Open
#20 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
167
Forks
9
PR merge metrics
No merged PRs in 30d

Description

It would be very convenient if this library supported backwards iteration of the range. Here's my use case: I have a trait called `Undo` that represents an action that can be undone. I would like to implement this trait for tuples, to allow a sequence of actions to be undone. The problem is that the tuples ought to be undone in reverse, starting from the end of the tuple.

I'd love to write something like

```rust
seq!(N in 2..=4 {
#(
seq!(I in 0..N {
impl Undo for (#( U~I, )*)
where
#(U~I: Undo,)*
{
type Target = T;

fn revert(self, target: &mut Self::Target) {
seq!(J in (0..N).rev() {
self.J.revert(target);
});
}
}
});
)*
});
```

Or alternately, if there were a way I could handle the reverse indexing myself like

```rust
seq!(N in 2..=4 {
#(
seq!(I in 0..N {
impl Undo for (#( U~I, )*)
where
#( U~I: Undo, )*
{
type Target = T;

fn revert(self, target: &mut Self::Target) {
#( self.#(N - I).revert(target); )*
})
}
});
)*
});
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.