pub struct Positive<T>(/* private fields */);Expand description
Proof that a signed value is > 0.
Implementations§
Source§impl Positive<i8>
impl Positive<i8>
Sourcepub const unsafe fn new_unchecked(value: i8) -> Self
pub const unsafe fn new_unchecked(value: i8) -> Self
§Safety
value must be > 0.
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 to_unsigned(self) -> u8
pub const fn to_unsigned(self) -> u8
Infallible cast to the unsigned counterpart.
Sourcepub const fn into_nonnegative(self) -> NonNegative<i8>
pub const fn into_nonnegative(self) -> NonNegative<i8>
Positive ⊂ NonNegative.
Sourcepub const fn into_nonzero(self) -> NonZero<i8>
pub const fn into_nonzero(self) -> NonZero<i8>
Positive ⊂ NonZero — narrows to core::num::NonZero with no
recheck, so a Positive divisor can feed DivNonZero directly.
Sourcepub const fn into_nonmin(self) -> NonMin<i8>
pub const fn into_nonmin(self) -> NonMin<i8>
Positive ⊂ NonMin (> 0, so != MIN).
Source§impl Positive<i16>
impl Positive<i16>
Sourcepub const unsafe fn new_unchecked(value: i16) -> Self
pub const unsafe fn new_unchecked(value: i16) -> Self
§Safety
value must be > 0.
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 to_unsigned(self) -> u16
pub const fn to_unsigned(self) -> u16
Infallible cast to the unsigned counterpart.
Sourcepub const fn into_nonnegative(self) -> NonNegative<i16>
pub const fn into_nonnegative(self) -> NonNegative<i16>
Positive ⊂ NonNegative.
Sourcepub const fn into_nonzero(self) -> NonZero<i16>
pub const fn into_nonzero(self) -> NonZero<i16>
Positive ⊂ NonZero — narrows to core::num::NonZero with no
recheck, so a Positive divisor can feed DivNonZero directly.
Sourcepub const fn into_nonmin(self) -> NonMin<i16>
pub const fn into_nonmin(self) -> NonMin<i16>
Positive ⊂ NonMin (> 0, so != MIN).
Source§impl Positive<i32>
impl Positive<i32>
Sourcepub const fn new(value: i32) -> Option<Self>
pub const fn new(value: i32) -> Option<Self>
Some iff value > 0.
Examples found in repository?
62fn signed_range() {
63 let nn = NonNegative::<i32>::new(81).expect("81 >= 0");
64 let u: u32 = nn.to_unsigned(); // infallible, bit-preserving
65 assert_eq!(u, 81);
66 assert_eq!(nn.isqrt(), 9); // total: input is known non-negative
67
68 // a positive value narrows straight into a NonZero divisor
69 let nz = Positive::<i32>::new(7).expect("7 > 0").into_nonzero();
70 assert_eq!(nz.get(), 7);
71}
72
73/// `NonMin`: a signed value proven `!= MIN` has total `abs`/`neg` and total
74/// signed division — the `MIN.abs()` and `MIN / -1` overflow cases are gone.
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 be > 0.
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 to_unsigned(self) -> u32
pub const fn to_unsigned(self) -> u32
Infallible cast to the unsigned counterpart.
Sourcepub const fn into_nonnegative(self) -> NonNegative<i32>
pub const fn into_nonnegative(self) -> NonNegative<i32>
Positive ⊂ NonNegative.
Sourcepub const fn into_nonzero(self) -> NonZero<i32>
pub const fn into_nonzero(self) -> NonZero<i32>
Positive ⊂ NonZero — narrows to core::num::NonZero with no
recheck, so a Positive divisor can feed DivNonZero directly.
Sourcepub const fn into_nonmin(self) -> NonMin<i32>
pub const fn into_nonmin(self) -> NonMin<i32>
Positive ⊂ NonMin (> 0, so != 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}Source§impl Positive<i64>
impl Positive<i64>
Sourcepub const unsafe fn new_unchecked(value: i64) -> Self
pub const unsafe fn new_unchecked(value: i64) -> Self
§Safety
value must be > 0.
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 to_unsigned(self) -> u64
pub const fn to_unsigned(self) -> u64
Infallible cast to the unsigned counterpart.
Sourcepub const fn into_nonnegative(self) -> NonNegative<i64>
pub const fn into_nonnegative(self) -> NonNegative<i64>
Positive ⊂ NonNegative.
Sourcepub const fn into_nonzero(self) -> NonZero<i64>
pub const fn into_nonzero(self) -> NonZero<i64>
Positive ⊂ NonZero — narrows to core::num::NonZero with no
recheck, so a Positive divisor can feed DivNonZero directly.
Sourcepub const fn into_nonmin(self) -> NonMin<i64>
pub const fn into_nonmin(self) -> NonMin<i64>
Positive ⊂ NonMin (> 0, so != MIN).
Source§impl Positive<i128>
impl Positive<i128>
Sourcepub const unsafe fn new_unchecked(value: i128) -> Self
pub const unsafe fn new_unchecked(value: i128) -> Self
§Safety
value must be > 0.
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 to_unsigned(self) -> u128
pub const fn to_unsigned(self) -> u128
Infallible cast to the unsigned counterpart.
Sourcepub const fn into_nonnegative(self) -> NonNegative<i128>
pub const fn into_nonnegative(self) -> NonNegative<i128>
Positive ⊂ NonNegative.
Sourcepub const fn into_nonzero(self) -> NonZero<i128>
pub const fn into_nonzero(self) -> NonZero<i128>
Positive ⊂ NonZero — narrows to core::num::NonZero with no
recheck, so a Positive divisor can feed DivNonZero directly.
Sourcepub const fn into_nonmin(self) -> NonMin<i128>
pub const fn into_nonmin(self) -> NonMin<i128>
Positive ⊂ NonMin (> 0, so != MIN).
Source§impl Positive<isize>
impl Positive<isize>
Sourcepub const unsafe fn new_unchecked(value: isize) -> Self
pub const unsafe fn new_unchecked(value: isize) -> Self
§Safety
value must be > 0.
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 to_unsigned(self) -> usize
pub const fn to_unsigned(self) -> usize
Infallible cast to the unsigned counterpart.
Sourcepub const fn into_nonnegative(self) -> NonNegative<isize>
pub const fn into_nonnegative(self) -> NonNegative<isize>
Positive ⊂ NonNegative.
Sourcepub const fn into_nonzero(self) -> NonZero<isize>
pub const fn into_nonzero(self) -> NonZero<isize>
Positive ⊂ NonZero — narrows to core::num::NonZero with no
recheck, so a Positive divisor can feed DivNonZero directly.
Sourcepub const fn into_nonmin(self) -> NonMin<isize>
pub const fn into_nonmin(self) -> NonMin<isize>
Positive ⊂ NonMin (> 0, so != MIN).
Trait Implementations§
impl<T: Copy> Copy for Positive<T>
impl<T: Eq> Eq for Positive<T>
Source§impl<T: PartialEq> PartialEq for Positive<T>
impl<T: PartialEq> PartialEq for Positive<T>
impl<T: PartialEq> StructuralPartialEq for Positive<T>
Source§impl TryFrom<i8> for Positive<i8>
Checked construction by value; mirrors Positive::new.
impl TryFrom<i8> for Positive<i8>
Checked construction by value; mirrors Positive::new.
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<i16> for Positive<i16>
Checked construction by value; mirrors Positive::new.
impl TryFrom<i16> for Positive<i16>
Checked construction by value; mirrors Positive::new.
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<i32> for Positive<i32>
Checked construction by value; mirrors Positive::new.
impl TryFrom<i32> for Positive<i32>
Checked construction by value; mirrors Positive::new.
Source§type Error = TypestateError
type Error = TypestateError
Source§impl TryFrom<i64> for Positive<i64>
Checked construction by value; mirrors Positive::new.
impl TryFrom<i64> for Positive<i64>
Checked construction by value; mirrors Positive::new.