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>
impl NonMin<i8>
Sourcepub const unsafe fn new_unchecked(value: i8) -> Self
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.
Sourcepub fn from_ref(value: &i8) -> Option<&Self>
pub fn from_ref(value: &i8) -> Option<&Self>
Zero-cost borrowed proof (repr(transparent) reinterpret).
Sourcepub const fn div_nonzero(self, d: NonZero<i8>) -> i8
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.
Sourcepub const fn rem_nonzero(self, d: NonZero<i8>) -> i8
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>
impl NonMin<i16>
Sourcepub const unsafe fn new_unchecked(value: i16) -> Self
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.
Sourcepub fn from_ref(value: &i16) -> Option<&Self>
pub fn from_ref(value: &i16) -> Option<&Self>
Zero-cost borrowed proof (repr(transparent) reinterpret).
Sourcepub const fn div_nonzero(self, d: NonZero<i16>) -> i16
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.
Sourcepub const fn rem_nonzero(self, d: NonZero<i16>) -> i16
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>
impl NonMin<i32>
Sourcepub const fn new(value: i32) -> Option<Self>
pub const fn new(value: i32) -> Option<Self>
Some iff value != <$t>::MIN.
Examples found in repository?
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}Sourcepub const unsafe fn new_unchecked(value: i32) -> Self
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.
Sourcepub fn from_ref(value: &i32) -> Option<&Self>
pub fn from_ref(value: &i32) -> Option<&Self>
Zero-cost borrowed proof (repr(transparent) reinterpret).
Sourcepub const fn neg(self) -> i32
pub const fn neg(self) -> i32
Total negation — MIN (the sole overflow) is excluded.
Examples found in repository?
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}Sourcepub const fn abs(self) -> i32
pub const fn abs(self) -> i32
Total abs — MIN (the sole overflow) is excluded.
Examples found in repository?
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}Sourcepub const fn div_nonzero(self, d: NonZero<i32>) -> i32
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?
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}Sourcepub const fn rem_nonzero(self, d: NonZero<i32>) -> i32
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>
impl NonMin<i64>
Sourcepub const unsafe fn new_unchecked(value: i64) -> Self
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.
Sourcepub fn from_ref(value: &i64) -> Option<&Self>
pub fn from_ref(value: &i64) -> Option<&Self>
Zero-cost borrowed proof (repr(transparent) reinterpret).
Sourcepub const fn div_nonzero(self, d: NonZero<i64>) -> i64
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.
Sourcepub const fn rem_nonzero(self, d: NonZero<i64>) -> i64
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>
impl NonMin<i128>
Sourcepub const unsafe fn new_unchecked(value: i128) -> Self
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.
Sourcepub fn from_ref(value: &i128) -> Option<&Self>
pub fn from_ref(value: &i128) -> Option<&Self>
Zero-cost borrowed proof (repr(transparent) reinterpret).
Sourcepub const fn div_nonzero(self, d: NonZero<i128>) -> i128
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.
Sourcepub const fn rem_nonzero(self, d: NonZero<i128>) -> i128
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>
impl NonMin<isize>
Sourcepub const unsafe fn new_unchecked(value: isize) -> Self
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.
Sourcepub fn from_ref(value: &isize) -> Option<&Self>
pub fn from_ref(value: &isize) -> Option<&Self>
Zero-cost borrowed proof (repr(transparent) reinterpret).
Sourcepub const fn div_nonzero(self, d: NonZero<isize>) -> isize
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.
Sourcepub const fn rem_nonzero(self, d: NonZero<isize>) -> isize
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§
impl<T: Copy> Copy for NonMin<T>
impl<T: Eq> Eq for NonMin<T>
impl<T: PartialEq> StructuralPartialEq for NonMin<T>
Source§impl TryFrom<i8> for NonMin<i8>
Checked construction by value; mirrors NonMin::new.
impl TryFrom<i8> for NonMin<i8>
Checked construction by value; mirrors NonMin::new.
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<i16> for NonMin<i16>
Checked construction by value; mirrors NonMin::new.
impl TryFrom<i16> for NonMin<i16>
Checked construction by value; mirrors NonMin::new.
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<i32> for NonMin<i32>
Checked construction by value; mirrors NonMin::new.
impl TryFrom<i32> for NonMin<i32>
Checked construction by value; mirrors NonMin::new.
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<i64> for NonMin<i64>
Checked construction by value; mirrors NonMin::new.
impl TryFrom<i64> for NonMin<i64>
Checked construction by value; mirrors NonMin::new.