Commit Graph

147 Commits

Author SHA1 Message Date
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
Christopher Durham
1a4c11a675
Replace unsound usage of transmute_copy (#72)
* Use transmute! to enforce safer transmute_copy use

* Test fix for #71
2021-07-22 23:12:56 -06:00
Lokathor
05d956f3b4
ptr pod impl fix. (#65)
* gate the unsound impl.

* changelog

* update cargo.toml
2021-06-13 08:40:58 -06:00
Daniel McNab
291b2d2513
Just gate the Display impl instead (#69)
The old version didn't compile (because of the extra semicolon)
2021-06-13 08:12:24 -06:00
Daniel McNab
043c18b4ba
Allow building on spir-v (#67)
* Allow building on spir-v

* Remove spurious `self`
2021-06-11 13:30:50 -06:00
Lokathor
99ecb1f6cb rename "unwrap" to "peel". 2021-06-10 14:05:15 -06:00
Connor Horman
cd8dbd64e9
Attach min_const_generics to a crate feature (#63)
* Attach min_const_generics to a crate feature

* Remove `min_const_generics` detection logic
2021-06-05 08:25:57 -06:00
Zakarum
f3e16c7051
Implement Pod and Zeroable for arrays of any size (#59)
* Implement Pod and Zeroable for arrays of any size

Only when rustc version is 1.51 or newer

* Rename cfg flag after feature name
2021-04-01 19:56:49 -06:00
Luis Wirth
30a96066fa
[Feature] extend TransparentWrapper conversion functions (#58)
* 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)`
2021-03-28 23:11:13 -06:00
Lokathor
e5d5061854
adjust the code to match the claims of the docs. (#53) 2021-02-17 21:43:32 -07:00
Lokathor
7e76723b3d
pod_collect_to_vec (#50)
* add the requested feature.

* maybe we need to be more aggressive than cargo clean is?
2021-01-13 16:49:11 -07:00
Marvin Löbel
92ce415317
Prevent try_zeroed_box<T>() from reserving size_of<T>() space on the stack. (#43)
* Add test

* Change try_zeroed_box implementation to not allocate space for T on the stack

* Add second test
2020-10-21 14:11:09 -06:00
Thom Chiovoloni
64cc5973c4
Add more tests, overhaul CI, and fix plug soundness hole in bytemuck::offset_of! (#38)
* Ensure functions that take generic <A, B> don't call them <T, U>

* Plug soundness hole in offset_of with safe_packed_borrows lint

* Add some more tests I noticed were missing (still many missing)

* Make CI way more thorough

* Fix running MSRV tests with unsupported `--feature`s

* Disable derive tests in sanitizers, and fix miri ci
2020-09-06 18:56:41 -06:00
Robin Appelman
24b71e078f
add derive macro for Contiguous (#31) 2020-08-21 16:08:34 -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
Lokathor
4d819357ba opt-in zeroable for maybe_unint 2020-08-19 21:43:44 -06:00
Lokathor
6414f14815 Use transmute_copy where appropriate. Format some docs. 2020-08-18 17:02:06 -06:00
Lokathor
436abe3e89 convert to resize_with 2020-07-25 15:53:27 -06:00
Lokathor
a2baf0abb3 use the correct (weaker) trait bound. 2020-07-25 15:50:10 -06:00
Lokathor
a1ef436bfa test new CI 2020-07-25 15:43:31 -06:00
Soveu
52be254e2c
zeroed_slice_box returns Result (#28)
It returns `Result<(), Box>` instead of `Box`
`cargo check` catched it
How did it pass CI?
2020-07-25 15:26:28 -06:00
Lokathor
4f2a822cb6 Correct the error variant returned in some situations. 2020-07-24 20:41:57 -06:00
Lokathor
51f1121d4f Add zeroed_slice_box 2020-07-24 20:34:32 -06:00
Lokathor
b531b050f3 Part of https://github.com/Lokathor/bytemuck/issues/21 2020-07-24 06:57:41 -06:00
Lokathor
1be7a90f16 Closes https://github.com/Lokathor/bytemuck/issues/22 2020-07-24 06:53:43 -06:00
Lokathor
3fc1e6ca4c Merge branch 'master' of https://github.com/Lokathor/bytemuck 2020-07-23 20:22:52 -06:00
Lokathor
c9a387a8da update offset_of to have 2-arg and 3-arg versions. 2020-07-23 20:21:09 -06:00
Lokathor
93e9b53571
Merge pull request #17 from HeroicKatora/master
Add a method to allocate zeroed slices
2020-07-23 19:06:59 -06:00
Andreas Molzer
11e791c88a Add a method to allocate zeroed slices
Note that the method requires a Pod and not only a Zeroable structure
since only the latter guarantees that slices have a consistent layout.
We need to compute this layout to manually allocate it.
2020-07-23 22:10:49 +02:00
Lokathor
fa686336e7 factor the panic to one spot. 2020-02-06 19:38:05 -07:00
Lokathor
cd8ed37eae Remove the unreachable_unchecked 2020-02-06 19:18:10 -07:00
Lokathor
326c78d127 formatting 2020-02-04 00:20:12 -07:00
Lokathor
c0c3b38025 eliminate some dead branches 2020-02-04 00:18:22 -07:00
Lokathor
6feac18319
Merge pull request #15 from thomcc/transparent
Add TransparentWrapper marker trait
2020-01-29 01:16:21 -07:00
Lokathor
f5d5523f77 Pod safety documentation change
The previous requirements were incorrect because I misunderstood how `repr(packed)` actually means `repr(Rust, packed)` rather than `repr(C, packed)` as I first assumed.
Closes https://github.com/Lokathor/bytemuck/issues/14
2020-01-29 01:08:14 -07:00
Thom Chiovoloni
e415a28966 Duplicate the doctests for the sake of miri 2020-01-11 08:01:58 -08:00
Thom Chiovoloni
f62e320bba Rework TransparentWrapper to not require that the wrapped type be public 2020-01-11 07:31:17 -08:00
Thom Chiovoloni
c383083c16 Add TransparentWrapper marker trait 2020-01-11 06:28:11 -08:00
Thom Chiovoloni
d9ca3306dd Add Contiguous trait 2019-12-11 19:04:23 -08:00
Thom Chiovoloni
ac3133170a Add a sound offset_of macro 2019-12-10 08:40:14 -08:00
Lokathor
93dade1ab1 documentation consistency 2019-12-06 12:31:51 -07:00
Simon Sapin
6e54fc51fb Add from_bytes, from_bytes_mut, try_from_bytes, try_from_bytes_mut
This can be useful when receiving a bytes buffer from I/O
2019-12-06 13:23:08 +01:00
Lokathor
de517daffa MaybeUninit has to wait for later. 2019-11-25 19:21:38 -07:00
Lokathor
91aeef0b45 much more docs. 2019-11-25 19:09:26 -07:00
Lokathor
af499f4c7c Remove .cast method usage and rustfmt 2019-11-25 18:46:23 -07:00
Lokathor
efc80f1c4d Update allocation.rs 2019-11-18 20:39:07 -07:00
Lokathor
fba68534dc more trait! 2019-11-10 23:41:41 -07:00
Lokathor
3fd763c764 Fix a Miri failure 2019-11-04 19:12:59 -07:00
Lokathor
79143a6295 cargo fmt is a strict one 2019-11-03 23:55:51 -07:00
Lokathor
af3f4404df document the failure case
Closes https://github.com/Lokathor/bytemuck/issues/3
2019-11-03 23:45:38 -07:00
Lokathor
15bbbb29fd docs 2019-11-03 23:39:06 -07:00
Lokathor
24ad0df209 Update zeroable.rs 2019-10-05 19:26:47 -06:00
Lokathor
55a4eef7b6 improve the ZST case 2019-09-21 17:39:37 -06:00
Lokathor
72d3ae2419 clarify when allocation related operations are available. 2019-09-21 17:33:36 -06:00
Lokathor
dad138dba3
guard against zero sized types with an error
We could update this to just not allocate in the future but I don't believe that the docs give us assurances in that area.
2019-09-20 13:05:09 -06:00
Lokathor
6fea4bfa58
make the deref explicit 2019-09-20 11:40:44 -06:00
Lokathor
40cb0d19c3
comments 2019-09-20 11:12:40 -06:00
Lokathor
d4fde53386
Update src/zeroable.rs
Co-Authored-By: Daniel Henry-Mantilla <daniel.henry.mantilla@gmail.com>
2019-09-20 11:10:01 -06:00
Lokathor
ca902ff52d
mention repr(packed) 2019-09-20 11:09:33 -06:00
Lokathor
28603a3198
gotta mask that unsafe 2019-09-20 11:08:40 -06:00
Lokathor
cc0a6aa751
use a result 2019-09-20 11:06:50 -06:00
Lokathor
973ec91f79
typo, remove old imports 2019-09-20 11:05:33 -06:00
Lokathor
8cdf598be4 formatting 2019-09-20 09:10:03 -06:00
Lokathor
876da036e4 zeroed out boxes 2019-09-20 09:01:23 -06:00
Lokathor
427893ab9b same fix for as_mut 2019-09-20 08:20:18 -06:00
Lokathor
c7e124e641 Remove Arc and Rc support, they've got repr(rust) internals. 2019-09-20 00:52:55 -06:00
Lokathor
2b8ef93dee simplify reborrow 2019-09-20 00:32:35 -06:00
Lokathor
22e1b7a6f6 ralith says to slim down the error 2019-09-19 23:11:06 -06:00
Lokathor
372ad15af9 commentary fix 2019-09-19 22:53:35 -06:00
Lokathor
7584fb2d19 Update lib.rs 2019-09-19 22:19:38 -06:00
Lokathor
a1b7c21a65 Update allocation.rs 2019-09-19 22:15:17 -06:00
Lokathor
0bae2db1b8 no_std + alloc 2019-09-19 22:11:25 -06:00
Lokathor
a45102007c more 2019-09-19 21:35:09 -06:00
Lokathor
70da6d7a77 zeroable 2019-09-19 19:23:24 -06:00