Commit Graph

145 Commits

Author SHA1 Message Date
zachs18
291a924518
Allow casting between slices of ZSTs and slices of non-ZSTs in all cases. (#256)
Casting ZST to non-ZST will result in a slice length of 0.
Casting non-ZST to ZST will only work if the input slice has length 0, and results in a slice length of 0; if the input slice is not of length 0, PodCastError::OutputSliceWouldHaveSlop is returned.

Updates the docs of the PodCastError variants to reflect when they can occur.
Updates the docs of try_cast_slice (and checked::) to remove note about ZST <-> non-ZST not being allowed.
Update bytes_of(_mut) to remove ZST check, since casting [ZST] -> [u8] is now allowed directly using cast_slice(_mut).
Update must_cast_slice checks and doctests to allow [ZST] -> [non-ZST], but disallow [non-ZST] -> [ZST].
2024-07-30 17:05:11 -06:00
zachs18
1c75146bb6
Fix UB when dropping BoxBytes owning a zero-sized layout (#258) 2024-07-30 16:23:38 -06:00
zachs18
005ee3254f
Fix panics in try_cast_slice_box etc (#254)
* Fix panics in try_cast_slice_{box,rc,arc},cast_vec.

Casting from non-empty non-ZST slices to ZST slices now returns Err(SizeMismatch) (instead of panicking).
Casting from empty non-ZST slices to ZST slices is allowed and returns an empty slice (instead of panicking).
Casting from ZST slices to non-ZST slices is allowed and returns an empty slice (status quo).

* Add tests for cast_slice_box,rc,arc.
2024-07-21 10:24:43 -06:00
Trevor Gross
9b81537c5f
Support Zeroable and Pod for f16 and f128 (#251)
These are gated under a new feature flag `nightly_float`.

Fixes: <https://github.com/Lokathor/bytemuck/issues/250>
2024-06-18 21:24:29 -06:00
Lokathor
c9540064f3
Fix the errors CI is showing (#247)
* Change #![allow(clippy::missing_docs_in_private_items)] to #[allow(clippy::missing_docs_in_private_items)], also rustfmt went wild

* fix for 1.34 building.

* clippy be quiet
2024-05-28 12:22:59 -06:00
Andrew Gazelka
1c3751410f
fix clippy lints (#137) 2024-05-28 11:31:06 -06:00
Urgau
f9f7bb10b3
Address unexpected_cfgs warnings instead of allowing the lint (#243) 2024-05-24 13:21:58 -06:00
Lokathor
a41510f303 put const_zeroed into the changelog 2024-05-13 10:59:46 -06:00
Lokathor
9319f4858b make clippy be quiet about useless nonsense. 2024-05-13 10:53:18 -06:00
Lokathor
b39b6813a7 fix up docs warnings 2024-05-13 10:51:37 -06:00
Tyler Ruckinger
99e95084a6
support const zeroed() (#240) 2024-05-13 10:16:20 -06:00
zachs18
eeb6e9dd53
Update TransparentWrapper asserts (#232)
* Update TransparentWrapper asserts.

* Update TransparentWrapperAlloc assert comments.
2024-04-09 19:02:38 -06:00
zachs18
0c821ce9d5
Fix miri CI (#231)
* Remove -Zmiri-tag-raw-pointers

It is now the default.

* Remove workaround for miri/2423
2024-04-01 07:31:39 -06:00
zachs18
a888e5f2c0
Convert BoxBytes to/from boxed slices (#228)
* Allow converting BoxBytes to/from boxed slices of NoUninit, and from Box<str>.

* Implement From<Box<T>> for BoxBytes generically

* Minor cleanup
2024-03-12 23:00:32 -06:00
zachs18
1afddf9d9b
Under features = ["nightly_stdsimd"], only enable stdarch_x86_avx512 feature on x86 or x86_64 targets. (#225) 2024-02-07 17:45:17 -07:00
Lokathor
678c923f52 Closes https://github.com/Lokathor/bytemuck/issues/224, the Nightly feature name changed. 2024-02-06 19:50:43 -07:00
Logan Magee
5f93477418
Fix typo in AnyBitPattern docs (#221) 2024-01-24 19:58:00 -07:00
Hrvoje Nikšić
8dc32b1e50
Explicitly document that pod_read_unaligned and try_pod_read_unaligned don't panic on unallowed reads (#220)
* Explicitly document that pod_read_unaligned and try_pod_read_unaligned don't panic on unallowed reads

Their names make that reasonably clear already, but it doesn't hurt to
be explicit. Also, when choosing between `*from_bytes(x)` and
`pod_read_unaligned(x)`, it's good to have a clearly documented
criterion.

* Cosmetic doc changes

Add a couple of missing links, add a missing "like" (consistent with
other similar sentences).

* Remove redundant link

The redundant link emits a warning in `cargo doc`.
2024-01-24 17:30:59 -07:00
Lokathor
085a5f573e Revise the crate-level docs. 2023-11-26 12:44:24 -07:00
Julien Cretin
fd27a5b018
Add from_box_bytes and box_bytes_of with BoxBytes type (#211)
* Add from_box_bytes and box_bytes_of with BoxBytes type

* Apply suggestions from code review

Co-authored-by: zachs18 <8355914+zachs18@users.noreply.github.com>

* Add into_raw_parts

* Add tests

* Rename from_parts to from_raw_parts

---------

Co-authored-by: zachs18 <8355914+zachs18@users.noreply.github.com>
2023-10-19 22:50:33 -06:00
Lokathor
1ba4215050 Create align_offset feature so that we can continue to work on 1.34 2023-09-05 14:08:36 -06:00
sarah
caff759066
Use align_offset to check alignment (#176)
Co-authored-by: sarah <>
2023-09-05 14:04:52 -06:00
zachs18
b38d7d0e21
Have cfgs as part of the input to impl_unsafe_marker_for_simd (#207)
... so that the macro can correctly expand with `doc(cfg(...))` attributes under `feature = "nightly_docs"`.
2023-09-05 14:01:02 -06:00
Lokathor
d9b23e3518 rename these functions before publishing them. 2023-09-05 13:54:15 -06:00
Thom Chiovoloni
d790c043a8
Add functions for writing zeroed bytes to &mut impl Zeroable and &mut [impl Zeroable] (#193)
* Add functions for writing zeroed bytes to `&mut impl Zeroable` and `&mut [impl Zeroable]`

* Support `T: !Copy` in `fill_zero`/`write_zero`

* Zero bytes in the drop guard for write_zero

* Update src/lib.rs

Co-authored-by: Alphyr <47725341+a1phyr@users.noreply.github.com>

---------

Co-authored-by: Lokathor <zefria@gmail.com>
Co-authored-by: Alphyr <47725341+a1phyr@users.noreply.github.com>
2023-09-05 13:46:04 -06:00
José Luis Cruz
f047fb6aeb
improve documentation for optional features (#203)
- new feature `nightly_docs` enables `doc_cfg` unstable feature that allows showing explicit dependencies on features.
- new cargo alias `nightly_docs` for a more convenient local documentation build `cargo +nightly nightly_docs --open`.
- annotate exported items and implementations depending on some feature with the corresponding `doc(cfg())` attribute.
- add the `nightly_docs` feature to the `[package.metadata.docs.rs]` section.
2023-09-05 13:39:41 -06:00
Lukasz Anforowicz
db69feee1e
Use safe, public APIs where possible to remove some unsafe blocks. (#206) 2023-09-05 13:34:39 -06:00
zachs18
1ed1aaecf9
Remove FIXME and checks about NonZero[int] layout. (#199)
The layout was documented as guaranteed in 1.70.0 https://github.com/rust-lang/rust/pull/94786/
2023-07-17 10:01:10 -06:00
zachs18
588d89082d
Relax Pod for PhantomData (#195)
As with Zeroable for PhantomData.
2023-06-15 16:43:27 -06:00
zachs18
c286994891
must_cast: fix test failures and fmt. (#194)
* cargo fmt src/must.rs

* Fix must.rs test failures.

* Under miri, use should_panic instead of compile_fail for must_cast doctests.

* Add description for workaround.
2023-06-15 09:54:23 -06:00
MaulingMonkey
8f130f2b79
must_cast (#187) 2023-06-06 15:50:39 -06:00
Daniel Henry-Mantilla
e528c1662f
Add missing ?Sized for TransparentWrapperAlloc extension trait (#185)
* Add missing `?Sized` for `TransparentWrapperAlloc` extension trait

* chore: Release bytemuck version 1.13.2
2023-06-06 15:48:41 -06:00
Lokathor
1b1b9a0e89
Fix 177 (#178)
* reduce required bounds.

* clippy get off my case

* spelling

* clippy lints on tests.
2023-02-28 13:29:34 -07: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
Alphyr
fa5e5dd1d2
Simplify try_zeroed_box and try_zeroed_slice_box (#162) 2023-01-19 13:33:27 -07:00
Alphyr
2548e460cb
Add more Zeroable and ZeroableInOption impls (#158) 2023-01-19 10:03:41 -07:00
zachs18
117222d3e4
Implement AnyBitPattern for MaybeUninit<T> where T: AnyBitPattern. (#160) 2023-01-11 00:24:05 -07:00
Christofer Nolander
a758c0956e
impl Zeroable for Atomic* (#157)
* `impl Zeroable for Atomic*`

fixes #74

* added feature flag for `zeroable_atomics`

Added documentation for some feature flags on the `Zeroable` trait.
Also fixed some invalid references in doc-comments.
2022-12-21 13:54:31 -07:00
zachs18
4eea45e218
Refactor simd impls. (#156) 2022-12-18 13:44:25 -07:00
Scallop Ye
2c8ce95548
Experimental support for stdsimd types (#154) 2022-12-17 16:13:52 -07:00
zachs18
4c37652113
Implement CheckedBitPattern for core::num::NonZero*. (#151)
* Implement CheckedBitPattern for core::num::NonZero*.

* Add tests for CheckedBitPattern.

* Fix stacked borrows violation.

`<&mut T>::as_ptr` gives a `*const T` which is read-only. `<&mut T>::as_mut_ptr` is necessary to get a `*mut T`.

* MSRV fix.

* Add layout checks for CheckedBitPattern for NonZero*.

(Until Rust guarantees the layout of NonZero[int] matches [int].)
Also adds a test that will fail if any NonZero[int] has a different size or alignment than [int].
2022-11-30 12:09:17 -07:00
zachs18
911f43db98
Make checked::pod_read_unaligned require T: CheckedBitPattern (#150)
Currently, `checked::pod_read_unaligned` requires `T: AnyBitPattern`. (This was likely just a copy-paste error from when the function was introduced in #91.)
There should be no current UB nor semver breakage, since this only relaxes the bound (any type this previously worked with was already sound and will continue to work).
`checked::try_pod_read_unaligned has the correct `T: CheckedBitPattern` bound.
2022-11-28 15:12:32 -07:00
yvt
f14410f329
Implement Zeroable on {Cell,Reverse}<impl Zeroable> (#149)
* Implement `Zeroable` on `Cell<impl Zeroable>`

* Implement `Zeroable` on `Reverse<impl Zeroable>`
2022-11-20 21:28:55 -07:00
yvt
0da8f3a28f
Implement Zeroable on UnsafeCell<impl Zeroable> (#148) 2022-11-20 16:40:41 -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
Christopher Serr
02021fba24
Conditionally compile in Arc (#140)
Apparently the latest release of `bytemuck` doesn't compile on some of
the targets that it used to because it uses `Arc`. `Arc` is not a type
that exists on every target, because not every targets supports atomics.
2022-11-05 07:00:40 -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
Lokathor
12f5bfd62e clippy you are often a pain in my butt 2022-10-30 16:18:16 -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