Commit Graph

123 Commits

Author SHA1 Message Date
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
zachs18
09dd2ffd68
Add cast_{arc,rc} (and slice and try), and {wrap,peel}_{arc,rc}. (#132)
* Add `allocation::{try_,}cast_{arc,rc}`, and add `{wrap,peel}_{arc,rc}` to `TransparentWrapperAlloc`.

* Avoid intermediate slice reference in `try_cast_slice_{arc,rc}`.

* remove `unsafe` block; run `cargo +nightly fmt` (ignoring files I didn't modify)

* Make `cast_rc` (etc) have the same bounds as `cast_mut`, due to the existence of `Rc::get_mut_unchecked`.
2022-09-01 17:23:28 -06:00
pali
bbd6a927ad
fix something_went_wrong on spirv (#125)
Removed Display trait bound on the SPIR-V architecture.
2022-08-11 12:11:48 -06:00
Waffle Maybe
331762b014
run cargo fmt (#120) 2022-07-24 09:27:49 -06:00
Waffle Maybe
0fe2b2c568
Add {wrap,peel}_box functions to TransparentWrapperAlloc (#119)
* Add `{wrap,peel}_box` functions to `TransparentWrapperAlloc`

* Add tests for `{wrap,peel}_box`
2022-07-23 21:09:32 -06:00
Gray Olson
e612031c1f
Add try_zeroed_vec and zeroed_vec (#117)
* add `try_zeroed_vec` and `zeroed_vec`, adjust impl of `try_zeroed_slice_box`

* go back to returning an error rather than calling handle_alloc_error

* use boxed slice .into_vec instead :)
2022-07-07 08:54:34 -06:00
Lokathor
6beb5382ec move this little guy where he goes 2022-07-02 15:30:08 -06:00
Lokathor
fda9e9f852 improve docs 2022-07-02 15:29:48 -06:00
Lokathor
1f265a9e0d Move the FooInOption traits into their own files for easier organization. 2022-07-02 15:21:11 -06:00
Edward
abe55e525e
feat: add TransparentWrapperAlloc trait (#111)
* feat: add TransparentWrapperAlloc trait

* implements wrap_vec and peel_vec

* fix: review comments

* remote asserts as they don't do anything
* avoid issue with stacked borrows
2022-06-28 17:55:31 -06:00
Edward
e8cd71df54
feat: ZeroableInOption and PodInOption traits (#114)
* same as Zeroable and Pod but for types which are Zeroable and Pod when
  wrapped in Option
* allows downstream users to implement Zeroable and Pod for their own
  Option<T> types without running into orphan rules
2022-06-28 17:54:56 -06:00
John
d691aa6a14
implement NoUninit for NonZero* (#107) 2022-06-06 13:20:34 -06:00
HeroicKatora
21e2ac55d7
Note on shared references to Pod-like types (#106)
* Note on shared references to Pod-like types

* Expand explanation of no-interior-mutability
2022-05-19 11:47:54 -06:00
mystise
d8b67687f3
Add cast_slice_box and try_cast_slice_box, changed try_cast_vec to adjust length and capacity when valid. (#100)
* Added try_cast_slice_box and cast_slice_box

* Modified try_cast_slice_box and try_cast_vec to recalculate the length if the change in size between the two types is valid.

* Ran cargo format
2022-04-19 11:50:28 -06:00
Gray Olson
1fb245c926
rename NoPadding to NoUninit and clarify docs (#95) 2022-03-29 21:25:51 -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
7fa51a420d
pod_read_unaligned and try_pod_read_unaligned (#92)
* pod_read_unaligned and try_pod_read_unaligned

* only do all-features on nightly.

* I forgot we can't use .cast on 1.34
2022-03-01 18:09:49 -07:00
Christopher Serr
6029639753
Implement Pod and Zeroable for AArch64 NEON Vectors (#80)
* Implement `Pod` and `Zeroable` for AArch64 NEON Vectors

The NEON intrinsics will be stabilized in Rust 1.59.

* Compile all-features only on nightly
2021-12-21 19:01:47 -07:00
Lokathor
c8ffbd19dd Merge branch 'main' of https://github.com/Lokathor/bytemuck into main 2021-12-15 23:16:42 -07:00
Lokathor
dd01ccae42 nightly_portable_simd 2021-12-15 23:16:38 -07:00
Alphyr
3923766b67
Impls for PhantomPinned and more PhantomData (#79) 2021-12-08 18:59:22 -07:00
Christopher Serr
944c90ca3f
Support WASM SIMD's v128 type (#73)
Due to the MSRV it is behind a feature gate.
2021-10-15 13:46:52 -06:00
Gauthier Segay
9ea604addb
minor doc patch to help users adjust their Cargo.toml for usage of the allocation module. (#76)
* just pointer to how to set the crate feature

helps the Cargo newbies to enable the feature easily while reading the documentation.

* Update allocation.rs
2021-10-15 13:45:16 -06:00