Skip to main content

Crate niche_value

Crate niche_value 

Source
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): implements std::error::Error for the error types. Disable for #![no_std].
  • serde: Serialize/Deserialize for every type.

Structs§

FiniteF32
An f32 guaranteed not to be non-finite (NaN or infinite).
FiniteF64
An f64 guaranteed not to be non-finite (NaN or infinite).
NonInfF32
An f32 guaranteed not to be infinite.
NonInfF64
An f64 guaranteed not to be infinite.
NonNanF32
An f32 guaranteed not to be NaN.
NonNanF64
An f64 guaranteed not to be NaN.
NonSubnormalF32
An f32 guaranteed not to be subnormal.
NonSubnormalF64
An f64 guaranteed not to be subnormal.
NonValueF32
An f32 whose bit pattern is known not to equal BITS.
NonValueF64
An f64 whose bit pattern is known not to equal BITS.
NonValueI8
An i8 that is known not to equal the const value N.
NonValueI16
An i16 that is known not to equal the const value N.
NonValueI32
An i32 that is known not to equal the const value N.
NonValueI64
An i64 that is known not to equal the const value N.
NonValueI128
An i128 that is known not to equal the const value N.
NonValueIsize
An isize that is known not to equal the const value N.
NonValueU8
An u8 that is known not to equal the const value N.
NonValueU16
An u16 that is known not to equal the const value N.
NonValueU32
An u32 that is known not to equal the const value N.
NonValueU64
An u64 that is known not to equal the const value N.
NonValueU128
An u128 that is known not to equal the const value N.
NonValueUsize
An usize that is known not to equal the const value N.
NonZeroF32
An f32 guaranteed not to be zero.
NonZeroF64
An f64 guaranteed not to be zero.
ParseFloatError
Error returned when a float string cannot be parsed into a niche float (mirrors core::num::ParseFloatError naming).
ParseIntError
Error returned when an integer string cannot be parsed into a niche integer (mirrors core::num::ParseIntError).
TryFromFloatError
Error returned when a checked f32/f64 conversion fails (the value was the forbidden bit pattern, or NaN/±inf for the class-based types).
TryFromIntError
Error returned when a checked integral conversion fails (mirrors core::num::TryFromIntError).

Type Aliases§

NonMaxF32
A f32 known not to be f32::MAX (bit-exact).
NonMaxF64
A f64 known not to be f64::MAX (bit-exact).
NonMaxI8
A i8 that is known not to equal its maximum. Drop-in for nonmax::NonMaxI8.
NonMaxI16
A i16 that is known not to equal its maximum. Drop-in for nonmax::NonMaxI16.
NonMaxI32
A i32 that is known not to equal its maximum. Drop-in for nonmax::NonMaxI32.
NonMaxI64
A i64 that is known not to equal its maximum. Drop-in for nonmax::NonMaxI64.
NonMaxI128
A i128 that is known not to equal its maximum. Drop-in for nonmax::NonMaxI128.
NonMaxIsize
A isize that is known not to equal its maximum. Drop-in for nonmax::NonMaxIsize.
NonMaxU8
A u8 that is known not to equal its maximum. Drop-in for nonmax::NonMaxU8.
NonMaxU16
A u16 that is known not to equal its maximum. Drop-in for nonmax::NonMaxU16.
NonMaxU32
A u32 that is known not to equal its maximum. Drop-in for nonmax::NonMaxU32.
NonMaxU64
A u64 that is known not to equal its maximum. Drop-in for nonmax::NonMaxU64.
NonMaxU128
A u128 that is known not to equal its maximum. Drop-in for nonmax::NonMaxU128.
NonMaxUsize
A usize that is known not to equal its maximum. Drop-in for nonmax::NonMaxUsize.
NonMinF32
A f32 known not to be f32::MIN (bit-exact).
NonMinF64
A f64 known not to be f64::MIN (bit-exact).
NonMinI8
A i8 that is known not to equal its minimum.
NonMinI16
A i16 that is known not to equal its minimum.
NonMinI32
A i32 that is known not to equal its minimum.
NonMinI64
A i64 that is known not to equal its minimum.
NonMinI128
A i128 that is known not to equal its minimum.
NonMinIsize
A isize that is known not to equal its minimum.
NonMinU8
A u8 that is known not to equal its minimum.
NonMinU16
A u16 that is known not to equal its minimum.
NonMinU32
A u32 that is known not to equal its minimum.
NonMinU64
A u64 that is known not to equal its minimum.
NonMinU128
A u128 that is known not to equal its minimum.
NonMinUsize
A usize that is known not to equal its minimum.