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 silent failure in derives #1769

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

Support silent failure in derives #1769

joshlf opened this issue Sep 27, 2024 · 0 comments

Comments

@joshlf
Copy link
Member

joshlf commented Sep 27, 2024

Some users want to either auto-generate #[derive(...)] attributes (e.g. for use with bindgen) or use a single macro to generate all types uniformly (e.g. libc (#557)). Having to manually maintain different derives for different types (e.g. when some types are IntoBytes but others aren't) is very laborious.

Instead, we could support a mode in which a failure simply causes zerocopy-derive to not emit a trait impl, but does not cause compilation failure:

#[derive(FromBytes, IntoBytes)]
#[zerocopy(derive_fail_silently)]
#[repr(transparent)]
struct Foo(bool);

In this example, IntoBytes, TryFromBytes, and FromZeros impls would be emitted, but a FromBytes impl would not.

For errors emitted by our derive code, this is doable and straightforward. For errors emitted when trying to compile code emitted by our derive code, we'll require the trivial_bounds feature to be stabilized.


As described in #712 (comment), users may also want this in "normal" code (rather than macro- or bindgen-generated code), in which case they may want to assure that certain traits are implemented. It would be bad UX if they simply discovered later that a particular bound wasn't satisfied. Perhaps we could support an attribute that specifies which traits should be treated like normal, and which should be treated silently:

#[derive(FromBytes, IntoBytes)]
#[zerocopy(derive_fail_silently)]
#[zerocopy(derive_fail_loudly(TryFromBytes, FromZeros, IntoBytes))]
#[repr(transparent)]
struct Foo(bool);
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