ConstrainedU16

Struct ConstrainedU16 

Source
pub struct ConstrainedU16<const MIN: u16, const MAX: u16, const DEF: u16 = MIN>(/* private fields */);
Expand description

An u16 value that is constrained within an inclusive range.

The range is defined at compile time, by assigning values to the parameters MIN and MAX. MIN indicates the lower inclusive bound of the range, while MAX indicateds the upper inclusive bound. The value will always be contained within the defined range once it’s constructed.

The condition MAX > MIN must be satified, or else the type can’t be constructed.

A default can be supplied by assigning a value to the parameter DEF. The default value must be contained by the range, meaning: MIN <= DEF <= MAX. Or else the type can’t be constructed.

It’s also required that either MIN to be greater than the primitive’s MIN or MAX to be lower than the primitive’s MAX, or else the type can’t be constructed.

§Layout

ConstrainedU16 is guaranteed to have the same layout and ABI as u16

§Examples

If the provided parameters satisfy the construction condition, associated constants and type constructors are acessible.

use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

assert_eq!(Constrained::MIN, 1);
assert_eq!(Constrained::MAX, 254);
assert_eq!(Constrained::DEF, 1);

let constrained = Constrained::new(1)?;
assert_eq!(constrained.get(), 1);

Associated constants and type constructors are guarded against parameters that violate the MAX > MIN condition.

use constrained_int::u16::ConstrainedU16;

// MIN greater or equal to MAX does not satisfy the construction condition.
type InvalidRange = ConstrainedU16<254, 1>;

// None of these will compile for InvalidRange.
let value = InvalidRange::MIN;
let constrained = InvalidRange::default();
let constrained = InvalidRange::new_min();
/* ...other constructors */

Implementations§

Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> ConstrainedU16<MIN, MAX, DEF>
where Guard<{ _ }>: Protect,

Source

pub const MIN: u16 = MIN

The minimum inclusive value that this type can hold.

It’s assigned the MIN parameter value. Always satisfies the condition: MIN < MAX.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

assert_eq!(Constrained::MIN, 1);
Source

pub const MAX: u16 = MAX

The maximum inclusive value that this type can hold.

It’s assigned the MAX parameter value. Always satisfies the condition: MAX > MIN.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

assert_eq!(Constrained::MAX, 254);
Source

pub const DEF: u16 = DEF

The initialized value when constructed with default().

It’s assigned the DEF parameter value. Always satisfies the condition: MIN <= DEF <= MAX.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254, 254>;

assert_eq!(Constrained::DEF, 254);
Source

pub const fn new(value: u16) -> Result<Self, ConstrainedU16Error<MIN, MAX>>

Creates a new instance with provided value, if it satifies the range’s inclusive bounds. If the provided value is out of bounds, an error is returned, indicating which bound was violated.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let min = Constrained::new(1)?;
assert_eq!(min.get(), 1);

let max = Constrained::new(254)?;
assert_eq!(max.get(), 254);

// Out of inclusive bounds.
assert!(Constrained::new(0).is_err());
assert!(Constrained::new(255).is_err());
Source

pub const fn saturating_new(value: u16) -> Self

Creates a new instance with provided value, if it satifies the range’s inclusive bounds. If provided value is out of bounds, the new instance is initialized with the value of the closest bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

// Below lower bound, so it constructs with lower bound value.
let min = Constrained::saturating_new(0);
assert_eq!(min.get(), 1);

// Above upper bound, so it constructs with upper bound value.
let max = Constrained::saturating_new(255);
assert_eq!(max.get(), 254);
Source

pub const fn checked_new(value: u16) -> Option<Self>

Creates a new instance with provided value, if it satifies the range’s inclusive bounds. If provided value is out of bounds, a None is returned.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let min = Constrained::checked_new(1).unwrap();
assert_eq!(min.get(), 1);

let max = Constrained::checked_new(254).unwrap();
assert_eq!(max.get(), 254);

