rust-lang / rust-lang/rust

Tracking Issue for NonMin and NonMax types.

Open
#151,435 9 comments 10 reactions 1 assignee View on GitHub

@moonheart08 is already working on this.

Since Jan 20, 2026.

C-tracking-issue S-tracking-unimplemented T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Feature gate: #![feature(nonmin_nonmax_types)]

This is a tracking issue for https://github.com/rust-lang/libs-team/issues/721

NonMin and NonMax are niche types, like NonZero, that provide niches at T::MIN and T::MAX respectively.
They are akin to NonZero in API, minus the individual aliases.

Public API

As this API is implemented for many distinct types, I'm opting to only show the impls for NonMin<isize> for demonstration purposes.
The surface area for NonMin and NonMax are identical and their behavior only differs in niche location.

// Constrained such that T is one of i8, i16, i32, i64, i128, or isize.
/// A value that is known to not be its minimum.
/// This enables some memory layout optimization. For example, `Option<NonMin<u32>>` is the same size as `u32`.
struct NonMin<T: NonMinPrimitive>;

// Constrained such that T is one of u8, u16, u32, u64, u128, or usize.
/// A value that is known to not be its maximum.
/// This enables some memory layout optimization. For example, `Option<NonMax<u32>>` is the same size as `u32`.
struct NonMax<T: NonMaxPrimitive>;

impl NonMin<isize> {
   const MIN: Self;
   const MAX: Self;
   const BITS: Self;
   
   pub const fn get(self) -> isize;
   pub const fn new(n: isize) -> Option<Self>;
   pub const unsafe fn new_unchecked(n: isize) -> Self;
   
   
   // Intent is to match the current NonZero surface area when sensible.
   // To avoid leaving an unreviewable spill of information, I will not list every method,
   // in favor of discussing them here verbally.
   // 
   // Input from https://github.com/rust-lang/libs-team/issues/721#issuecomment-3725065768
   // is to be accounted for, a lot of functions only ever shrink their input around zero
   // and will not produce the min or max value, making them obvious candidates to provide.
   //
   // The checked_ family of math functions (add, sub, mul, div) will exist, but
   // the overflowing_/wrapping_ family is not useful by definition, as they allow an operation to
   // wrap to the MIN or MAX of a type.
   // 
   // saturating_ is possible, but would need to be falliable as it can only saturate in 
   // one direction, or alternatively all three questionable families could be made to work 
   // by defining them as working within the valid space of the type.
   // 
   // The methods behind the nonzero_bitwise and nonzero_ops feature flags would also be sensible.
}
Steps / History

(Remember to update the S-tracking-* label when checking boxes.)

Maintenance remarks
Unresolved Questions
  • What action should be taken for overflowing_, wrapping_, and saturating_?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.