derive_more
derive_more copied to clipboard
Support pointwise addition for arrays and tuples
trafficstars
Resolves https://github.com/JelteF/derive_more/issues/342
Synopsis
We want to support examples like these:
struct StructRecursive {
a: i32,
b: [i32; 2],
c: [[i32; 2]; 3],
d: (i32, i32),
e: ((u8, [i32; 3]), i32),
f: ((u8, i32), (u8, ((i32, u64, ((u8, u8), u16)), u8))),
g: i32,
}
struct TupleRecursive((i32, u8), [(i32, u8); 10]);
Supporting arrays and tuples inside of enums would also be useful, but that's not in this PR.
Solution
Overhaul some helper functions in add_helpers and make them recursive.
Checklist
- [ ] Documentation is updated (if required)
- [x] Tests are added/updated (if required)
- [ ] CHANGELOG entry is added (if required)
Unfortunately, as implemented this breaks for
#[derive(Add)]
pub struct GenericArrayStruct<T> {
pub a: [T; 2],
}
A workaround is:
#[derive(Add)]
pub struct GenericArrayStruct<T: Copy> {
pub a: [T; 2],
}
But a more proper fix would be useful.
The latest commit on this branch has an alternative that uses Vec and iterators, but does not require Copy. I'm not sure if that's better or worse?
Moved this to the 1.1.0 milestone as this isn't a breaking change.