@@ -75,6 +75,18 @@ fn canonicalize_signed_zero<T: FloatCore>(x: T) -> T {
75
75
/// s.insert(OrderedFloat(NAN));
76
76
/// assert!(s.contains(&OrderedFloat(NAN)));
77
77
/// ```
78
+ ///
79
+ /// # Representation
80
+ ///
81
+ /// `OrderedFloat` has `#[repr(transparent)]` and permits any value, so it is sound to use
82
+ /// [transmute](core::mem::transmute) or pointer casts to convert between any type `T` and
83
+ /// `OrderedFloat<T>`.
84
+ /// However, consider using [`bytemuck`] as a safe alternative if possible.
85
+ ///
86
+ #[ cfg_attr(
87
+ not( feature = "bytemuck" ) ,
88
+ doc = "[`bytemuck`]: https://docs.rs/bytemuck/1/"
89
+ ) ]
78
90
#[ derive( Default , Clone , Copy ) ]
79
91
#[ repr( transparent) ]
80
92
pub struct OrderedFloat < T > ( pub T ) ;
@@ -1157,6 +1169,18 @@ impl<T: FloatCore + Num> Num for OrderedFloat<T> {
1157
1169
/// // This will panic:
1158
1170
/// let c = a + b;
1159
1171
/// ```
1172
+ ///
1173
+ /// # Representation
1174
+ ///
1175
+ /// `NotNan` has `#[repr(transparent)]`, so it is sound to use
1176
+ /// [transmute](core::mem::transmute) or pointer casts to convert between any type `T` and
1177
+ /// `NotNan<T>`, as long as this does not create a NaN value.
1178
+ /// However, consider using [`bytemuck`] as a safe alternative if possible.
1179
+ ///
1180
+ #[ cfg_attr(
1181
+ not( feature = "bytemuck" ) ,
1182
+ doc = "[`bytemuck`]: https://docs.rs/bytemuck/1/"
1183
+ ) ]
1160
1184
#[ derive( PartialOrd , PartialEq , Default , Clone , Copy ) ]
1161
1185
#[ repr( transparent) ]
1162
1186
pub struct NotNan < T > ( T ) ;
@@ -2730,7 +2754,7 @@ mod impl_arbitrary {
2730
2754
#[ cfg( feature = "bytemuck" ) ]
2731
2755
mod impl_bytemuck {
2732
2756
use super :: { FloatCore , NotNan , OrderedFloat } ;
2733
- use bytemuck:: { AnyBitPattern , CheckedBitPattern , NoUninit , Pod , Zeroable } ;
2757
+ use bytemuck:: { AnyBitPattern , CheckedBitPattern , NoUninit , Pod , TransparentWrapper , Zeroable } ;
2734
2758
2735
2759
unsafe impl < T : Zeroable > Zeroable for OrderedFloat < T > { }
2736
2760
@@ -2752,6 +2776,10 @@ mod impl_bytemuck {
2752
2776
}
2753
2777
}
2754
2778
2779
+ // OrderedFloat allows any value of the contained type, so it is a TransparentWrapper.
2780
+ // NotNan does not, so it is not.
2781
+ unsafe impl < T > TransparentWrapper < T > for OrderedFloat < T > { }
2782
+
2755
2783
#[ test]
2756
2784
fn test_not_nan_bit_pattern ( ) {
2757
2785
use bytemuck:: checked:: { try_cast, CheckedCastError } ;
0 commit comments