// Out of inclusive bounds.
assert_eq!(None, Constrained::checked_new(0));
assert_eq!(None, Constrained::checked_new(255));
Source

pub const fn new_min() -> Self

Creates a new instance with the value defined by the range’s lower bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let min = Constrained::new_min();
assert_eq!(min.get(), 1);
Source

pub const fn new_max() -> Self

Creates a new instance with the value defined by the range’s upper bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let max = Constrained::new_max();
assert_eq!(max.get(), 254);
Source

pub const fn range() -> RangeInclusive<u16>

Returns a RangeInclusive value corresponding to the type’s bondaries.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let range = Constrained::range();
assert_eq!(Constrained::MIN, *range.start());
assert_eq!(Constrained::MAX, *range.end());
Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> ConstrainedU16<MIN, MAX, DEF>

Source

pub fn set(&mut self, value: u16) -> Result<(), ConstrainedU16Error<MIN, MAX>>

Sets the contained value, if it satifies the range’s inclusive bounds. If the provided value is out of bounds, an error is returned, indicating which bound was violated.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let mut constrained = Constrained::default();
assert_eq!(constrained.get(), 1);

constrained.set(254)?;
assert_eq!(constrained.get(), 254);

// Out of inclusive bounds.
assert!(constrained.set(0).is_err());
assert!(constrained.set(255).is_err());

assert_eq!(constrained.get(), 254);
Source

pub const fn get(&self) -> u16

Returns the value of the contained integer type.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let constrained = Constrained::default();
assert_eq!(constrained.get(), 1);
Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> ConstrainedU16<MIN, MAX, DEF>

Source

pub const fn saturating_add(self, rhs: u16) -> Self

Saturating integer addition. Computes self + rhs, saturating the result at range’s upper bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let mut constrained = Constrained::new_max();
// Saturates at upper bound.
constrained = constrained.saturating_add(1);
assert_eq!(constrained.get(), 254);
Source

pub const fn saturating_add_signed(self, rhs: i16) -> Self

Saturating addition with a signed integer.

Computes self + rhs, saturating the result at the range’s upper bound if the integer is positive, or at the range’s lower bound if negative.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let mut constrained = Constrained::new_min();
// Saturates at upper bound.
constrained = constrained.saturating_add_signed(-1);
assert_eq!(constrained.get(), 1);
Source

pub const fn saturating_sub(self, rhs: u16) -> Self

Saturating integer substraction. Computes self - rhs, saturating the result at range’s lower bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let mut constrained = Constrained::new_min();
// Saturates at lower bound.
constrained = constrained.saturating_sub(1);
assert_eq!(constrained.get(), 1);
Source

pub const fn checked_add(self, rhs: u16) -> Option<Self>

Checked integer addition. Computes self + rhs, returning None if result is greater than range’s upper bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let constrained = Constrained::new_max();
// Above upper bound.
assert_eq!(constrained.checked_add(1), None);
Source

pub const fn checked_add_signed(self, rhs: i16) -> Option<Self>

Checked addition with a signed integer. Computes self + rhs, returning None if result is out of the range’s inclusive bounds.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let constrained = Constrained::new_min();
// Below lower bound.
assert_eq!(constrained.checked_add_signed(-1), None);
Source

pub const fn checked_sub(self, rhs: u16) -> Option<Self>

Checked integer substraction. Computes self - rhs, returning None if result is lower than range’s lower bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let constrained = Constrained::new_min();
// Below lower bound.
assert_eq!(constrained.checked_sub(1), None);
Source

pub const fn try_add(self, rhs: u16) -> Result<Self, MaxU16Error<MAX>>

Fallible integer addition. Computes self + rhs, returning an error if the result is greater than the range’s upper bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let constrained = Constrained::new_max();
// Above upper bound.
assert!(constrained.try_add(1).is_err());
Source

pub const fn try_add_signed( self, rhs: i16, ) -> Result<Self, ConstrainedU16Error<MIN, MAX>>

