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

Support generic parameters when deriving AsBytes on a #[repr(transparent)] type #9

Closed
joshlf opened this issue Sep 19, 2022 · 0 comments · Fixed by #120
Closed

Support generic parameters when deriving AsBytes on a #[repr(transparent)] type #9

joshlf opened this issue Sep 19, 2022 · 0 comments · Fixed by #120

Comments

@joshlf
Copy link
Member

joshlf commented Sep 19, 2022

Migrated from https://fxbug.dev/84475

Originally reported by @Kestrer

Hi, I'm not quite sure how to open an issue with Zerocopy directly so I hope I put this in the right place.

I'm the maintainer of the bounded-integer crate (https://docs.rs/bounded-integer) and I would like to support implementing the Zerocopy traits on the bounded integers. My code looks something like this:

type Inner = u8;

#[derive(Unaligned, AsBytes)]
#[repr(transparent)]
pub struct Bounded<const MIN: Inner, const MAX: Inner>(Inner);

However zerocopy complains because it doesn't support generic parameters.

Comment by @cmyr

I've also just run into this, and I'd be happy to prepare a patch if that would be welcome (and if I can remember how gerrit works).

I've taken a quick skim through the current impl, and stared into space for a few minutes, and it isn't obvious to me why this constraint exists, currently. I trust there is a reason (given that the limitation does not exist for FromBytes) but it isn't obvious to me what it is, so any useful information here would be welcome. :)

Comment by @joshlf

Hi folks, sorry for the delay in responding to this.

Generic parameters are supported for Unaligned - try deriving that trait on its own without AsBytes.

The reason that AsBytes doesn't support generic parameters is that I haven't figured out a way to ensure that the resulting type has no padding, which is a requirement for AsBytes. In particular, consider the following type:

#[repr(C)]
struct Foo<T, U> {
    t: T,
    u: U,
}

In order for Foo<T, U> to be AsBytes, there can't be any padding. That, however, depends on both the sizes and alignments of T and U. I haven't figured out how to write an impl bound that expresses the right requirements.

Take a look at the implementation of the AsBytes derive for how we do this when there are no generic parameters - we use const code to compare the size of the type to the sum of the sizes of its fields. That approach doesn't translate since const generics aren't powerful enough yet to just use the same code in an impl bound. We'd need another approach.

Comment by @joshlf

Update: Some of the other developers realized that we can probably support your specific use case more easily since your type is #[repr(transparent)], and thus we don't need to worry about any layout computation - we just need to bound Inner: AsBytes. I'm changing this issue to track that more narrow goal.

joshlf pushed a commit that referenced this issue Nov 6, 2022
Also consolidate some utility types and macros.

Closes #9
joshlf pushed a commit that referenced this issue Aug 3, 2023
Also consolidate some utility types and macros.

Closes #9
@joshlf joshlf mentioned this issue Aug 20, 2023
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 a pull request may close this issue.

1 participant