Skip to main content

NonMin

Struct NonMin 

Source
pub struct NonMin<T>(/* private fields */);
Expand description

Proof that a signed value is not T::MIN.

neg/abs overflow only at MIN, and the only signed-division overflow is MIN / -1. So with the dividend proven != MIN, neg / abs and div_nonzero / rem_nonzero by any non-zero divisor are total — the co-proof the unsigned DivNonZero doesn’t need. Positive / NonNegative narrow here for free (into_nonmin).

use const_num_traits::NonMin;
use core::num::NonZero;

let a = NonMin::<i32>::new(-7).unwrap();
assert_eq!(a.abs(), 7);
assert_eq!(a.neg(), 7);
let d = NonZero::new(2).unwrap();
assert_eq!(a.div_nonzero(d), -3); // -7 / 2, truncating
assert_eq!(a.rem_nonzero(d), -1); // -7 % 2
assert!(NonMin::<i32>::new(i32::MIN).is_none());

Implementations§

Source§

impl NonMin<i8>

Source

pub fn new_ct(value: i8) -> CtOption<NonMin<i8>>

Available on crate feature ct only.

Constant-time new: None-masked when value == MIN.

Source§

impl NonMin<i16>

Source

pub fn new_ct(value: i16) -> CtOption<NonMin<i16>>

Available on crate feature ct only.

Constant-time new: None-masked when value == MIN.

Source§

impl NonMin<i32>

Source

pub fn new_ct(value: i32) -> CtOption<NonMin<i32>>

Available on crate feature ct only.

Constant-time new: None-masked when value == MIN.

Source§

impl NonMin<i64>

Source

pub fn new_ct(value: i64) -> CtOption<NonMin<i64>>

Available on crate feature ct only.

Constant-time new: None-masked when value == MIN.

Source§

impl NonMin<i128>

Source

pub fn new_ct(value: i128) -> CtOption<NonMin<i128>>

Available on crate feature ct only.

Constant-time new: None-masked when value == MIN.

Source§

impl NonMin<isize>

Source

pub fn new_ct(value: isize) -> CtOption<NonMin<isize>>

Available on crate feature ct only.

Constant-time new: None-masked when value == MIN.

Source§

impl NonMin<i8>

Source

pub const fn new(value: i8) -> Option<Self>

Some iff value != <$t>::MIN.

Source

pub const unsafe fn new_unchecked(value: i8) -> Self

§Safety

value must not equal <$t>::MIN (i.e. value != <$t>::MIN) — the invariant consumed by neg/abs/div_nonzero/rem_nonzero.

Source

pub const fn get(self) -> i8

The proven value.

Source

pub fn from_ref(value: &i8) -> Option<&Self>

Zero-cost borrowed proof (repr(transparent) reinterpret).

Source

pub const fn neg(self) -> i8

Total negation — MIN (the sole overflow) is excluded.

Source

pub const fn abs(self) -> i8

Total absMIN (the sole overflow) is excluded.

Source

pub const fn div_nonzero(self, d: NonZero<i8>) -> i8

Total signed division by a non-zero divisor: the dividend is proven != MIN, so MIN / -1 (the only overflow) can’t occur, and the divisor’s niche rules out divide-by-zero.

Source

pub const fn rem_nonzero(self, d: NonZero<i8>) -> i8

Total signed remainder by a non-zero divisor (same reasoning as div_nonzero; MIN % -1 is excluded).

Source§

impl NonMin<i16>

Source

pub const fn new(value: i16) -> Option<Self>

Some iff value != <$t>::MIN.

Source

pub const unsafe fn new_unchecked(value: i16) -> Self

§Safety

value must not equal <$t>::MIN (i.e. value != <$t>::MIN) — the invariant consumed by neg/abs/div_nonzero/rem_nonzero.

Source

pub const fn get(self) -> i16

The proven value.

Source

pub fn from_ref(value: &i16) -> Option<&Self>

Zero-cost borrowed proof (repr(transparent) reinterpret).

Source

pub const fn neg(self) -> i16

Total negation — MIN (the sole overflow) is excluded.

Source

pub const fn abs(self) -> i16

Total absMIN (the sole overflow) is excluded.

Source

pub const fn div_nonzero(self, d: NonZero<i16>) -> i16

Total signed division by a non-zero divisor: the dividend is proven != MIN, so MIN / -1 (the only overflow) can’t occur, and the divisor’s niche rules out divide-by-zero.

Source

pub const fn rem_nonzero(self, d: NonZero<i16>) -> i16

Total signed remainder by a non-zero divisor (same reasoning as div_nonzero; MIN % -1 is excluded).

Source§

impl NonMin<i32>

Source

pub const fn new(value: i32) -> Option<Self>

