google / zerocopy

https://discord.gg/MAvWH2R6zk
Apache License 2.0
1.58k stars 104 forks source link

Implement traits for tuples #274

Open joshlf opened 1 year ago

joshlf commented 1 year ago

Although we do not do so currently, it's valid to implement FromZeroes and FromBytes for tuples of any size, so long as all of the elements of those tuples also implement those traits. Use this as a model for how to provide generic implementations of these traits: https://github.com/google/zerocopy/blob/0ff2dd15c958ab615b8094cf273f6b37d113c8fa/src/lib.rs#L1094-L1108

Note: We cannot implement Unaligned nor AsBytes for any tuple type other than () — all other tuple types have the default representation, and so a) there is no upper bound on their alignment and, b) there are no guarantees made about the absence of padding.

Additionally, add tests for the presence/absence of these impls for various concrete tuple types (where the tuple element types are both sufficient and insufficient for generating the impl for the entire tuple). The assert_impls! macro may be helpful for this: https://github.com/google/zerocopy/blob/b083f1f12839b91a54460ab5d2518105f3fc5355/src/lib.rs#L4512

Following the lead of the standard library, we should implement these traits for tuples up to size 12.

RyanWelly commented 1 year ago

Can I have a go at this issue?

jswrenn commented 1 year ago

Sure!