Skip to main content

NonNegative

Struct NonNegative 

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

Proof that a signed value is >= 0.

Implementations§

Source§

impl NonNegative<i8>

Source

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

Available on crate feature ct only.

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

Source§

impl NonNegative<i16>

Source

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

Available on crate feature ct only.

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

Source§

impl NonNegative<i32>

Source

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

Available on crate feature ct only.

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

Source§

impl NonNegative<i64>

Source

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

Available on crate feature ct only.

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

Source§

impl NonNegative<i128>

Source

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

Available on crate feature ct only.

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

Source§

impl NonNegative<isize>

Source

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

Available on crate feature ct only.

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

Source§

impl NonNegative<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 — the value is >= 0 so the bit pattern is preserved; no Option, no panic.

Source

pub const fn abs(self) -> i8

abs is the identity on a non-negative value (branch-free).

Source

pub const fn isqrt(self) -> i8

isqrt is total on a non-negative value (the inherent signed isqrt would otherwise panic on negatives).

Source

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

NonNegative ⊂ NonMin (>= 0, and MIN < 0, so != MIN) — narrows with no recheck so it can feed total neg/abs/division.

Source§

impl NonNegative<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 — the value is >= 0 so the bit pattern is preserved; no Option, no panic.

Source

pub const fn abs(self) -> i16

abs is the identity on a non-negative value (branch-free).

Source

pub const fn isqrt(self) -> i16

isqrt is total on a non-negative value (the inherent signed isqrt would otherwise panic on negatives).

Source

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

NonNegative ⊂ NonMin (>= 0, and MIN < 0, so != MIN) — narrows with no recheck so it can feed total neg/abs/division.

Source§

impl NonNegative<i32>

Source

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

Some iff value >= 0.

Examples found in repository?
examples/typestates.rs (line 63)
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 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 — the value is >= 0 so the bit pattern is preserved; no Option, no panic.

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

abs is the identity on a non-negative value (branch-free).

Source

pub const fn isqrt(self) -> i32

isqrt is total on a non-negative value (the inherent signed isqrt would otherwise panic on negatives).

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

NonNegative ⊂ NonMin (>= 0, and MIN < 0, so != MIN) — narrows with no recheck so it can feed total neg/abs/division.

Source§

impl NonNegative<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 — the value is >= 0 so the bit pattern is preserved; no Option, no panic.

Source

pub const fn abs(self) -> i64

abs is the identity on a non-negative value (branch-free).

Source

pub const fn isqrt(self) -> i64

isqrt is total on a non-negative value (the inherent signed isqrt would otherwise panic on negatives).

Source

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

NonNegative ⊂ NonMin (>= 0, and MIN < 0, so != MIN) — narrows with no recheck so it can feed total neg/abs/division.

Source§

impl NonNegative<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 — the value is >= 0 so the bit pattern is preserved; no Option, no panic.

Source

pub const fn abs(self) -> i128

abs is the identity on a non-negative value (branch-free).

Source

pub const fn isqrt(self) -> i128

isqrt is total on a non-negative value (the inherent signed isqrt would otherwise panic on negatives).

Source

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

NonNegative ⊂ NonMin (>= 0, and MIN < 0, so != MIN) — narrows with no recheck so it can feed total neg/abs/division.

Source§

impl NonNegative<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 — the value is >= 0 so the bit pattern is preserved; no Option, no panic.

Source

pub const fn abs(self) -> isize

abs is the identity on a non-negative value (branch-free).

Source

pub const fn isqrt(self) -> isize

isqrt is total on a non-negative value (the inherent signed isqrt would otherwise panic on negatives).

Source

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

NonNegative ⊂ NonMin (>= 0, and MIN < 0, so != MIN) — narrows with no recheck so it can feed total neg/abs/division.

Trait Implementations§

Source§

impl<T: Clone> Clone for NonNegative<T>

Source§

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

Source§

impl<T: Debug> Debug for NonNegative<T>

Source§

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

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

impl<T: Eq> Eq for NonNegative<T>

Source§

impl<T: PartialEq> PartialEq for NonNegative<T>

Source§

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

Source§

impl TryFrom<i8> for NonNegative<i8>

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

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

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

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

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

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

§

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

§

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

§

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

§

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

§

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

§

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