Commit Graph

91 Commits

Author SHA1 Message Date
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