Skip to content

Support silent failure in derives #1769

@joshlf

Description

@joshlf

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions