niche-value 0.1.0

Numeric types that forbid a chosen value (or NaN / infinity), so Option<T> is niche-optimized. Generalizes nonmax.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Float niche types.
//!
//! Two families:
//! * **bit-exact** — [`NonValueF32`]/[`NonValueF64`], forbidding one bit pattern
//!   (const generic `BITS`), plus the `NonMax*`/`NonMin*` aliases.
//! * **class-based** — [`NonNanF32`], [`NonInfF32`], … which reject a whole
//!   semantic class at construction while anchoring their niche on one
//!   representative pattern.
//!
//! Rejection is always by **bit pattern**, never by mathematical value (forced
//! by soundness: a value check would let a `NaN` equal to the anchor slip
//! through, forming an unsound `NonZero(0)`). Consequences: `+0.0` and `-0.0`
//! are distinct bit patterns, and a bit-exact type forbids exactly one of them.

use crate::error::{ParseFloatError, TryFromFloatError};

// ============================ bit-exact family ============================

macro_rules! niche_float {
    ($nv:ident, $prim:ident, $bits:ident, $nonzero:ident, $nonmax:ident, $nonmin:ident) => {
        #[doc = concat!("An [`", stringify!($prim), "`] whose bit pattern is known not to equal `BITS`.")]
        ///
        /// `Option<Self>` is niche-optimized to the size of the primitive.
        /// Rejection is bit-exact (see the module docs). Because it can still
        /// hold `NaN`, it implements only [`PartialEq`]/[`PartialOrd`] (by
        /// value, matching the primitive) — not [`Eq`]/[`Ord`]/[`Hash`].
        #[derive(Clone, Copy)]
        #[repr(transparent)]
        pub struct $nv<const BITS: $bits>(core::num::$nonzero);

        impl<const BITS: $bits> $nv<BITS> {
            /// Creates a value if `value.to_bits() != BITS`, otherwise `None`.
            #[inline]
            pub const fn new(value: $prim) -> Option<Self> {
                match core::num::$nonzero::new(value.to_bits() ^ BITS) {
                    None => None,
                    Some(inner) => Some(Self(inner)),
                }
            }

            /// Creates a value without checking its bit pattern.
            ///
            /// # Safety
            ///
            /// `value.to_bits()` must not equal `BITS`.
            #[inline]
            pub const unsafe fn new_unchecked(value: $prim) -> Self {
                // SAFETY: caller guarantees `value.to_bits() != BITS`.
                Self(unsafe { core::num::$nonzero::new_unchecked(value.to_bits() ^ BITS) })
            }

            /// Returns the value as a primitive.
            #[inline]
            pub const fn get(&self) -> $prim {
                $prim::from_bits(self.0.get() ^ BITS)
            }
        }

        impl<const BITS: $bits> PartialEq for $nv<BITS> {
            #[inline]
            fn eq(&self, other: &Self) -> bool {
                self.get() == other.get()
            }
        }
        impl<const BITS: $bits> PartialOrd for $nv<BITS> {
            #[inline]
            fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
                self.get().partial_cmp(&other.get())
            }
        }
        impl<const BITS: $bits> From<$nv<BITS>> for $prim {
            #[inline]
            fn from(value: $nv<BITS>) -> Self {
                value.get()
            }
        }
        impl<const BITS: $bits> core::convert::TryFrom<$prim> for $nv<BITS> {
            type Error = TryFromFloatError;
            #[inline]
            fn try_from(value: $prim) -> Result<Self, Self::Error> {
                Self::new(value).ok_or(TryFromFloatError(()))
            }
        }
        impl<const BITS: $bits> core::str::FromStr for $nv<BITS> {
            type Err = ParseFloatError;
            #[inline]
            fn from_str(value: &str) -> Result<Self, Self::Err> {
                Self::new(<$prim as core::str::FromStr>::from_str(value)?).ok_or(ParseFloatError(()))
            }
        }

        impl<const BITS: $bits> core::fmt::Debug for $nv<BITS> {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::Debug::fmt(&self.get(), f)
            }
        }
        impl<const BITS: $bits> core::fmt::Display for $nv<BITS> {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::Display::fmt(&self.get(), f)
            }
        }
        impl<const BITS: $bits> core::fmt::LowerExp for $nv<BITS> {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::LowerExp::fmt(&self.get(), f)
            }
        }
        impl<const BITS: $bits> core::fmt::UpperExp for $nv<BITS> {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::UpperExp::fmt(&self.get(), f)
            }
        }

        #[cfg(feature = "serde")]
        impl<const BITS: $bits> serde::Serialize for $nv<BITS> {
            fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                self.get().serialize(serializer)
            }
        }
        #[cfg(feature = "serde")]
        impl<'de, const BITS: $bits> serde::Deserialize<'de> for $nv<BITS> {
            fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                let value = <$prim as serde::Deserialize>::deserialize(deserializer)?;
                Self::new(value).ok_or_else(|| serde::de::Error::custom("bit pattern is forbidden by niche type"))
            }
        }

        #[doc = concat!("A [`", stringify!($prim), "`] known not to be `", stringify!($prim), "::MAX` (bit-exact).")]
        pub type $nonmax = $nv<{ $prim::MAX.to_bits() }>;
        #[doc = concat!("A [`", stringify!($prim), "`] known not to be `", stringify!($prim), "::MIN` (bit-exact).")]
        pub type $nonmin = $nv<{ $prim::MIN.to_bits() }>;

        const _: () = {
            assert!(core::mem::size_of::<$nonmax>() == core::mem::size_of::<$prim>());
            assert!(core::mem::size_of::<Option<$nonmax>>() == core::mem::size_of::<$prim>());
        };
    };
}

niche_float!(NonValueF32, f32, u32, NonZeroU32, NonMaxF32, NonMinF32);
niche_float!(NonValueF64, f64, u64, NonZeroU64, NonMaxF64, NonMinF64);

// ============================ class-based family ============================

macro_rules! niche_float_class {
    (
        $ty:ident, $prim:ident, $bits:ident, $nonzero:ident,
        anchor = $anchor:expr, reject = $reject:ident, what = $what:literal
    ) => {
        #[doc = concat!("An [`", stringify!($prim), "`] guaranteed not to be ", $what, ".")]
        ///
        /// `Option<Self>` is niche-optimized to the size of the primitive.
        #[derive(Clone, Copy)]
        #[repr(transparent)]
        pub struct $ty(core::num::$nonzero);

        impl $ty {
            /// The niche anchor: a representative bit pattern from the forbidden
            /// class. Never stored, so it is free to serve as `Option::None`.
            const ANCHOR: $bits = $anchor;

            #[doc = concat!("Creates a value if it is not ", $what, ", otherwise `None`.")]
            #[inline]
            pub fn new(value: $prim) -> Option<Self> {
                if value.$reject() {
                    return None;
                }
                // The predicate guarantees `value.to_bits() != ANCHOR` (the
                // anchor is itself a member of the forbidden class), so the XOR
                // is never zero and `NonZero::new` always returns `Some`.
                core::num::$nonzero::new(value.to_bits() ^ Self::ANCHOR).map(Self)
            }

            #[doc = concat!("Creates a value without checking that it is not ", $what, ".")]
            ///
            /// # Safety
            #[doc = concat!("`value` must not be ", $what, ".")]
            #[inline]
            pub unsafe fn new_unchecked(value: $prim) -> Self {
                // SAFETY: caller guarantees the value is outside the forbidden
                // class, hence `value.to_bits() != ANCHOR`.
                Self(unsafe { core::num::$nonzero::new_unchecked(value.to_bits() ^ Self::ANCHOR) })
            }

            /// Returns the value as a primitive.
            #[inline]
            pub const fn get(&self) -> $prim {
                $prim::from_bits(self.0.get() ^ Self::ANCHOR)
            }
        }

        impl PartialEq for $ty {
            #[inline]
            fn eq(&self, other: &Self) -> bool {
                self.get() == other.get()
            }
        }
        impl From<$ty> for $prim {
            #[inline]
            fn from(value: $ty) -> Self {
                value.get()
            }
        }
        impl core::convert::TryFrom<$prim> for $ty {
            type Error = TryFromFloatError;
            #[inline]
            fn try_from(value: $prim) -> Result<Self, Self::Error> {
                Self::new(value).ok_or(TryFromFloatError(()))
            }
        }
        impl core::str::FromStr for $ty {
            type Err = ParseFloatError;
            #[inline]
            fn from_str(value: &str) -> Result<Self, Self::Err> {
                Self::new(<$prim as core::str::FromStr>::from_str(value)?)
                    .ok_or(ParseFloatError(()))
            }
        }
        impl core::fmt::Debug for $ty {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::Debug::fmt(&self.get(), f)
            }
        }
        impl core::fmt::Display for $ty {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::Display::fmt(&self.get(), f)
            }
        }
        impl core::fmt::LowerExp for $ty {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::LowerExp::fmt(&self.get(), f)
            }
        }
        impl core::fmt::UpperExp for $ty {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                core::fmt::UpperExp::fmt(&self.get(), f)
            }
        }

        #[cfg(feature = "serde")]
        impl serde::Serialize for $ty {
            fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
                self.get().serialize(serializer)
            }
        }
        #[cfg(feature = "serde")]
        impl<'de> serde::Deserialize<'de> for $ty {
            fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
                let value = <$prim as serde::Deserialize>::deserialize(deserializer)?;
                Self::new(value)
                    .ok_or_else(|| serde::de::Error::custom(concat!("value is ", $what)))
            }
        }

        const _: () = {
            assert!(core::mem::size_of::<$ty>() == core::mem::size_of::<$prim>());
            assert!(core::mem::size_of::<Option<$ty>>() == core::mem::size_of::<$prim>());
        };
    };
}

