Commit Graph

18 Commits

Author SHA1 Message Date
Christopher Serr
b97dee7f90
Switch to syn 2.0 (#183)
This upgrades the derives from `syn` 1.0 to `syn` 2.0. While the MSRV
for `syn` 2.0 is not Rust 1.56 which is higher than the MSRV of
`bytemuck`, the derives don't fall under the MSRV policy.
2023-03-21 13:52:18 -06:00
zachs18
1039388f0b
Fix soundness issue of TransparentWrapper derive macro. (#173)
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`.
2023-02-17 12:24:16 -07:00
Tom Dohrmann
2d1f876216
various small fixes to the derive macros (#168)
* parse `repr(usize)` and `repr(isize)`

This can be used with enums.

* emit packed repr without type suffix

Previously we emitted the packed attribute with a type suffix for the
value `packed(4u32)`. This is not allowed. Instead we should emit
`packed(4)`.

* read bits instead of creating reference

This is required for packed structs where creating a reference to
fields is not allowed. We can make a copy of the bits because the bits
are `AnyBitPattern` which implies `Copy`.

* emit generics on implied traits
2023-01-25 21:49:46 -07:00
zachs18
dbb776d443
Allow byte literals as enum discriminants in CheckedBitPattern derive macro. (#155) 2022-12-29 17:23:14 -07:00
zachs18
c9e1ae1373
Remove where clause from derived TransparentWrapper impls. (#146)
* Remove where clause from derived TransparentWrapper impls.

* Add test for TransparentWrapper trait bound regression.
2022-11-20 16:40:20 -07:00
John Nunley
518baf9c0b
Allow repr(transparent) to be used generically in derive(Pod) (#139)
* Enabled transparent generics

* Move trait checks to implementation block

* Replace add_trait_marker impl
2022-11-03 07:53:01 -06:00
Daniel Henry-Mantilla
d47d527ac2
Fix regression #127: support align in reprs again (#128) 2022-08-16 12:42:07 -06:00
Daniel Henry-Mantilla
1ebf7c251d
Cleanup: use a macro for reprs, use syn errors & fix cfg-target-arch (#124) 2022-08-12 06:50:51 -06:00
Tom Dohrmann
2c97676bfc
support deriving Pod for packed generic types. (#123)
* improve `#[repr]` parsing

* allow deriving `Pod` for packed generic types

* Revert "Update Cargo.toml"

This reverts commit 6632bcef2c.
2022-08-07 14:32:00 -06:00
daxpedda
237d7d3730
Avoid clippy::missing_const_for_fn (#115) 2022-07-08 17:16:25 -06:00
Gray Olson
1fb245c926
rename NoPadding to NoUninit and clarify docs (#95) 2022-03-29 21:25:51 -06:00
Gray Olson
1652a2dcd2
Support unions in derive macros (#94)
* 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
2022-03-29 20:01:48 -06:00
Gray Olson
b472189ff8
Add NoPadding, AnyBitPattern, and CheckedBitPattern traits (#91)
* 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
2022-03-29 17:01:02 -06:00
Yanchi Toth
b264926ac0
Emit padding-asserting code that doesn't trigger clippy::identity_op (#45) 2020-10-21 14:11:30 -06:00
Lucien Greathouse
19e61623d7
Blame struct name, better error for padding (#35) 2020-08-23 20:12:47 -06:00
Robin Appelman
24b71e078f
add derive macro for Contiguous (#31) 2020-08-21 16:08:34 -06:00
Lucien Greathouse
550dc3396c
Fix Pod derive to use explicit absolute path to 'core' (#32) 2020-08-21 15:13:55 -06:00
Robin Appelman
cf944452b7
add basic derive macro for Pod, Zeroable and TransparentWrapper for structs (#30)
* 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>
2020-08-20 19:04:36 -06:00