Some iff value != <$t>::MIN.

Examples found in repository?
examples/typestates.rs (line 76)
75fn nonmin() {
76    let a = NonMin::<i32>::new(-7).expect("-7 != MIN");
77    assert_eq!(a.abs(), 7); // no overflow branch
78    assert_eq!(a.neg(), 7);
79
80    // signed division by NonZero: MIN/-1 is impossible by construction
81    let d = core::num::NonZero::new(2).unwrap();
82    assert_eq!(a.div_nonzero(d), -7 / 2);
83
84    // Positive/NonNegative narrow into NonMin for free
85    let q = Positive::<i32>::new(9).unwrap().into_nonmin();
86    assert_eq!(q.abs(), 9);
87
88    assert!(NonMin::<i32>::new(i32::MIN).is_none());
89}
Source

pub const unsafe fn new_unchecked(value: i32) -> Self

§Safety

value must not equal <$t>::MIN (i.e. value != <$t>::MIN) — the invariant consumed by neg/abs/div_nonzero/rem_nonzero.

Source

pub const fn get(self) -> i32

The proven value.

Source

pub fn from_ref(value: &i32) -> Option<&Self>

Zero-cost borrowed proof (repr(transparent) reinterpret).

Source

pub const fn neg(self) -> i32

Total negation — MIN (the sole overflow) is excluded.

Examples found in repository?
examples/typestates.rs (line 78)
75fn nonmin() {
76    let a = NonMin::<i32>::new(-7).expect("-7 != MIN");
77    assert_eq!(a.abs(), 7); // no overflow branch
78    assert_eq!(a.neg(), 7);
79
80    // signed division by NonZero: MIN/-1 is impossible by construction
81    let d = core::num::NonZero::new(2).unwrap();
82    assert_eq!(a.div_nonzero(d), -7 / 2);
83
84    // Positive/NonNegative narrow into NonMin for free
85    let q = Positive::<i32>::new(9).unwrap().into_nonmin();
86    assert_eq!(q.abs(), 9);
87
88    assert!(NonMin::<i32>::new(i32::MIN).is_none());
89}
Source

pub const fn abs(self) -> i32

Total absMIN (the sole overflow) is excluded.

Examples found in repository?
examples/typestates.rs (line 77)
75fn nonmin() {
76    let a = NonMin::<i32>::new(-7).expect("-7 != MIN");
77    assert_eq!(a.abs(), 7); // no overflow branch
78    assert_eq!(a.neg(), 7);
79
80    // signed division by NonZero: MIN/-1 is impossible by construction
81    let d = core::num::NonZero::new(2).unwrap();
82    assert_eq!(a.div_nonzero(d), -7 / 2);
83
84    // Positive/NonNegative narrow into NonMin for free
85    let q = Positive::<i32>::new(9).unwrap().into_nonmin();
86    assert_eq!(q.abs(), 9);
87
88    assert!(NonMin::<i32>::new(i32::MIN).is_none());
89}
Source

pub const fn div_nonzero(self, d: NonZero<i32>) -> i32

Total signed division by a non-zero divisor: the dividend is proven != MIN, so MIN / -1 (the only overflow) can’t occur, and the divisor’s niche rules out divide-by-zero.

Examples found in repository?
examples/typestates.rs (line 82)
75fn nonmin() {
76    let a = NonMin::<i32>::new(-7).expect("-7 != MIN");
77    assert_eq!(a.abs(), 7); // no overflow branch
78    assert_eq!(a.neg(), 7);
79
80    // signed division by NonZero: MIN/-1 is impossible by construction
81    let d = core::num::NonZero::new(2).unwrap();
82    assert_eq!(a.div_nonzero(d), -7 / 2);
83
84    // Positive/NonNegative narrow into NonMin for free
85    let q = Positive::<i32>::new(9).unwrap().into_nonmin();
86    assert_eq!(q.abs(), 9);
87
88    assert!(NonMin::<i32>::new(i32::MIN).is_none());
89}
Source

pub const fn rem_nonzero(self, d: NonZero<i32>) -> i32

Total signed remainder by a non-zero divisor (same reasoning as div_nonzero; MIN % -1 is excluded).

Source§

impl NonMin<i64>

Source

pub const fn new(value: i64) -> Option<Self>

Some iff value != <$t>::MIN.

Source

pub const unsafe fn new_unchecked(value: i64) -> Self

§Safety

value must not equal <$t>::MIN (i.e. value != <$t>::MIN) — the invariant consumed by neg/abs/div_nonzero/rem_nonzero.

Source

pub const fn get(self) -> i64

The proven value.

Source

pub fn from_ref(value: &i64) -> Option<&Self>

Zero-cost borrowed proof (repr(transparent) reinterpret).

Source