/// `PartialOrd` for a class-based float that may still hold `NaN` (so no total
/// order): comparison is by value and can be `None`.
macro_rules! impl_partial_ord {
    ($ty:ident) => {
        impl PartialOrd for $ty {
            #[inline]
            fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
                self.get().partial_cmp(&other.get())
            }
        }
    };
}

/// Adds total `Eq`/`Ord`/`Hash` (and a canonical `PartialOrd`) to a float type
/// that can never hold `NaN`. `-0.0` is normalized to `+0.0` in `Hash` to stay
/// consistent with `Eq` (`+0.0 == -0.0`), while `get()` round-trips the sign.
macro_rules! impl_total_ord {
    ($ty:ident, $prim:ident) => {
        impl PartialOrd for $ty {
            #[inline]
            fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
                Some(self.cmp(other))
            }
        }
        impl Eq for $ty {}
        impl Ord for $ty {
            #[inline]
            fn cmp(&self, other: &Self) -> core::cmp::Ordering {
                // SAFETY-of-logic: this type never holds NaN, so partial_cmp is total.
                self.get()
                    .partial_cmp(&other.get())
                    .expect("type invariant guarantees no NaN")
            }
        }
        impl core::hash::Hash for $ty {
            #[inline]
            fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
                let value = self.get();
                let normalized = if value == 0.0 { 0.0 } else { value };
                normalized.to_bits().hash(state);
            }
        }
    };
}

niche_float_class!(
    NonNanF32,
    f32,
    u32,
    NonZeroU32,
    anchor = 0x7FC0_0000,
    reject = is_nan,
    what = "`NaN`"
);
niche_float_class!(
    NonNanF64,
    f64,
    u64,
    NonZeroU64,
    anchor = 0x7FF8_0000_0000_0000,
    reject = is_nan,
    what = "`NaN`"
);
impl_total_ord!(NonNanF32, f32);
impl_total_ord!(NonNanF64, f64);

niche_float_class!(
    NonInfF32,
    f32,
    u32,
    NonZeroU32,
    anchor = 0x7F80_0000,
    reject = is_infinite,
    what = "infinite"
);
niche_float_class!(
    NonInfF64,
    f64,
    u64,
    NonZeroU64,
    anchor = 0x7FF0_0000_0000_0000,
    reject = is_infinite,
    what = "infinite"
);
impl_partial_ord!(NonInfF32);
impl_partial_ord!(NonInfF64);

#[cfg(test)]
mod tests {
    use super::*;
    use core::mem::size_of;

    #[test]
    fn bit_exact_rejection_and_roundtrip() {
        assert_eq!(NonMaxF32::new(1.5).unwrap().get(), 1.5);
        assert!(NonMaxF32::new(f32::MAX).is_none());
        assert!(NonMinF32::new(f32::MIN).is_none());
        assert_eq!(NonMaxF32::new(f32::INFINITY).unwrap().get(), f32::INFINITY);
    }

