From 7abdbc96a734ce19a8564d3487f6d4b0e176e3e7 Mon Sep 17 00:00:00 2001 From: Kevin Reid Date: Tue, 15 Oct 2024 11:00:00 -0700 Subject: [PATCH] Add `impl bytemuck::TransparentWrapper for OrderedFloat`. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This gives a more strongly-typed way for users to take advantage of `OrderedFloat`’s representation. --- src/lib.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3d27509..38790b5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,7 +78,7 @@ fn canonicalize_signed_zero(x: T) -> T { /// /// # Representation /// -/// `OrderedFloat` has `#[repr(transparent)]` and permits any value, so it is sound to use +/// `OrderedFloat` has `#[repr(transparent)]` and permits any value, so it is sound to use /// [transmute](core::mem::transmute) or pointer casts to convert between any type `T` and /// `OrderedFloat`. /// However, consider using [`bytemuck`] as a safe alternative if possible. @@ -2754,7 +2754,7 @@ mod impl_arbitrary { #[cfg(feature = "bytemuck")] mod impl_bytemuck { use super::{FloatCore, NotNan, OrderedFloat}; - use bytemuck::{AnyBitPattern, CheckedBitPattern, NoUninit, Pod, Zeroable}; + use bytemuck::{AnyBitPattern, CheckedBitPattern, NoUninit, Pod, TransparentWrapper, Zeroable}; unsafe impl Zeroable for OrderedFloat {} @@ -2776,6 +2776,10 @@ mod impl_bytemuck { } } + // OrderedFloat allows any value of the contained type, so it is a TransparentWrapper. + // NotNan does not, so it is not. + unsafe impl TransparentWrapper for OrderedFloat {} + #[test] fn test_not_nan_bit_pattern() { use bytemuck::checked::{try_cast, CheckedCastError};