Skip to content

Commit

Permalink
Suppot the unstable f16 and f128 types
Browse files Browse the repository at this point in the history
Closes #1999
  • Loading branch information
Brezak committed Nov 9, 2024
1 parent e3b2ec1 commit 2c86de4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ alloc = []
derive = ["zerocopy-derive"]
simd = []
simd-nightly = ["simd"]
# This feature enables support for the unstable f16 and f128 types
float-nightly = []
std = ["alloc"]
# This feature depends on all other features that work on the stable compiler.
# We make no stability guarantees about this feature; it may be modified or
Expand Down
2 changes: 2 additions & 0 deletions src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ safety_comment! {
unsafe_impl!(isize: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
unsafe_impl!(f32: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
unsafe_impl!(f64: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
unsafe_impl!(#[cfg(feature="float-nightly")] f16: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
unsafe_impl!(#[cfg(feature="float-nightly")] f128: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
}

safety_comment! {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@
all(feature = "simd-nightly", any(target_arch = "powerpc", target_arch = "powerpc64")),
feature(stdarch_powerpc)
)]
#![cfg_attr(feature = "float-nightly", feature(f16, f128))]
#![cfg_attr(doc_cfg, feature(doc_cfg))]
#![cfg_attr(
__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS,
Expand Down
31 changes: 29 additions & 2 deletions src/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,37 @@ macro_rules! unsafe_impl {
unsafe_impl!(@method $trait $(; |$candidate: MaybeAligned<$repr>| $is_bit_valid)?);
}
};

// Implement all `$traits` for `$ty` with no bounds.
($ty:ty: $($traits:ident),*) => {
$( unsafe_impl!($ty: $traits); )*
//
// The 2 arms under this one are there so we can apply
// N attributes for each one of M trait implementations.
// The simple solution of:
//
// ($(#[$attrs:meta])* $ty:ty: $($traits:ident),*) => {
// $( unsafe_impl!( $(#[$attrs])* $ty: $traits ) );*
// }
//
// Won't work. The macro processor sees that the outer repetition
// contains both $attrs and $traits and expects them to match the same
// amount of fragments.
//
// To solve this we must:
// 1. Pack the attributes into a single token tree fragment we can match over
// 2. Expand the traits.
// 3. Unpack and expand the attributes.
($(#[$attrs:meta])* $ty:ty: $($traits:ident),*) => {
unsafe_impl!(@impl_traits_with_packed_attrs { $( #[$attrs])* } $ty: $($traits),*)
};

(@impl_traits_with_packed_attrs $attrs:tt $ty:ty: $($traits:ident),*) => {
$( unsafe_impl!(@unpack_attrs $attrs $ty: $traits); )*
};

(@unpack_attrs { $(#[$attrs:meta])* } $ty:ty: $traits:ident) => {
unsafe_impl!($(#[$attrs])* $ty: $traits);
};

// This arm is identical to the following one, except it contains a
// preceding `const`. If we attempt to handle these with a single arm, there
// is an inherent ambiguity between `const` (the keyword) and `const` (the
Expand Down

0 comments on commit 2c86de4

Please sign in to comment.