    #[test]
    fn signed_zero_is_bit_distinct() {
        // Forbid +0.0's bit pattern; -0.0 is a *different* pattern and allowed.
        type NoPosZero = NonValueF32<0x0000_0000>;
        assert!(NoPosZero::new(0.0).is_none());
        let neg = NoPosZero::new(-0.0).unwrap();
        assert!(neg.get().is_sign_negative());
        assert_eq!(neg.get(), 0.0); // value-equal to +0.0
    }

    #[test]
    fn nonnan_rejects_every_nan_but_keeps_infinity() {
        assert!(NonNanF32::new(f32::NAN).is_none());
        // a different (signaling-ish) NaN bit pattern is also rejected
        assert!(NonNanF32::new(f32::from_bits(0x7F80_0001)).is_none());
        assert!(NonNanF32::new(f32::from_bits(0xFFFF_FFFF)).is_none());
        // infinities are NOT NaN, so allowed
        assert_eq!(NonNanF32::new(f32::INFINITY).unwrap().get(), f32::INFINITY);
        assert_eq!(NonNanF32::new(-2.5).unwrap().get(), -2.5);
    }

    #[test]
    fn noninf_rejects_both_infinities_but_keeps_nan() {
        assert!(NonInfF32::new(f32::INFINITY).is_none());
        assert!(NonInfF32::new(f32::NEG_INFINITY).is_none());
        // NaN is not infinite, so allowed (and thus NonInf is NOT Eq/Ord)
        assert!(NonInfF64::new(f64::NAN).unwrap().get().is_nan());
        assert_eq!(NonInfF32::new(3.0).unwrap().get(), 3.0);
    }

    #[test]
    fn nonnan_is_totally_ordered_and_hashable() {
        use std::collections::{BTreeSet, HashSet};
        let a = NonNanF64::new(-1.0).unwrap();
        let b = NonNanF64::new(0.0).unwrap();
        let c = NonNanF64::new(2.5).unwrap();
        assert!(a < b && b < c);

        // usable as ordered / hashed keys
        let mut set: BTreeSet<NonNanF64> = BTreeSet::new();
        set.insert(c);
        set.insert(a);
        set.insert(b);
        let sorted: Vec<f64> = set.iter().map(|x| x.get()).collect();
        assert_eq!(sorted, vec![-1.0, 0.0, 2.5]);

        let mut hs: HashSet<NonNanF32> = HashSet::new();
        assert!(hs.insert(NonNanF32::new(0.0).unwrap()));
        // -0.0 is value-equal to +0.0 and must collide in the set
        assert!(!hs.insert(NonNanF32::new(-0.0).unwrap()));
    }

    #[test]
    fn nonnan_signed_zero_roundtrips_but_compares_equal() {
        let pos = NonNanF32::new(0.0).unwrap();
        let neg = NonNanF32::new(-0.0).unwrap();
        assert_eq!(pos, neg); // value equality
        assert_eq!(pos.cmp(&neg), core::cmp::Ordering::Equal);
        // but get() is lossless on the sign bit
        assert!(pos.get().is_sign_positive());
        assert!(neg.get().is_sign_negative());
    }

    #[test]
    fn sizes_niche_optimized() {
        assert_eq!(size_of::<Option<NonValueF32<7>>>(), size_of::<f32>());
        assert_eq!(size_of::<Option<NonMaxF64>>(), size_of::<f64>());
        assert_eq!(size_of::<Option<NonNanF32>>(), size_of::<f32>());
        assert_eq!(size_of::<Option<NonNanF64>>(), size_of::<f64>());
        assert_eq!(size_of::<Option<NonInfF32>>(), size_of::<f32>());
    }

    #[test]
    fn fmt_and_parse() {
        let v = NonNanF64::new(1.5).unwrap();
        assert_eq!(format!("{v}"), "1.5");
        assert_eq!(format!("{v:e}"), format!("{:e}", 1.5f64));
        let p: NonNanF32 = "2.5".parse().unwrap();
        assert_eq!(p.get(), 2.5);
        assert!("NaN".parse::<NonNanF32>().is_err());
    }

    #[test]
    #[cfg(feature = "serde")]
    fn serde_roundtrip() {
        let v = NonNanF64::new(1.25).unwrap();
        let bytes = bincode::serialize(&v).unwrap();
        let back: NonNanF64 = bincode::deserialize(&bytes).unwrap();
        assert_eq!(v, back);
    }
}