Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement traits for tuples #274

Open
joshlf opened this issue Aug 17, 2023 · 2 comments · May be fixed by #1022
Open

Implement traits for tuples #274

joshlf opened this issue Aug 17, 2023 · 2 comments · May be fixed by #1022
Labels
compatibility-nonbreaking Changes that are (likely to be) non-breaking experience-medium This issue is of medium difficulty, and requires some experience help wanted Extra attention is needed

Comments

@joshlf
Copy link
Member

joshlf commented Aug 17, 2023

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:

zerocopy/src/lib.rs

Lines 1094 to 1108 in 0ff2dd1

safety_comment! {
/// SAFETY:
/// `Wrapping<T>` is guaranteed by its docs [1] to have the same layout as
/// `T`. Also, `Wrapping<T>` is `#[repr(transparent)]`, and has a single
/// field, which is `pub`. Per the reference [2], this means that the
/// `#[repr(transparent)]` attribute is "considered part of the public ABI".
///
/// [1] https://doc.rust-lang.org/nightly/core/num/struct.Wrapping.html#layout-1
/// [2] https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent
unsafe_impl!(T: FromZeroes => FromZeroes for Wrapping<T>);
unsafe_impl!(T: FromBytes => FromBytes for Wrapping<T>);
unsafe_impl!(T: AsBytes => AsBytes for Wrapping<T>);
unsafe_impl!(T: Unaligned => Unaligned for Wrapping<T>);
assert_unaligned!(Wrapping<()>, Wrapping<u8>);
}

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:

assert_impls!((): FromZeroes, FromBytes, AsBytes, Unaligned);

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

@joshlf joshlf added the compatibility-nonbreaking Changes that are (likely to be) non-breaking label Aug 17, 2023
@joshlf joshlf mentioned this issue Aug 20, 2023
@joshlf joshlf added experience-medium This issue is of medium difficulty, and requires some experience help wanted Extra attention is needed labels Aug 28, 2023
@jswrenn jswrenn changed the title Implement traits for tuple types (and maybe other container types?) Implement traits for tuples. Sep 29, 2023
@RyanWelly
Copy link

Can I have a go at this issue?

@jswrenn
Copy link
Collaborator

jswrenn commented Oct 11, 2023

Sure!

@joshlf joshlf changed the title Implement traits for tuples. Implement traits for tuples Nov 1, 2023
joshlf added a commit that referenced this issue Mar 5, 2024
@joshlf joshlf linked a pull request Mar 5, 2024 that will close this issue
joshlf added a commit that referenced this issue Mar 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compatibility-nonbreaking Changes that are (likely to be) non-breaking experience-medium This issue is of medium difficulty, and requires some experience help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants