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

Initial commit of DstLayout::for_dst #541

Closed
wants to merge 1 commit into from
Closed

Conversation

joshlf
Copy link
Member

@joshlf joshlf commented Oct 24, 2023

This will be used by the custom derive of KnownLayout to compute the DstLayout for unsized types.

Makes progress on #29

@jswrenn jswrenn force-pushed the dst-layout-from-dst branch 2 times, most recently from 824e41f to 85ed994 Compare October 31, 2023 16:45
@jswrenn jswrenn force-pushed the dst-layout-from-dst branch 3 times, most recently from d0fcb15 to 21cea7e Compare November 2, 2023 17:20
zerocopy-derive/src/lib.rs Outdated Show resolved Hide resolved
src/macro_util.rs Outdated Show resolved Hide resolved
src/byteorder.rs Outdated Show resolved Hide resolved
@jswrenn jswrenn force-pushed the dst-layout-from-dst branch 4 times, most recently from 5927d5b to 8956931 Compare November 8, 2023 19:31
Comment on lines +483 to +485
// SAFETY: TODO
SizeInfo::Sized { _size }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not convinced this is sound for non-repr(C) types. Rust reserves the right to add excess padding.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah very good point. Maybe rename to for_repr_c_dst or something? And add a safety precondition?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should the UX of the derive be? Some possibilities:

  • unconditionally require repr(C) on structs.
  • don't unconditionally require repr(C), but expand to DstLayout::for_type::<Self>() on non-repr(C) types, and DstLayout::for_repr_c_dst on repr(C) types.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally I'd prefer the latter, but how can we make the UX of the error message good when the user tries to derive on an unsized, non-repr(C) type?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, but not sure yet how good the UX can be. This still needs UI tests.

@jswrenn jswrenn force-pushed the dst-layout-from-dst branch 3 times, most recently from 05fd194 to da522c6 Compare November 8, 2023 19:44
src/lib.rs Outdated
@@ -3908,6 +3965,80 @@ mod tests {
}
}

mod for_dst {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the thinking behind having these in a nested module as opposed to just naming them test_dst_layout_for_sized etc?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • reduces repetition in the names
  • makes it possible to code-collapse the entire group in an editor

My preference (not in this PR) would be even to go a little further and drop test_ from all tests. All of the tests are already located in a module called "tests".

src/lib.rs Show resolved Hide resolved
panic!(concat!(
"cannot compute layout of `",
stringify!($ty),
"`; type is larger than zerocopy supports"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving a TODO so we don't forget: We should have an issue link here so there's a way for users to report that they need support for larger types.

zerocopy-derive/src/lib.rs Outdated Show resolved Hide resolved
@jswrenn jswrenn force-pushed the dst-layout-from-dst branch 5 times, most recently from ef0f250 to 4a2c8f0 Compare November 14, 2023 19:18
This will be used by the custom derive of `KnownLayout` to compute the
`DstLayout` for unsized types.

Makes progress on #29

Co-authored-by: Jack Wrenn <jswrenn@amazon.com>
@joshlf
Copy link
Member Author

joshlf commented Nov 27, 2023

Superseded by #633 and #638.

@joshlf joshlf closed this Nov 27, 2023
@joshlf joshlf deleted the dst-layout-from-dst branch November 27, 2023 18:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants