Skip to content

Relax requirements for deriving FromZeroes on enums #30

@joshlf

Description

@joshlf

This issue used to track FromZeroes, which has now been merged. However, support for #[derive(FromZeroes)] is incomplete - the rules implemented when deriving on an enum are equivalent to the rules for deriving FromBytes on an enum. Those rules are unnecessarily restrictive.

Now, this issue tracks:

  • Figuring out what rules enums need to follow in order to be FromZeroes
  • Modifying our derive to implement those rules

Old text

Add an unsafe marker trait called FromZeroes (or similar) which indicates that a type can be safely constructed from all 0 bytes. Add a custom derive to zerocopy-derive for this trait.

I would expect the API to look something like this:

unsafe trait FromZeroes {
    #[doc(hidden)]
    fn only_derive_is_allowed_to_implement_this_trait() where Self: Sized;

    fn zero(&mut self);

    // These would all be moved from `FromBytes`.

    fn new_zeroed() -> Self where Self: Sized { ... }
    #[cfg(feature = "alloc")]
    fn new_box_zeroed() -> Box<Self> where Self: Sized { ... }
    #[cfg(feature = "alloc")]
    fn new_box_slice_zeroed() -> Box<Self> where Self: Sized { ... }

    // If we also implement `Zeroed` (#31):
    fn read_from_zeroed<B: ByteSlice>(bytes: Zeroed<B>) -> Option<Self> where Self: Sized { ... }
    fn read_from_zeroed_prefix<B: ByteSlice>(bytes: Zeroed<B>) -> Option<Self> where Self: Sized { ... }
    fn read_from_zeroed_suffix<B: ByteSlice>(bytes: Zeroed<B>) -> Option<Self> where Self: Sized { ... }
}

// `FromBytes` would gain a `FromZeroes` bound.
unsafe trait FromBytes: FromZeroes { ... }

Metadata

Metadata

Assignees

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