Fallible addition with signed integer. Computes self + rhs, returning an error if the result is out of the range’s inclusive bounds.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let constrained = Constrained::new_min();
// Below upper bound.
assert!(constrained.try_add_signed(-1).is_err());
Source

pub const fn try_sub(self, rhs: u16) -> Result<Self, MinU16Error<MIN>>

Fallible integer substraction. Computes self + rhs, returning an error if the result is lower than the range’s lower bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let constrained = Constrained::new_min();
// Below lower bound.
assert!(constrained.try_sub(1).is_err());
Source

pub const fn wrapping_add(self, rhs: u16) -> Self

Wrapping (modular) addition. Computes self + rhs, wrapping around at the range’s upper bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let mut constrained = Constrained::new_max();
// Wraps around the upper bound.
constrained = constrained.wrapping_add(1);
assert_eq!(constrained.get(), Constrained::MIN);
Source

pub const fn wrapping_add_signed(self, rhs: i16) -> Self

Wrapping (modular) addition with signed integer.

Computes self + rhs, wrapping the result around the range’s upper bound if the integer is positive, or at the range’s lower bound if negative.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let mut constrained = Constrained::new_min();
// Wraps around the upper bound.
constrained = constrained.wrapping_add_signed(-1);
assert_eq!(constrained.get(), Constrained::MAX);
Source

pub const fn wrapping_sub(self, rhs: u16) -> Self

Wrapping (modular) substraction. Computes self - rhs, wrapping around at the range’s lower bound.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let mut constrained = Constrained::new_min();
// Wraps around the lower bound.
constrained = constrained.wrapping_sub(1);
assert_eq!(constrained.get(), Constrained::MAX);
Source

pub const fn overflowing_add(self, rhs: u16) -> (Self, bool)

Wrapping (modular) addition, indicating if result was wrapped around.

Computes self + rhs, wrapping the result around the range’s upper bound. If a wrapping addition would have occurred, then the boolean is set to true, else to false.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let mut constrained = Constrained::new_max();
let mut wrapped: bool;

// Wraps around the upper bound, the boolean is set to `true`.
(constrained, wrapped) = constrained.overflowing_add(1);
assert_eq!(constrained.get(), Constrained::MIN);
assert_eq!(wrapped, true);
Source

pub const fn overflowing_add_signed(self, rhs: i16) -> (Self, bool)

Wrapping (modular) addition with signed integer, indicating if result was wrapped around.

Computes self + rhs, wrapping the result around the range’s upper bound if the integer is positive, or at the range’s lower bound if negative. If a wrapping addition would have occurred, then the boolean is set to true, else to false.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let mut constrained = Constrained::new_min();
let mut wrapped: bool;

// Wraps around the lower bound, the boolean is set to `true`.
(constrained, wrapped) = constrained.overflowing_add_signed(-1);
assert_eq!(constrained.get(), Constrained::MAX);
assert_eq!(wrapped, true);
Source

pub const fn overflowing_sub(self, rhs: u16) -> (Self, bool)

Wrapping (modular) substraction, indicating if result was wrapped around.

Computes self - rhs, wrapping the result around the range’s lower bound. If a wrapping substraction would have occurred, then the boolean is set to true, else to false.

§Example
use constrained_int::u16::ConstrainedU16;

type Constrained = ConstrainedU16<1, 254>;

let mut constrained = Constrained::new_min();
let mut wrapped: bool;

// Wraps around the lower bound, the boolean is set to `true`.
(constrained, wrapped) = constrained.overflowing_sub(1);
assert_eq!(constrained.get(), Constrained::MAX);
assert_eq!(wrapped, true);

Trait Implementations§

Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> Binary for ConstrainedU16<MIN, MAX, DEF>

Source§

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

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

impl<const MIN: u16, const MAX: u16, const DEF: u16> Clone for ConstrainedU16<MIN, MAX, DEF>

Source§

fn clone(&self) -> ConstrainedU16<MIN, MAX, DEF>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> Debug for ConstrainedU16<MIN, MAX, DEF>

Source§

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

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

