Skip to main content

Bounded

Struct Bounded 

Source
pub struct Bounded<const MIN: u64, const MAX: u64>(/* private fields */);
Expand description

A value known at compile time to lie in [MIN, MAX].

The constructor is the only way in, so Bounded<MIN, MAX> is a refinement-typed integer: the invariant cannot be broken by construction.

Both constructors, new and clamp, refuse an empty range: when MIN > MAX no u64 satisfies MIN <= v <= MAX, so neither can produce a value. clamp used to return MIN in that case, which is outside [LOW, HIGH] and so broke the very invariant the type exists to carry.

Implementations§

Source§

impl<const MIN: u64, const MAX: u64> Bounded<MIN, MAX>

Source

pub const LOW: u64 = MIN

The inclusive lower bound.

Source

pub const HIGH: u64 = MAX

The inclusive upper bound.

Source

pub const fn new(v: u64) -> Option<Self>

Construct, or None when the value is outside the range.

Source

pub const fn get(self) -> u64

The raw value. Always within [MIN, MAX] by construction.

Source

pub const fn clamp(v: u64) -> Option<Self>

Clamp an arbitrary value into the range, or None when the range is empty.

MIN > MAX is an empty range: no u64 satisfies MIN <= v <= MAX, so there is no value to return. Returning MIN (the old behaviour) produced a Bounded whose get() is outside its own [LOW, HIGH], which breaks the invariant the type exists to carry – Bounded<100, 10>::clamp(50) gave 100, and 100 <= 10 is false. new already returns None for the same range, so the two constructors agree: an empty range admits nothing.

Source

pub const fn add(self, other: Self) -> Option<Self>

Add, or None if the sum leaves the range.

Source

pub const fn sub(self, other: Self) -> Option<Self>

Subtract, or None if the difference leaves the range.

Accepts exactly when other <= self and the difference is still >= MIN. Both halves matter: at MIN == 10, self == 11 and other == 10 are in range and other <= self, yet the difference 1 is below MIN, so this returns None. The underflow gate is the other half – without it a wrapping difference can land back in range and be accepted. crate::prove::bounded_sub_is_total is the obligation that proves this is exactly the condition.

Trait Implementations§

Source§

impl<const MIN: u64, const MAX: u64> Clone for Bounded<MIN, MAX>

Source§

fn clone(&self) -> Self

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<const MIN: u64, const MAX: u64> Copy for Bounded<MIN, MAX>

Source§

impl<const MIN: u64, const MAX: u64> Debug for Bounded<MIN, MAX>

Source§

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

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

impl<const MIN: u64, const MAX: u64> Eq for Bounded<MIN, MAX>

Source§

impl<const MIN: u64, const MAX: u64> Ord for Bounded<MIN, MAX>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<const MIN: u64, const MAX: u64> PartialEq for Bounded<MIN, MAX>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl<const MIN: u64, const MAX: u64> PartialOrd for Bounded<MIN, MAX>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<const MIN: u64, const MAX: u64> StructuralPartialEq for Bounded<MIN, MAX>

Auto Trait Implementations§

§

impl<const MIN: u64, const MAX: u64> Forget for Bounded<MIN, MAX>

§

impl<const MIN: u64, const MAX: u64> Freeze for Bounded<MIN, MAX>

§

impl<const MIN: u64, const MAX: u64> Move for Bounded<MIN, MAX>

§

impl<const MIN: u64, const MAX: u64> RefUnwindSafe for Bounded<MIN, MAX>

§

impl<const MIN: u64, const MAX: u64> Send for Bounded<MIN, MAX>

§

impl<const MIN: u64, const MAX: u64> Sync for Bounded<MIN, MAX>

§

impl<const MIN: u64, const MAX: u64> Unpin for Bounded<MIN, MAX>

§

impl<const MIN: u64, const MAX: u64> UnsafeUnpin for Bounded<MIN, MAX>

§

impl<const MIN: u64, const MAX: u64> UnwindSafe for Bounded<MIN, MAX>

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.