Skip to content

Commit

Permalink
Add impl bytemuck::TransparentWrapper for OrderedFloat.
Browse files Browse the repository at this point in the history
This gives a more strongly-typed way for users to take advantage of
`OrderedFloat`’s representation.
  • Loading branch information
kpreid committed Oct 15, 2024
1 parent 4b07101 commit 7abdbc9
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fn canonicalize_signed_zero<T: FloatCore>(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<T>`.
/// However, consider using [`bytemuck`] as a safe alternative if possible.
Expand Down Expand Up @@ -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<T: Zeroable> Zeroable for OrderedFloat<T> {}

Expand All @@ -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<T> TransparentWrapper<T> for OrderedFloat<T> {}

#[test]
fn test_not_nan_bit_pattern() {
use bytemuck::checked::{try_cast, CheckedCastError};
Expand Down

0 comments on commit 7abdbc9

Please sign in to comment.