In Rust, is there any way to use trait
s and impl
s to (recursively) flatten tuples?
If it helps, something that works with N nested pairs is
I implemented this with specialization and auto trait.
docs.rs
github repo
Usage is basically
assert_eq!((1, (2, ((3,) (4, 5)), (), 6).flatten(), (1, 2, 3, 4, 5, 6));
It removes all empty unit tuple ()
(like python).
If you are writing generic code, you need to add
where (A, B): Flatten
rustc wants this because Flatten is implemented only if length of output tuple is smaller than 13.