Skip to main content

Positive

Struct Positive 

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

Proof that a signed value is > 0.

Implementations§

Source§

impl Positive<i8>

Source

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

Available on crate feature ct only.

Constant-time new: None-masked when value <= 0.

Source§

impl Positive<i16>

Source

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

Available on crate feature ct only.

Constant-time new: None-masked when value <= 0.

Source§

impl Positive<i32>

Source

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

Available on crate feature ct only.

Constant-time new: None-masked when value <= 0.

Source§

impl Positive<i64>

Source

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

Available on crate feature ct only.

Constant-time new: None-masked when value <= 0.

Source§

impl Positive<i128>

Source

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

Available on crate feature ct only.

Constant-time new: None-masked when value <= 0.

Source§

impl Positive<isize>

Source

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

Available on crate feature ct only.

Constant-time new: None-masked when value <= 0.

Source§

impl Positive<i8>

Source

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

Some iff value > 0.

Source

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

§Safety

value must be > 0.

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 to_unsigned(self) -> u8

Infallible cast to the unsigned counterpart.

Source

pub const fn abs(self) -> i8

abs is the identity on a positive value.

Source

pub const fn isqrt(self) -> i8

isqrt is total on a positive value.

Source

pub const fn into_nonnegative(self) -> NonNegative<i8>

Positive ⊂ NonNegative.

Source

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.

Source

pub const fn into_nonmin(self) -> NonMin<i8>

Positive ⊂ NonMin (> 0, so != MIN).

Source§

impl Positive<i16>

Source

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

Some iff value > 0.

Source

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

§Safety

value must be > 0.

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 to_unsigned(self) -> u16

Infallible cast to the unsigned counterpart.

Source

pub const fn abs(self) -> i16

abs is the identity on a positive value.

Source

pub const fn isqrt(self) -> i16

isqrt is total on a positive value.

Source

pub const fn into_nonnegative(self) -> NonNegative<i16>

Positive ⊂ NonNegative.

Source

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.

Source

pub const fn into_nonmin(self) -> NonMin<i16>

Positive ⊂ NonMin (> 0, so != MIN).

Source§

impl Positive<i32>

Source

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

Some iff value > 0.

Examples found in repository?
examples/typestates.rs (line 69)
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}
Source

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

§Safety

value must be > 0.

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 to_unsigned(self) -> u32

Infallible cast to the unsigned counterpart.

Source

pub const fn abs(self) -> i32

abs is the identity on a positive value.

Source

pub const fn isqrt(self) -> i32

isqrt is total on a positive value.

Source

pub const fn into_nonnegative(self) -> NonNegative<i32>

Positive ⊂ NonNegative.

Source

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.

Examples found in repository?
examples/typestates.rs (line 69)
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}
Source

pub const fn into_nonmin(self) -> NonMin<i32>

Positive ⊂ NonMin (> 0, so != MIN).

Examples found in repository?
examples/typestates.rs (line 85)
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>

Source

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

Some iff value > 0.

Source

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

§Safety

value must be > 0.

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 to_unsigned(self) -> u64

Infallible cast to the unsigned counterpart.

Source

pub const fn abs(self) -> i64

abs is the identity on a positive value.

Source

pub const fn isqrt(self) -> i64

isqrt is total on a positive value.

Source

pub const fn into_nonnegative(self) -> NonNegative<i64>

Positive ⊂ NonNegative.

Source

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.

Source

pub const fn into_nonmin(self) -> NonMin<i64>

Positive ⊂ NonMin (> 0, so != MIN).

Source§

impl Positive<i128>

Source

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

Some iff value > 0.

Source

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

§Safety

value must be > 0.

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 to_unsigned(self) -> u128

Infallible cast to the unsigned counterpart.

Source

pub const fn abs(self) -> i128

abs is the identity on a positive value.

Source

pub const fn isqrt(self) -> i128

isqrt is total on a positive value.

Source

pub const fn into_nonnegative(self) -> NonNegative<i128>

Positive ⊂ NonNegative.

Source

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.

Source

pub const fn into_nonmin(self) -> NonMin<i128>

Positive ⊂ NonMin (> 0, so != MIN).

Source§

impl Positive<isize>

Source

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

Some iff value > 0.

Source

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

§Safety

value must be > 0.

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 to_unsigned(self) -> usize

Infallible cast to the unsigned counterpart.

Source

pub const fn abs(self) -> isize

abs is the identity on a positive value.

Source

pub const fn isqrt(self) -> isize

isqrt is total on a positive value.

Source

pub const fn into_nonnegative(self) -> NonNegative<isize>

Positive ⊂ NonNegative.

Source

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.

Source

pub const fn into_nonmin(self) -> NonMin<isize>

Positive ⊂ NonMin (> 0, so != MIN).

Trait Implementations§

Source§

impl<T: Clone> Clone for Positive<T>

Source§

fn clone(&self) -> Positive<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 Positive<T>

Source§

impl<T: Debug> Debug for Positive<T>

Source§

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

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

impl<T: Eq> Eq for Positive<T>

Source§

impl<T: PartialEq> PartialEq for Positive<T>

Source§

fn eq(&self, other: &Positive<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 Positive<T>

Source§

impl TryFrom<i8> for Positive<i8>

Checked construction by value; mirrors Positive::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 Positive<i16>

Checked construction by value; mirrors Positive::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 Positive<i32>

Checked construction by value; mirrors Positive::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 Positive<i64>

Checked construction by value; mirrors Positive::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 Positive<i128>

Checked construction by value; mirrors Positive::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 Positive<isize>

Checked construction by value; mirrors Positive::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 Positive<T>
where T: Freeze,

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for Positive<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.