derive_more icon indicating copy to clipboard operation
derive_more copied to clipboard

Support pointwise addition for arrays and tuples

Open matthiasgoergens opened this issue 1 year ago • 3 comments
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)

matthiasgoergens avatar Mar 20 '24 09:03 matthiasgoergens

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.

matthiasgoergens avatar Mar 20 '24 14:03 matthiasgoergens

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?

matthiasgoergens avatar Mar 20 '24 15:03 matthiasgoergens

Moved this to the 1.1.0 milestone as this isn't a breaking change.

JelteF avatar Jul 01 '24 10:07 JelteF