Skip to content

Commit 106811f

Browse files
bors[bot]bradleyhardencuviper
authored
Merge #100
100: Add support for `bytemuck` traits r=cuviper a=bradleyharden `bytemuck` seems to be the *de facto* standard crate for safe transmuting. Because `Complex<T>` is `repr(C)`, it would satisfy the requirements for `Zeroable` and `Pod`, as long as `T` satisfies them as well. Add optional implemenations of `Zeroable` and `Pod`, gated behind the `bytemuck` feature. Closes #99 Co-authored-by: Bradley Harden <bradleyharden@gmail.com> Co-authored-by: Josh Stone <cuviper@gmail.com>
2 parents d0a06ca + f37e873 commit 106811f

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@ exclude = ["/bors.toml", "/ci/*", "/.github/*"]
1414
edition = "2018"
1515

1616
[package.metadata.docs.rs]
17-
features = ["std", "serde", "rand"]
17+
features = ["bytemuck", "std", "serde", "rand"]
1818

1919
[dependencies]
2020

21+
[dependencies.bytemuck]
22+
optional = true
23+
version = "1"
24+
2125
[dependencies.num-traits]
2226
version = "0.2.11"
2327
default-features = false

ci/test_full.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ if ! check_version $MSRV ; then
2828
fi
2929

3030
FEATURES=(libm serde)
31+
check_version 1.34 && FEATURES+=(bytemuck)
3132
check_version 1.36 && FEATURES+=(rand)
3233
echo "Testing supported features: ${FEATURES[*]}"
3334

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,18 @@ impl<T: FloatCore> Complex<T> {
599599
}
600600
}
601601

602+
// Safety: `Complex<T>` is `repr(C)` and contains only instances of `T`, so we
603+
// can guarantee it contains no *added* padding. Thus, if `T: Zeroable`,
604+
// `Complex<T>` is also `Zeroable`
605+
#[cfg(feature = "bytemuck")]
606+
unsafe impl<T: bytemuck::Zeroable> bytemuck::Zeroable for Complex<T> {}
607+
608+
// Safety: `Complex<T>` is `repr(C)` and contains only instances of `T`, so we
609+
// can guarantee it contains no *added* padding. Thus, if `T: Pod`,
610+
// `Complex<T>` is also `Pod`
611+
#[cfg(feature = "bytemuck")]
612+
unsafe impl<T: bytemuck::Pod> bytemuck::Pod for Complex<T> {}
613+
602614
impl<T: Clone + Num> From<T> for Complex<T> {
603615
#[inline]
604616
fn from(re: T) -> Self {

0 commit comments

Comments
 (0)