Commit Graph

68 Commits

Author SHA1 Message Date
Lokathor
09d32a1574 move the changes to the correct changelog. 2024-03-12 23:15:07 -06:00
Lokathor
069d4585a3 changelog 2024-03-12 23:11:27 -06:00
Lokathor
91bbbdf9a5 chore: Release bytemuck_derive version 1.6.0 2024-03-10 11:41:42 -06:00
Lokathor
adf7bf4f2b changelog 2024-03-10 11:38:52 -06:00
Andri
c705218630
Added custom crate path with tests (#209)
Co-authored-by: Andri <80914617+Shuray04@users.noreply.github.com>
2023-10-19 23:02:54 -06:00
Tom Dohrmann
d10fbfc6ff
allow deriving CheckedBitPattern for enums with fields (#171)
* 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>
2023-09-06 17:37:07 +02:00
Lokathor
88f5c8be75 chore: Release bytemuck_derive version 1.5.0 2023-09-05 15:15:09 -06:00
Lokathor
c22cf36bec docs. 2023-09-05 15:06:36 -06:00
Lokathor
01c2a07013 derive changelog 2023-09-05 15:01:53 -06:00
Valentin
3c1a0d9b35
Improve assembly of Contiguous derive (#200)
`from_integer` and `into_integer` are usually provided by the trait's
default implementation. We override this implementation because it goes
through `transmute_copy`, which can lead to inefficient assembly as seen
in https://github.com/Lokathor/bytemuck/issues/175 .
2023-09-05 13:41:44 -06:00
zachs18
58445b1250
Fix derive non-run doctests. (#204)
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.
2023-09-05 13:36:10 -06:00
zachs18
39b42b8fa3
Zeroable derive custom bounds (#196)
* Add space because rust-analyzer thinks it's a weird literal otherwise.

* Add custom bounds for Zeroable.

* Cleanup custom bounds code.

* Only parse explicit bounds for Zeroable.

* Qualify syn types.

* If no explicit bounds are given, apply the default bounds.

* Add perfect derive semantics to `#[zeroable(bound = "...")]`.
2023-07-26 13:50:58 -06:00
Connor Horman
3b81c85c60
Update proc-macro2 (#201) 2023-07-25 16:15:01 -06:00
Charles Lew
df5262b19c
Add #[doc(hidden)] on derive generated items. (#189) 2023-05-31 08:46:00 -06:00
C. K. Young
8391afa876
Make AnyBitPattern derive work for generic structs (#101)
* Allow generic structs to use `AnyBitPattern` derive.

* Attempt to nudge the CI into retrying.
2023-03-23 18:22:56 -06:00
Lokathor
b41f4cc1af chore: Release bytemuck_derive version 1.4.1 2023-03-23 15:40:54 -06:00
Lokathor
12c9871a71 changelog 2023-03-23 15:21:33 -06:00
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
Dirk Stolle
d1655f541b
Fix a few typos (#169) 2023-01-29 16:41:40 -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
Lokathor
2bcc0ef6de chore: Release bytemuck_derive version 1.4.0 2023-01-19 17:45:46 -07:00
Lokathor
28d3aa7ba9 changelog 2023-01-19 17:44:21 -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
Christopher Serr
7311e9b4b8
Implement ByteEq and ByteHash derives (#144)
* Implement `ByteEq` and `ByteHash` derives

This adds the derives `ByteEq` and `ByteHash` that can be used as an
alternative to the `Eq` / `PartialEq` and `Hash` derives from the
standard library. The difference is that these variants use `bytemuck`
to convert their values to byte slices before comparing / hashing them.
This allows the comparisons to turn into a simple `memcmp` / `bcmp` (or
completely inlined as a few vector instructions) and allows hashers to
process all bytes at once, possibly allowing for some vector operations
as well.

Here's a quick comparison of the generated assembly:
![https://i.imgur.com/CGTSWTZ.png](https://i.imgur.com/CGTSWTZ.png)

* Address review comments
2022-11-17 13:13:24 -07:00
Lokathor
5c9d81ca1a (cargo-release) version 1.3.0 2022-11-05 15:49:51 -06:00
Lokathor
86226cefc4 changelog. 2022-11-05 15:48:37 -06: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
Dennis Duda
f1571512d2
fix try_pod_read_unaligned... never having worked correctly (#138)
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.
2022-10-30 16:06:55 -06:00
Lokathor
3f2e91d3a6 (cargo-release) version 1.2.1 2022-08-16 21:01:26 -06:00
Lokathor
b7b438026d derive changlog. 2022-08-16 21:00:48 -06:00
Daniel Henry-Mantilla
d47d527ac2
Fix regression #127: support align in reprs again (#128) 2022-08-16 12:42:07 -06:00
Lokathor
9ea5f65e0f add a note about intended MSRV of the derives (none!) 2022-08-14 09:50:39 -06:00
Lokathor
f053800ce0 bump bytemuck_derive version with changelog 2022-08-14 09:49:53 -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
Lokathor
a053f1d887 derive version bump. 2022-07-18 19:27:55 -06:00
Edward
2864b52591
Fix/syn broken semver (#116)
* fix(derive): syn backwards compatibility

* fix: bump minimum syn version more after tests
2022-07-18 19:25:00 -06:00
piwicode
9d90497f7c
Replace LICENCES-* symlinks with a licence copy. (#118)
Fixes the licences content in bytemuck_derive.

Archives on crates.io does not contain a licence copy, but
just a file containing the symlink relative path instead.

https://static.crates.io/crates/bytemuck_derive/bytemuck_derive-1.1.0.crate
2022-07-13 06:53:17 -06:00
daxpedda
237d7d3730
Avoid clippy::missing_const_for_fn (#115) 2022-07-08 17:16:25 -06:00
Nicholas Bishop
96f2e549b3
Fix docstring typo (#104) 2022-05-13 14:51:43 -06:00
Lokathor
73ae1ee89e (cargo-release) version 1.1.0 2022-03-29 23:15:56 -06:00
Lokathor
4459b9295c changelog for the derives. 2022-03-29 23:15:32 -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
Lokathor
df53958735 (cargo-release) start next development iteration 1.0.2-alpha.0 2020-11-27 09:33:20 -07:00
Lokathor
1cf639571b (cargo-release) version 1.0.1 2020-11-27 09:32:36 -07:00
Fabio Valentini
51d68a4220
Include LICENSE files in published bytemuck_derive crates (#47) 2020-11-27 08:31:40 -07:00