Skip to content

Commit 90c438b

Browse files
Suppot the unstable f16 and f128 types (#2042) (#2058)
Closes #1999 Co-authored-by: Brezak <bezak.adam@proton.me>
1 parent 9dc79c2 commit 90c438b

File tree

5 files changed

+43
-2
lines changed

5 files changed

+43
-2
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ alloc = []
6161
derive = ["zerocopy-derive"]
6262
simd = []
6363
simd-nightly = ["simd"]
64+
float-nightly = []
6465
std = ["alloc"]
6566
# This feature depends on all other features that work on the stable compiler.
6667
# We make no stability guarantees about this feature; it may be modified or

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ for network parsing.
137137
available on nightly. Since these types are unstable, support for any type
138138
may be removed at any point in the future.
139139

140+
- **`float-nightly`**
141+
Adds support for the unstable `f16` and `f128` types. These types are
142+
not yet fully implemented and may not be supported on all platforms.
143+
140144
[duplicate-import-errors]: https://github.com/google/zerocopy/issues/1587
141145
[simd-layout]: https://rust-lang.github.io/unsafe-code-guidelines/layout/packed-simd-vectors.html
142146

src/impls.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ safety_comment! {
7272
unsafe_impl!(isize: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
7373
unsafe_impl!(f32: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
7474
unsafe_impl!(f64: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
75+
#[cfg(feature = "float-nightly")]
76+
unsafe_impl!(#[cfg_attr(doc_cfg, doc(cfg(feature = "float-nightly")))] f16: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
77+
#[cfg(feature = "float-nightly")]
78+
unsafe_impl!(#[cfg_attr(doc_cfg, doc(cfg(feature = "float-nightly")))] f128: Immutable, TryFromBytes, FromZeros, FromBytes, IntoBytes);
7579
}
7680

7781
safety_comment! {

src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@
137137
//! available on nightly. Since these types are unstable, support for any type
138138
//! may be removed at any point in the future.
139139
//!
140+
//! - **`float-nightly`**
141+
//! Adds support for the unstable `f16` and `f128` types. These types are
142+
//! not yet fully implemented and may not be supported on all platforms.
143+
//!
140144
//! [duplicate-import-errors]: https://github.com/google/zerocopy/issues/1587
141145
//! [simd-layout]: https://rust-lang.github.io/unsafe-code-guidelines/layout/packed-simd-vectors.html
142146
//!
@@ -304,6 +308,7 @@
304308
all(feature = "simd-nightly", any(target_arch = "powerpc", target_arch = "powerpc64")),
305309
feature(stdarch_powerpc)
306310
)]
311+
#![cfg_attr(feature = "float-nightly", feature(f16, f128))]
307312
#![cfg_attr(doc_cfg, feature(doc_cfg))]
308313
#![cfg_attr(
309314
__ZEROCOPY_INTERNAL_USE_ONLY_NIGHTLY_FEATURES_IN_TESTS,

src/util/macros.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,37 @@ macro_rules! unsafe_impl {
6262
unsafe_impl!(@method $trait $(; |$candidate: MaybeAligned<$repr>| $is_bit_valid)?);
6363
}
6464
};
65+
6566
// Implement all `$traits` for `$ty` with no bounds.
66-
($ty:ty: $($traits:ident),*) => {
67-
$( unsafe_impl!($ty: $traits); )*
67+
//
68+
// The 2 arms under this one are there so we can apply
69+
// N attributes for each one of M trait implementations.
70+
// The simple solution of:
71+
//
72+
// ($(#[$attrs:meta])* $ty:ty: $($traits:ident),*) => {
73+
// $( unsafe_impl!( $(#[$attrs])* $ty: $traits ) );*
74+
// }
75+
//
76+
// Won't work. The macro processor sees that the outer repetition
77+
// contains both $attrs and $traits and expects them to match the same
78+
// amount of fragments.
79+
//
80+
// To solve this we must:
81+
// 1. Pack the attributes into a single token tree fragment we can match over.
82+
// 2. Expand the traits.
83+
// 3. Unpack and expand the attributes.
84+
($(#[$attrs:meta])* $ty:ty: $($traits:ident),*) => {
85+
unsafe_impl!(@impl_traits_with_packed_attrs { $(#[$attrs])* } $ty: $($traits),*)
6886
};
87+
88+
(@impl_traits_with_packed_attrs $attrs:tt $ty:ty: $($traits:ident),*) => {
89+
$( unsafe_impl!(@unpack_attrs $attrs $ty: $traits); )*
90+
};
91+
92+
(@unpack_attrs { $(#[$attrs:meta])* } $ty:ty: $traits:ident) => {
93+
unsafe_impl!($(#[$attrs])* $ty: $traits);
94+
};
95+
6996
// This arm is identical to the following one, except it contains a
7097
// preceding `const`. If we attempt to handle these with a single arm, there
7198
// is an inherent ambiguity between `const` (the keyword) and `const` (the

0 commit comments

Comments
 (0)