impl<const MIN: u16, const MAX: u16, const DEF: u16> Default for ConstrainedU16<MIN, MAX, DEF>
where Guard<{ _ }>: Protect,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, const MIN: u16, const MAX: u16, const DEF: u16> Deserialize<'de> for ConstrainedU16<MIN, MAX, DEF>

Available on crate feature serde only.
Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> Display for ConstrainedU16<MIN, MAX, DEF>

Source§

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

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

impl<const MIN: u16, const MAX: u16, const DEF: u16> Hash for ConstrainedU16<MIN, MAX, DEF>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> LowerHex for ConstrainedU16<MIN, MAX, DEF>

Source§

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

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

impl<const MIN: u16, const MAX: u16, const DEF: u16> Octal for ConstrainedU16<MIN, MAX, DEF>

Source§

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

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

impl<const MIN: u16, const MAX: u16, const DEF: u16> Ord for ConstrainedU16<MIN, MAX, DEF>

Source§

fn cmp(&self, other: &ConstrainedU16<MIN, MAX, DEF>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

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

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

impl<const MIN: u16, const MAX: u16, const DEF: u16> PartialEq for ConstrainedU16<MIN, MAX, DEF>

Source§

fn eq(&self, other: &ConstrainedU16<MIN, MAX, DEF>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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<const MIN: u16, const MAX: u16, const DEF: u16> PartialOrd for ConstrainedU16<MIN, MAX, DEF>

Source§

fn partial_cmp(&self, other: &ConstrainedU16<MIN, MAX, DEF>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · 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 · 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 · 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 · 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: u16, const MAX: u16, const DEF: u16> RangeBounds<u16> for ConstrainedU16<MIN, MAX, DEF>

Source§

fn start_bound(&self) -> Bound<&u16>

Start index bound. Read more
Source§

fn end_bound(&self) -> Bound<&u16>

End index bound. Read more
1.35.0 · Source§

fn contains<U>(&self, item: &U) -> bool
where T: PartialOrd<U>, U: PartialOrd<T> + ?Sized,

Returns true if item is contained in the range. Read more
Source§

fn is_empty(&self) -> bool
where T: PartialOrd,

🔬This is a nightly-only experimental API. (range_bounds_is_empty)
Returns true if the range contains no items. One-sided ranges (RangeFrom, etc) always return false. Read more
Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> Serialize for ConstrainedU16<MIN, MAX, DEF>

Available on crate feature serde only.
Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more
Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> TryFrom<u16> for ConstrainedU16<MIN, MAX, DEF>
where Guard<{ _ }>: Protect,

Source§

type Error = ConstrainedU16Error<MIN, MAX>

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

fn try_from(value: u16) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> UpperHex for ConstrainedU16<MIN, MAX, DEF>

Source§

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

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

impl<const MIN: u16, const MAX: u16, const DEF: u16> Copy for ConstrainedU16<MIN, MAX, DEF>

Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> Eq for ConstrainedU16<MIN, MAX, DEF>

Source§

impl<const MIN: u16, const MAX: u16, const DEF: u16> StructuralPartialEq for ConstrainedU16<MIN, MAX, DEF>

Auto Trait Implementations§

§

impl<const MIN: u16, const MAX: u16, const DEF: u16> Freeze for ConstrainedU16<MIN, MAX, DEF>

§

impl<const MIN: u16, const MAX: u16, const DEF: u16> RefUnwindSafe for ConstrainedU16<MIN, MAX, DEF>

§

impl<const MIN: u16, const MAX: u16, const DEF: u16> Send for ConstrainedU16<MIN, MAX, DEF>

§

impl<const MIN: u16, const MAX: u16, const DEF: u16> Sync for ConstrainedU16<MIN, MAX, DEF>

§

impl<const MIN: u16, const MAX: u16, const DEF: u16> Unpin for ConstrainedU16<MIN, MAX, DEF>

§

impl<const MIN: u16, const MAX: u16, const DEF: u16> UnwindSafe for ConstrainedU16<MIN, MAX, DEF>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,