Expand description
niche_value provides numeric types that are known not to hold one
chosen value, so that Option<T> (and other niches) are no larger than T.
It generalizes nonmax: where nonmax forbids the
maximum value, this crate forbids an arbitrary value chosen with a const
generic — NonValueU8<N>, NonValueI32<N>, … — and additionally
supports floats (NonValueF32<BITS>, NonValueF64<BITS>) plus
semantic-class float types (NonNanF32, NonInfF32, …).
§Mechanism
Every type stores a core::num::NonZero of value_bits ^ forbidden_bits.
Because the stored value can never equal the forbidden bit pattern, the XOR
is never 0, so the NonZero niche survives and Option<T> reuses it.
This is exactly nonmax’s trick, with the forbidden value made generic.
use niche_value::{NonValueU8, NonMaxU8, NonNanF32};
use core::mem::size_of;
// Forbid an arbitrary value:
let v = NonValueU8::<7>::new(3).unwrap();
assert_eq!(v.get(), 3);
assert!(NonValueU8::<7>::new(7).is_none());
// nonmax-compatible specialization:
assert!(NonMaxU8::new(u8::MAX).is_none());
// Niche optimization holds everywhere:
assert_eq!(size_of::<Option<NonValueU8<7>>>(), size_of::<u8>());
assert_eq!(size_of::<Option<NonNanF32>>(), size_of::<f32>());§Float semantics
Bit-exact float types (NonValueF32<BITS>) reject by bit pattern; the
class-based types reject a whole semantic class by value — is_nan(),
== 0.0, is_infinite(), is_subnormal() — while anchoring their niche on
one representative pattern drawn from that class, so the anchor is itself
rejected and never constructible. A value predicate is sound only when the
anchor compares equal to itself: +0.0 == +0.0 lets NonZeroF32 reject
with == 0.0, whereas NaN != NaN forces NonNanF32 to reject with
is_nan() (a == NaN check would let the anchor slip through and form an
unsound NonZero(0)).
Consequences: +0.0 and -0.0 are distinct patterns, so
NonValueF32<BITS> forbids exactly one of them, whereas NonZeroF32
rejects zero as a class (both +0.0 and -0.0). Because NonNanF32 and
FiniteF32 can never hold NaN, they are the float types that implement
total Ord/Eq/Hash — a niche-optimized NotNan / Finite.
§Features
std(default): implementsstd::error::Errorfor the error types. Disable for#![no_std].serde:Serialize/Deserializefor every type.
Structs§
- Finite
F32 - An
f32guaranteed not to be non-finite (NaNor infinite). - Finite
F64 - An
f64guaranteed not to be non-finite (NaNor infinite). - NonInf
F32 - An
f32guaranteed not to be infinite. - NonInf
F64 - An
f64guaranteed not to be infinite. - NonNan
F32 - An
f32guaranteed not to beNaN. - NonNan
F64 - An
f64guaranteed not to beNaN. - NonSubnormal
F32 - An
f32guaranteed not to be subnormal. - NonSubnormal
F64 - An
f64guaranteed not to be subnormal. - NonValue
F32 - An
f32whose bit pattern is known not to equalBITS. - NonValue
F64 - An
f64whose bit pattern is known not to equalBITS. - NonValue
I8 - An
i8that is known not to equal the const valueN. - NonValue
I16 - An
i16that is known not to equal the const valueN. - NonValue
I32 - An
i32that is known not to equal the const valueN. - NonValue
I64 - An
i64that is known not to equal the const valueN. - NonValue
I128 - An
i128that is known not to equal the const valueN. - NonValue
Isize - An
isizethat is known not to equal the const valueN. - NonValue
U8 - An
u8that is known not to equal the const valueN. - NonValue
U16 - An
u16that is known not to equal the const valueN. - NonValue
U32 - An
u32that is known not to equal the const valueN. - NonValue
U64 - An
u64that is known not to equal the const valueN. - NonValue
U128 - An
u128that is known not to equal the const valueN. - NonValue
Usize - An
usizethat is known not to equal the const valueN. - NonZero
F32 - An
f32guaranteed not to be zero. - NonZero
F64 - An
f64guaranteed not to be zero. - Parse
Float Error - Error returned when a float string cannot be parsed into a niche float
(mirrors
core::num::ParseFloatErrornaming). - Parse
IntError - Error returned when an integer string cannot be parsed into a niche integer
(mirrors
core::num::ParseIntError). - TryFrom
Float Error - Error returned when a checked
f32/f64conversion fails (the value was the forbidden bit pattern, orNaN/±inffor the class-based types). - TryFrom
IntError - Error returned when a checked integral conversion fails (mirrors
core::num::TryFromIntError).
Type Aliases§
- NonMax
F32 - A
f32known not to bef32::MAX(bit-exact). - NonMax
F64 - A
f64known not to bef64::MAX(bit-exact). - NonMax
I8 - A
i8that is known not to equal its maximum. Drop-in fornonmax::NonMaxI8. - NonMax
I16 - A
i16that is known not to equal its maximum. Drop-in fornonmax::NonMaxI16. - NonMax
I32 - A
i32that is known not to equal its maximum. Drop-in fornonmax::NonMaxI32. - NonMax
I64 - A
i64that is known not to equal its maximum. Drop-in fornonmax::NonMaxI64. - NonMax
I128 - A
i128that is known not to equal its maximum. Drop-in fornonmax::NonMaxI128. - NonMax
Isize - A
isizethat is known not to equal its maximum. Drop-in fornonmax::NonMaxIsize. - NonMax
U8 - A
u8that is known not to equal its maximum. Drop-in fornonmax::NonMaxU8. - NonMax
U16 - A
u16that is known not to equal its maximum. Drop-in fornonmax::NonMaxU16. - NonMax
U32 - A
u32that is known not to equal its maximum. Drop-in fornonmax::NonMaxU32. - NonMax
U64 - A
u64that is known not to equal its maximum. Drop-in fornonmax::NonMaxU64. - NonMax
U128 - A
u128that is known not to equal its maximum. Drop-in fornonmax::NonMaxU128. - NonMax
Usize - A
usizethat is known not to equal its maximum. Drop-in fornonmax::NonMaxUsize. - NonMin
F32 - A
f32known not to bef32::MIN(bit-exact). - NonMin
F64 - A
f64known not to bef64::MIN(bit-exact). - NonMin
I8 - A
i8that is known not to equal its minimum. - NonMin
I16 - A
i16that is known not to equal its minimum. - NonMin
I32 - A
i32that is known not to equal its minimum. - NonMin
I64 - A
i64that is known not to equal its minimum. - NonMin
I128 - A
i128that is known not to equal its minimum. - NonMin
Isize - A
isizethat is known not to equal its minimum. - NonMin
U8 - A
u8that is known not to equal its minimum. - NonMin
U16 - A
u16that is known not to equal its minimum. - NonMin
U32 - A
u32that is known not to equal its minimum. - NonMin
U64 - A
u64that is known not to equal its minimum. - NonMin
U128 - A
u128that is known not to equal its minimum. - NonMin
Usize - A
usizethat is known not to equal its minimum.