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

Relax requirements for deriving FromZeroes on enums #30

Closed
joshlf opened this issue Oct 7, 2022 · 3 comments · Fixed by #143 or #145
Closed

Relax requirements for deriving FromZeroes on enums #30

joshlf opened this issue Oct 7, 2022 · 3 comments · Fixed by #143 or #145
Assignees

Comments

@joshlf
Copy link
Member

joshlf commented Oct 7, 2022

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 { ... }
joshlf added a commit that referenced this issue Oct 30, 2022
joshlf added a commit that referenced this issue Nov 2, 2022
joshlf added a commit that referenced this issue Nov 2, 2022
joshlf added a commit that referenced this issue Nov 2, 2022
joshlf added a commit that referenced this issue Nov 6, 2022
joshlf added a commit that referenced this issue Nov 6, 2022
joshlf added a commit that referenced this issue Nov 7, 2022
joshlf added a commit that referenced this issue Nov 7, 2022
joshlf added a commit that referenced this issue Nov 8, 2022
joshlf added a commit that referenced this issue Nov 9, 2022
joshlf added a commit that referenced this issue Nov 9, 2022
joshlf added a commit that referenced this issue Nov 9, 2022
joshlf added a commit that referenced this issue Nov 10, 2022
joshlf added a commit that referenced this issue Nov 10, 2022
joshlf added a commit that referenced this issue Nov 10, 2022
joshlf added a commit that referenced this issue Nov 10, 2022
Closes #30

TODO:
  - Support deriving on enums
  - Doc comment on `FromZeroes`
  - trybuild tests for zerocopy-derive
joshlf added a commit that referenced this issue Nov 10, 2022
Closes #30

TODO:
  - Support deriving on enums
  - Doc comment on `FromZeroes`
  - trybuild tests for zerocopy-derive
joshlf added a commit that referenced this issue Nov 10, 2022
Closes #30

TODO:
  - Support deriving on enums
  - Doc comment on `FromZeroes`
  - trybuild tests for zerocopy-derive
joshlf added a commit that referenced this issue Nov 11, 2022
Closes #30

TODO: Doc comment on `FromZeroes`
joshlf added a commit that referenced this issue Nov 11, 2022
Closes #30

TODO: Doc comment on `FromZeroes`
joshlf added a commit that referenced this issue Nov 11, 2022
joshlf added a commit that referenced this issue Nov 11, 2022
@joshlf joshlf linked a pull request Nov 11, 2022 that will close this issue
joshlf added a commit that referenced this issue Nov 11, 2022
joshlf added a commit that referenced this issue Nov 11, 2022
joshlf added a commit that referenced this issue Nov 11, 2022
joshlf added a commit that referenced this issue Nov 12, 2022
joshlf added a commit that referenced this issue Nov 12, 2022
joshlf added a commit that referenced this issue Nov 12, 2022
@joshlf
Copy link
Member Author

joshlf commented Nov 15, 2022

Reopening so that this issue can track deriving FromZeroes on enums; see the issue comment for more details.

@joshlf joshlf reopened this Nov 15, 2022
@joshlf joshlf changed the title Add FromZeroes trait Relax requirements for deriving FromZeroes on enums Nov 15, 2022
@joshlf
Copy link
Member Author

joshlf commented Nov 15, 2022

@djkoloski if you'd like, I can assign this to you (you just need to comment on this issue first before GitHub will let me do that).

@djkoloski
Copy link
Member

Sure!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants