Skip to content

Commit e2a0efe

Browse files
notgullmbrubeck
authored andcommitted
Change unchecked_new to use const fn
1 parent 4c884c8 commit e2a0efe

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/lib.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,16 @@ impl<T: Float> Zero for OrderedFloat<T> {
235235
/// A NaN value cannot be stored in this type.
236236
#[derive(PartialOrd, PartialEq, Debug, Default, Clone, Copy)]
237237
#[repr(transparent)]
238-
pub struct NotNan<T: Float>(T);
238+
pub struct NotNan<T>(T);
239+
240+
impl<T> NotNan<T> {
241+
/// Create a NotNan value from a value that is guaranteed to not be NaN
242+
///
243+
/// Behaviour is undefined if `val` is NaN
244+
pub const unsafe fn unchecked_new(val: T) -> Self {
245+
NotNan(val)
246+
}
247+
}
239248

240249
impl<T: Float> NotNan<T> {
241250
/// Create a NotNan value.
@@ -248,14 +257,6 @@ impl<T: Float> NotNan<T> {
248257
}
249258
}
250259

251-
/// Create a NotNan value from a value that is guaranteed to not be NaN
252-
///
253-
/// Behaviour is undefined if `val` is NaN
254-
pub unsafe fn unchecked_new(val: T) -> Self {
255-
debug_assert!(!val.is_nan());
256-
NotNan(val)
257-
}
258-
259260
/// Get the value out.
260261
pub fn into_inner(self) -> T {
261262
self.0

tests/test.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,3 +584,10 @@ fn not_nan64_sum_product() {
584584
assert_eq!([a,b,c].iter().product::<NotNan<_>>(), a * b * c);
585585

586586
}
587+
588+
#[test]
589+
fn not_nan_usage_in_const_context() {
590+
const A: NotNan<f32> = unsafe { NotNan::unchecked_new(111f32) };
591+
592+
assert_eq!(A, NotNan::new(111f32).unwrap());
593+
}

0 commit comments

Comments
 (0)