pub const fn neg(self) -> i64

Total negation — MIN (the sole overflow) is excluded.

Source

pub const fn abs(self) -> i64

Total absMIN (the sole overflow) is excluded.

Source

pub const fn div_nonzero(self, d: NonZero<i64>) -> i64

Total signed division by a non-zero divisor: the dividend is proven != MIN, so MIN / -1 (the only overflow) can’t occur, and the divisor’s niche rules out divide-by-zero.

Source

pub const fn rem_nonzero(self, d: NonZero<i64>) -> i64

Total signed remainder by a non-zero divisor (same reasoning as div_nonzero; MIN % -1 is excluded).

Source§

impl NonMin<i128>

Source

pub const fn new(value: i128) -> Option<Self>

Some iff value != <$t>::MIN.

Source

pub const unsafe fn new_unchecked(value: i128) -> Self

§Safety

value must not equal <$t>::MIN (i.e. value != <$t>::MIN) — the invariant consumed by neg/abs/div_nonzero/rem_nonzero.

Source

pub const fn get(self) -> i128

The proven value.

Source

pub fn from_ref(value: &i128) -> Option<&Self>

Zero-cost borrowed proof (repr(transparent) reinterpret).

Source

pub const fn neg(self) -> i128

Total negation — MIN (the sole overflow) is excluded.

Source

pub const fn abs(self) -> i128

Total absMIN (the sole overflow) is excluded.

Source

pub const fn div_nonzero(self, d: NonZero<i128>) -> i128

Total signed division by a non-zero divisor: the dividend is proven != MIN, so MIN / -1 (the only overflow) can’t occur, and the divisor’s niche rules out divide-by-zero.

Source

pub const fn rem_nonzero(self, d: NonZero<i128>) -> i128

Total signed remainder by a non-zero divisor (same reasoning as div_nonzero; MIN % -1 is excluded).

Source§

impl NonMin<isize>

Source

pub const fn new(value: isize) -> Option<Self>

Some iff value != <$t>::MIN.

Source

pub const unsafe fn new_unchecked(value: isize) -> Self

§Safety

value must not equal <$t>::MIN (i.e. value != <$t>::MIN) — the invariant consumed by neg/abs/div_nonzero/rem_nonzero.

Source

pub const fn get(self) -> isize

The proven value.

Source

pub fn from_ref(value: &isize) -> Option<&Self>

Zero-cost borrowed proof (repr(transparent) reinterpret).

Source

pub const fn neg(self) -> isize

Total negation — MIN (the sole overflow) is excluded.

Source

pub const fn abs(self) -> isize

Total absMIN (the sole overflow) is excluded.

Source

pub const fn div_nonzero(self, d: NonZero<isize>) -> isize

Total signed division by a non-zero divisor: the dividend is proven != MIN, so MIN / -1 (the only overflow) can’t occur, and the divisor’s niche rules out divide-by-zero.

Source

pub const fn rem_nonzero(self, d: NonZero<isize>) -> isize

Total signed remainder by a non-zero divisor (same reasoning as div_nonzero; MIN % -1 is excluded).

Trait Implementations§

Source§

impl<T: Clone> Clone for NonMin<T>

Source§

fn clone(&self) -> NonMin<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Copy> Copy for NonMin<T>

Source§

impl<T: Debug> Debug for NonMin<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Eq> Eq for NonMin<T>

Source§

impl<T: PartialEq> PartialEq for NonMin<T>

Source§

fn eq(&self, other: &NonMin<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialEq> StructuralPartialEq for NonMin<T>

Source§

impl TryFrom<i8> for NonMin<i8>

Checked construction by value; mirrors NonMin::new.

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i8) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<i16> for NonMin<i16>

Checked construction by value; mirrors NonMin::new.

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i16) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<i32> for NonMin<i32>

Checked construction by value; mirrors NonMin::new.

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i32) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<i64> for NonMin<i64>

Checked construction by value; mirrors NonMin::new.

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i64) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<i128> for NonMin<i128>

Checked construction by value; mirrors NonMin::new.

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i128) -> Result<Self, TypestateError>

Performs the conversion.
Source§

impl TryFrom<isize> for NonMin<isize>

Checked construction by value; mirrors NonMin::new.

Source§

type Error = TypestateError

The type returned in the event of a conversion error.
Source§

fn try_from(value: isize) -> Result<Self, TypestateError>

Performs the conversion.

Auto Trait Implementations§

§

impl<T> Freeze for NonMin<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for NonMin<T>
where T: RefUnwindSafe,

§

impl<T> Send for NonMin<T>
where T: Send,

§

impl<T> Sync for NonMin<T>
where T: Sync,

§

impl<T> Unpin for NonMin<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for NonMin<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for NonMin<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.