* Add support for deriving Zeroable for fieldful enums if:
1. the enum is repr(Int), repr(C), or repr(C, Int),
2. the enum has a variant with discriminant 0,
3. and all fields of the variant with discriminant 0 are Zeroable.
* Allow using derive(Zeroable) with explicit bounds. Update documentation and doctests.
* doc update
* doc update
* remove unused
* Factor out get_zero_variant helper function.
* Use i128 to track disciminants instead of i64.
* Add doc-comment for `get_fields`
Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
* Update derive/src/traits.rs
Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
---------
Co-authored-by: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
* simplify `ToTokens` impl for `Representation`
Instead of collecting the representation and modifier into `Option`s
and determining whether a comma is needed manually, we can use the
`Puncutuated` struct which handles commas automatically.
This will also make emitting the `align` modifier in the future easier.
* emit alignment modifier
This is required for correctly implementing `CheckedBitPattern` because
we need the layout of the type and its `Bits` type to have the same
layout.
* add unit test for `#[repr]` parsing
* allow multiple alignment modifiers
According to RFC #1358, if multiple alignment modifiers are specified,
the resulting alignment is the maximum of all alignment modifiers.
* actually return the error we just created
* factor out the integer Repr's into their own type
This is a preparation step for adding support for `#[repr(C, int)]`.
* allow parsing `#[repr(C, int)]`
This can be used on enums with fields.
* derive `CheckedBitPattern` for enums with fields
The implementation mostly mirrors the desugaring described at
https://doc.rust-lang.org/reference/type-layout.html
* add comments and rename some idents
* update error message
* update docs for `CheckedBitPattern` derive
* add new nested test case, change generated type naming scheme
* fix wrong comment
* small nit
---------
Co-authored-by: Gray Olson <gray@grayolson.com>
Doctests only run in the crate proper; doctests inside of integration tests are not run.
Move the existing doctests from derive/tests/basic.rs to derive/lib.rs, expanding them to make them
better for documentation as well.
Uses the compiler to check that all non-wrapped fields are actually 1-ZSTs,
and uses Zeroable to check that all non-wrapped fields are "conjurable".
Additionally, relaxes the bound of `PhantomData<T: Zeroable>: Zeroable` to all `T: ?Sized`.
It seems like a copy-paste error has happened between `try_from_bytes`/`try_from_bytes_mut` and `try_pod_read_unaligned`, causing `internal::try_pod_read_unaligned` to try to read a &T::Bits instead of a T::Bits, usually failing with a `SizeMismatch` error. In the worst case, this allows UB in safe code by having a type allowing any bit pattern and being pointer-sized.
* allow deriving traits on unions in some cases
* basic union tests for nopadding and anybitpattern
* implement derives for unions for more traits
* remove Pod and AnyBitPattern derives for unions due to possible unsoundness
* add MaybePod and NoPadding traits
* MaybePod and NoPadding derive macros
* fix doctest
* fmt
* fix bad doc link
* move new casting functions into separate modules
* fmt
* fix doctest and derive test
* remove relaxed module, add anybitpattern
* rename MaybePod to CheckedCastFromPod
* rename checked casting functions
* rework CheckedCastFromPod into CheckedBitPattern
* add anybitpattern derive, fix up other derives
* fix doctest
* fix derive trait of bits type
* export AnyBitPattern derive
* export anybitpattern from traits
* actually export derive macro for AnyBitPattern
* make bits struct pub because of type leaking rules
* allow clippy lint in derive
* add copy bound to CheckedBitPattern
* - replace Pod bounds with NoPadding+AnyBitPattern
- add try and panic versions of checked cast functions
- slightly update docs
* fix derive tests
* - adapt the allocation module cast functions as well
- as part of that, make AnyBitPattern a subtrait of Zeroable
- AnyBitPattern derive also derives Zeroable
* @JakobDegen and @zakarumych nits
* superset -> subset on CheckedBitPattern and NoPadding docs
* derive Debug on generated `Bits` structs, which can be useful for debugging failures
* don't derive debug on spirv target arch
* make it work on 1.34
* merge conflicts
* fix erroneous behavior in doctest
* add basic derive macro for Pod and Zeroable for structs
* add derive macro for TransparentWrapper
* use core::mem::size_of instead of std::mem::size_of in generated code
* cleanup error handling a bit
* remove unneeded iter logic
* remove unneeded clone and order impl
* fix generics
* fix doc typo
Co-authored-by: Lucien Greathouse <me@lpghatguy.com>
* remove unneeded lifetime anotation
* use unreachable for already rejected patch
Co-authored-by: Lucien Greathouse <me@lpghatguy.com>