Skip to content

Commit

Permalink
Merge pull request #158 from kpreid/transparent
Browse files Browse the repository at this point in the history
Document that `NotNan` is `repr(transparent)`.
  • Loading branch information
mbrubeck authored Oct 15, 2024
2 parents c8422ce + 7abdbc9 commit e2c7a82
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ fn canonicalize_signed_zero<T: FloatCore>(x: T) -> T {
/// s.insert(OrderedFloat(NAN));
/// assert!(s.contains(&OrderedFloat(NAN)));
/// ```
///
/// # Representation
///
/// `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.
///
#[cfg_attr(
not(feature = "bytemuck"),
doc = "[`bytemuck`]: https://docs.rs/bytemuck/1/"
)]
#[derive(Default, Clone, Copy)]
#[repr(transparent)]
pub struct OrderedFloat<T>(pub T);
Expand Down Expand Up @@ -1157,6 +1169,18 @@ impl<T: FloatCore + Num> Num for OrderedFloat<T> {
/// // This will panic:
/// let c = a + b;
/// ```
///
/// # Representation
///
/// `NotNan` has `#[repr(transparent)]`, so it is sound to use
/// [transmute](core::mem::transmute) or pointer casts to convert between any type `T` and
/// `NotNan<T>`, as long as this does not create a NaN value.
/// However, consider using [`bytemuck`] as a safe alternative if possible.
///
#[cfg_attr(
not(feature = "bytemuck"),
doc = "[`bytemuck`]: https://docs.rs/bytemuck/1/"
)]
#[derive(PartialOrd, PartialEq, Default, Clone, Copy)]
#[repr(transparent)]
pub struct NotNan<T>(T);
Expand Down Expand Up @@ -2730,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 @@ -2752,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 e2c7a82

Please sign in to comment.