Skip to content

Commit 2226f13

Browse files
committed
Add support for bytemuck traits
`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.
1 parent 3a89daa commit 2226f13

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-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

src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,18 @@ impl<T: FloatCore> Complex<T> {
592592
}
593593
}
594594

595+
/// Saftey: `Complex<T>` is `repr(C)` and contains only instances of `T`, so we
596+
/// can guarantee it contains no *added* padding. Thus, if `T: Zeroable`,
597+
/// `Complex<T>` is also `Zeroable`
598+
#[cfg(feature = "bytemuck")]
599+
unsafe impl<T: bytemuck::Zeroable> bytemuck::Zeroable for Complex<T> {}
600+
601+
/// Saftey: `Complex<T>` is `repr(C)` and contains only instances of `T`, so we
602+
/// can guarantee it contains no *added* padding. Thus, if `T: Pod`,
603+
/// `Complex<T>` is also `Pod`
604+
#[cfg(feature = "bytemuck")]
605+
unsafe impl<T: bytemuck::Pod> bytemuck::Pod for Complex<T> {}
606+
595607
impl<T: Clone + Num> From<T> for Complex<T> {
596608
#[inline]
597609
fn from(re: T) -> Self {

0 commit comments

Comments
 (0)