Skip to content

Commit

Permalink
Implement KnownLayout for f16 and f128 (#2250) (#2312)
Browse files Browse the repository at this point in the history
Co-authored-by: usamoi <[email protected]>
  • Loading branch information
google-pr-creation-bot and usamoi authored Feb 6, 2025
1 parent 3acb652 commit 64df858
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
21 changes: 20 additions & 1 deletion src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,6 +1538,16 @@ mod tests {
IntoBytes,
!Unaligned
);
#[cfg(feature = "float-nightly")]
assert_impls!(
f16: KnownLayout,
Immutable,
TryFromBytes,
FromZeros,
FromBytes,
IntoBytes,
!Unaligned
);
assert_impls!(
f32: KnownLayout,
Immutable,
Expand All @@ -1556,7 +1566,16 @@ mod tests {
IntoBytes,
!Unaligned
);

#[cfg(feature = "float-nightly")]
assert_impls!(
f128: KnownLayout,
Immutable,
TryFromBytes,
FromZeros,
FromBytes,
IntoBytes,
!Unaligned
);
assert_impls!(
bool: KnownLayout,
Immutable,
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,6 +929,14 @@ impl_known_layout!(
NonZeroU64, NonZeroI64, NonZeroU128, NonZeroI128, NonZeroUsize, NonZeroIsize
);
#[rustfmt::skip]
#[cfg(feature = "float-nightly")]
impl_known_layout!(
#[cfg_attr(doc_cfg, doc(cfg(feature = "float-nightly")))]
f16,
#[cfg_attr(doc_cfg, doc(cfg(feature = "float-nightly")))]
f128
);
#[rustfmt::skip]
impl_known_layout!(
T => Option<T>,
T: ?Sized => PhantomData<T>,
Expand Down
5 changes: 3 additions & 2 deletions src/util/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,12 +532,13 @@ macro_rules! impl_known_layout {
($($tyvar:ident $(: ?$optbound:ident)? => $ty:ty),* $(,)?) => {
$(impl_known_layout!(@inner , $tyvar $(: ?$optbound)? => $ty);)*
};
($($ty:ty),*) => { $(impl_known_layout!(@inner , => $ty);)* };
(@inner $(const $constvar:ident : $constty:ty)? , $($tyvar:ident $(: ?$optbound:ident)?)? => $ty:ty) => {
($($(#[$attrs:meta])* $ty:ty),*) => { $(impl_known_layout!(@inner , => $(#[$attrs])* $ty);)* };
(@inner $(const $constvar:ident : $constty:ty)? , $($tyvar:ident $(: ?$optbound:ident)?)? => $(#[$attrs:meta])* $ty:ty) => {
const _: () = {
use core::ptr::NonNull;

#[allow(non_local_definitions)]
$(#[$attrs])*
// SAFETY: Delegates safety to `DstLayout::for_type`.
unsafe impl<$($tyvar $(: ?$optbound)?)? $(, const $constvar : $constty)?> KnownLayout for $ty {
#[allow(clippy::missing_inline_in_public_items)]
Expand Down

0 comments on commit 64df858

Please sign in to comment.