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 validation-only mode in derives #1673

Open
joshlf opened this issue Sep 17, 2024 · 0 comments
Open

Support validation-only mode in derives #1673

joshlf opened this issue Sep 17, 2024 · 0 comments

Comments

@joshlf
Copy link
Member

joshlf commented Sep 17, 2024

Some users want to know whether one of their public types satisfies the layout properties required by FromBytes, IntoBytes, etc, but do not want to expose a trait impl to their users. Currently, the best way to do this is pretty ugly:

#[derive(FromBytes)]
struct FooInner { ... }

#[repr(transparent)]
pub struct Foo(FooInner);

// Somewhere in `unsafe` code...

// SAFETY: Since `Foo` is a `#[repr(transparent)]` wrapper around `FooInner`, and 
// since `FooInner: FromBytes`, we know that...

It would be great if we could instead support a validate-only mode for our derives. This would look something like:

#[derive(FromBytes)]
#[zerocopy(validate-only)]
pub struct Foo { ... }

// Somewhere in `unsafe` code...

// SAFETY: Since `Foo` is validated to be `FromBytes`...

Note that this would only be sound for non-generic types. For generic types, our derives don't say in the general case whether a type satisfies the trait's requirements, but instead generate an impl with appropriate bounds. Thus, just because a derive on a generic type compiles successfully doesn't mean that all instantiations of that type satisfy that trait's requirements.

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

No branches or pull requests

1 participant