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`.
* `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.
* 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].
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.
* 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
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.
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.
* 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`.
* 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 :)
* 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
* 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
* 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
* 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
* update rustfmt.toml
replace `merge_imports = true` (deprecated) with `imports_granularity = "Crate"`
* run `cargo +nightly fmt`
* rewrite docs and rename `Wrapped` to `Inner`
rewriting some docs for conciseness
rename `Wrapped` to `Inner`, because it's hard to visually differentiate between
`Wrapper` and `Wrapped`
* impl missing `TransparentWrapper::wrap_{}` fns
Implement 3 new wrapping functions on `TransparentWrapper` providing new
conversions.
- `TransparentWrapper::wrap(s: Inner) -> Self`
- `TransparentWrapper::wrap_slice(s: &[Inner]) -> &[Self]`
- `TransparentWrapper::wrap_slice_mut(s: &mut [Inner]) -> &mut [Self]`
* impl `TransparentWrapper::unwrap_{}` fns
Implement counterparts to `TransparentWrapper::wrap_{}` functions
providing reverse conversions.
- `TransparentWrapper::unwrap(self) -> Inner`
- `TransparentWrapper::unwrap_ref(&self) -> &Inner`
- `TransparentWrapper::unwrap_mut(&mut self) -> &mut Inner`
- `TransparentWrapper::unwrap_slice(s: &[Self]) -> &[Inner]`
- `TransparentWrapper::unwrap_slice_mut(s: &mut [Self]) -> &mut [Inner]`
* add `TransparentWrapper` UB test
This test is only for MIRI to check all trait functions on
`TransparentWrapper` if they cause any UB.
The output of the functions is not checked.
* small `TransparentWrapper` doc adjustments
* change fn signature on `TransparentWrapper`
Methods on `TransparentWrapper` trait are now associated functions.
They now take `Self` instead of `self` as argument)
- `TransparentWrapper::unwrap(s: Self)`
- `TransparentWrapper::unwrap_ref(s: &Self)`
- `TransparentWrapper::unwrap_mut(s: &mut Self)`