Skip to main content

niche_value/
lib.rs

1//! `niche_value` provides numeric types that are known **not** to hold one
2//! chosen value, so that `Option<T>` (and other niches) are no larger than `T`.
3//!
4//! It generalizes [`nonmax`](https://docs.rs/nonmax): where `nonmax` forbids the
5//! *maximum* value, this crate forbids an *arbitrary* value chosen with a const
6//! generic — [`NonValueU8<N>`], [`NonValueI32<N>`], … — and additionally
7//! supports floats ([`NonValueF32<BITS>`], [`NonValueF64<BITS>`]) plus
8//! semantic-class float types ([`NonNanF32`], [`NonInfF32`], …).
9//!
10//! # Mechanism
11//!
12//! Every type stores a [`core::num::NonZero`] of `value_bits ^ forbidden_bits`.
13//! Because the stored value can never equal the forbidden bit pattern, the XOR
14//! is never `0`, so the `NonZero` niche survives and `Option<T>` reuses it.
15//! This is exactly `nonmax`'s trick, with the forbidden value made generic.
16//!
17//! ```
18//! use niche_value::{NonValueU8, NonMaxU8, NonNanF32};
19//! use core::mem::size_of;
20//!
21//! // Forbid an arbitrary value:
22//! let v = NonValueU8::<7>::new(3).unwrap();
23//! assert_eq!(v.get(), 3);
24//! assert!(NonValueU8::<7>::new(7).is_none());
25//!
26//! // nonmax-compatible specialization:
27//! assert!(NonMaxU8::new(u8::MAX).is_none());
28//!
29//! // Niche optimization holds everywhere:
30//! assert_eq!(size_of::<Option<NonValueU8<7>>>(), size_of::<u8>());
31//! assert_eq!(size_of::<Option<NonNanF32>>(), size_of::<f32>());
32//! ```
33//!
34//! # Float semantics
35//!
36//! Bit-exact float types ([`NonValueF32<BITS>`]) reject by **bit pattern**; the
37//! class-based types reject a whole **semantic class** by value — `is_nan()`,
38//! `== 0.0`, `is_infinite()`, `is_subnormal()` — while anchoring their niche on
39//! one representative pattern *drawn from that class*, so the anchor is itself
40//! rejected and never constructible. A value predicate is sound only when the
41//! anchor compares equal to itself: `+0.0 == +0.0` lets [`NonZeroF32`] reject
42//! with `== 0.0`, whereas `NaN != NaN` forces [`NonNanF32`] to reject with
43//! `is_nan()` (a `== NaN` check would let the anchor slip through and form an
44//! unsound `NonZero(0)`).
45//!
46//! Consequences: `+0.0` and `-0.0` are distinct patterns, so
47//! [`NonValueF32<BITS>`] forbids exactly one of them, whereas [`NonZeroF32`]
48//! rejects zero as a *class* (both `+0.0` and `-0.0`). Because [`NonNanF32`] and
49//! [`FiniteF32`] can never hold `NaN`, they are the float types that implement
50//! total [`Ord`]/[`Eq`]/[`Hash`] — a niche-optimized `NotNan` / `Finite`.
51//!
52//! # Features
53//!
54//! * `std` (default): implements [`std::error::Error`] for the error types.
55//!   Disable for `#![no_std]`.
56//! * `serde`: `Serialize`/`Deserialize` for every type.
57
58#![cfg_attr(not(feature = "std"), no_std)]
59#![warn(missing_docs)]
60
61mod error;
62mod float;
63mod int;
64
65pub use error::{ParseFloatError, ParseIntError, TryFromFloatError, TryFromIntError};
66pub use float::*;
67pub use int::*;