Struct Ranged

Source
pub struct Ranged<Min, Max, Underlying>(/* private fields */);
Expand description

Holds a single value between Min and Max. That value is stored in a single variable of type Underlying.

Example:

use typenum::{N5, P5};
use ranged_num::Ranged;

let ranged = Ranged::<N5, P5, i8>::try_new(-1).unwrap();
assert_eq!(ranged.value(), -1);

Implementations§

Source§

impl<Min, Max, Underlying> Ranged<Min, Max, Underlying>

Source

pub fn new<T>() -> Self
where T: IsLessOrEqual<Max> + CanMake<Underlying>, Min: IsLessOrEqual<T>,

Create a new Ranged holding the compile time known T.

use typenum::{N2, P3, P4};
use ranged_num::Ranged;

let ranged = Ranged::<N2, P4, isize>::new::<P3>();
assert_eq!(ranged.value(), 3);
Source

pub fn try_new(u: Underlying) -> Option<Self>
where Min: CanMake<Underlying>, Max: CanMake<Underlying>, Underlying: PartialOrd<Underlying>,

Create a new Randed from Min to Max (inclusive) from the given value. Returns Some(Ranged) if the given value is between Min and Max.

Example:

use typenum::{U2, U4};
use ranged_num::Ranged;

let ranged = Ranged::<U2, U4, usize>::try_new(3).unwrap();
assert_eq!(ranged.value(), 3);

let x = Ranged::<U2, U4, usize>::try_new(1);
assert!(x.is_none());
Source

pub fn wrapped_add(&self, u: Underlying) -> Self
where Min: CanMake<Underlying>, Max: CanMake<Underlying> + AddU<U1>, <Max as AddU<U1>>::Output: Sub<Min>, <<Max as AddU<U1>>::Output as Sub<Min>>::Output: CanMake<Underlying>, Underlying: Copy + Add<Underlying, Output = Underlying> + Sub<Underlying, Output = Underlying> + Rem<Underlying, Output = Underlying>,

Add u to the value held by this Ranged. If the sum wraps past Max start again from Min.

Example:

use typenum::{N2, P2, P4};
use ranged_num::Ranged;

let x = Ranged::<N2, P4, isize>::new::<P2>();
let y: Ranged<N2, P4, isize> = x.wrapped_add(4);
assert_eq!(y.value(), -1);
Source

pub fn offset<T>(&self) -> Ranged<Sum<Min, T>, Sum<Max, T>, Underlying>
where Min: Add<T>, Max: Add<T>, T: CanMake<Underlying>, Underlying: Copy + Add<Underlying, Output = Underlying>,

Offset the Ranged by T.

Example:

use typenum::*;
use ranged_num::Ranged;

let x = Ranged::<N2, P4, isize>::new::<P2>();
assert_eq!(x.value(), 2);

let x: Ranged<N3, P3, isize> = x.offset::<N1>();
assert_eq!(x.value(), 1);
Source

pub fn value(&self) -> Underlying
where Underlying: Copy,

Retrieve the value of this Ranged. It will be between Min and Max (inclusive).

Example:

use typenum::{N2, P3, P4};
use ranged_num::Ranged;

let ranged = Ranged::<N2, P4, isize>::new::<P3>();
assert_eq!(ranged.value(), 3);
Source

pub fn max_value(&self) -> Underlying
where Max: CanMake<Underlying>,

Calculate the maximum value that this Ranged could hold. That is get the runtime value of Max.

Example:

use typenum::{N2, P3, P4};
use ranged_num::Ranged;

let ranged = Ranged::<N2, P4, isize>::new::<P3>();
assert_eq!(ranged.max_value(), 4);
Source

pub fn min_value(&self) -> Underlying
where Min: CanMake<Underlying>,

Calculate the minimum value that this Ranged could hold. That is get the runtime value of Min.

Example:

use typenum::{N2, P3, P4};
use ranged_num::Ranged;

let ranged = Ranged::<N2, P4, isize>::new::<P3>();
assert_eq!(ranged.min_value(), -2);

Trait Implementations§

Source§

impl<Min1, Max1, Min2, Max2, Underlying> Add<Ranged<Min2, Max2, Underlying>> for Ranged<Min1, Max1, Underlying>
where Min1: Add<Min2>, Max1: Add<Max2>, Underlying: Add<Underlying, Output = Underlying>,

Source§

type Output = Ranged<<Min1 as Add<Min2>>::Output, <Max1 as Add<Max2>>::Output, Underlying>

The resulting type after applying the + operator.
Source§

fn add(self, other: Ranged<Min2, Max2, Underlying>) -> Self::Output

Performs the + operation. Read more
Source§

impl<Min: Debug, Max: Debug, Underlying: Debug> Debug for Ranged<Min, Max, Underlying>

Source§

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

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

impl<Min1, Max1, Min2, Max2, Underlying> Mul<Ranged<Min2, Max2, Underlying>> for Ranged<Min1, Max1, Underlying>
where Min1: Mul<Min2> + Mul<Max2>, Max1: Mul<Min2> + Mul<Max2>, Prod<Min1, Min2>: Min<Prod<Min1, Max2>> + Max<Prod<Min1, Max2>>, Prod<Max1, Min2>: Min<Prod<Max1, Max2>> + Max<Prod<Max1, Max2>>, Minimum<Prod<Min1, Min2>, Prod<Min1, Max2>>: Min<Minimum<Prod<Max1, Min2>, Prod<Max1, Max2>>>, Maximum<Prod<Min1, Min2>, Prod<Min1, Max2>>: Max<Maximum<Prod<Max1, Min2>, Prod<Max1, Max2>>>, Underlying: Mul<Underlying, Output = Underlying>,

Source§

type Output = Ranged<<<<Min1 as Mul<Min2>>::Output as Min<<Min1 as Mul<Max2>>::Output>>::Output as Min<<<Max1 as Mul<Min2>>::Output as Min<<Max1 as Mul<Max2>>::Output>>::Output>>::Output, <<<Min1 as Mul<Min2>>::Output as Max<<Min1 as Mul<Max2>>::Output>>::Output as Max<<<Max1 as Mul<Min2>>::Output as Max<<Max1 as Mul<Max2>>::Output>>::Output>>::Output, Underlying>

The resulting type after applying the * operator.
Source§

fn mul(self, other: Ranged<Min2, Max2, Underlying>) -> Self::Output

Performs the * operation. Read more
Source§

impl<Min1, Max1, Min2, Max2, Underlying> Rem<Ranged<Min2, Max2, Underlying>> for Ranged<Min1, Max1, Underlying>
where Underlying: Rem<Underlying, Output = Underlying>, Max2: Sub<U1>,

Source§

type Output = Ranged<UTerm, <Max2 as Sub<UInt<UTerm, B1>>>::Output, Underlying>

The resulting type after applying the % operator.
Source§

fn rem(self, other: Ranged<Min2, Max2, Underlying>) -> Self::Output

Performs the % operation. Read more
Source§

impl<Min1, Max1, Min2, Max2, Underlying> Sub<Ranged<Min2, Max2, Underlying>> for Ranged<Min1, Max1, Underlying>
where Min1: Sub<Max2>, Max1: Sub<Min2>, Underlying: Sub<Underlying, Output = Underlying>,

Source§

type Output = Ranged<<Min1 as Sub<Max2>>::Output, <Max1 as Sub<Min2>>::Output, Underlying>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Ranged<Min2, Max2, Underlying>) -> Self::Output

Performs the - operation. Read more

Auto Trait Implementations§

§

impl<Min, Max, Underlying> Freeze for Ranged<Min, Max, Underlying>
where Underlying: Freeze,

§

impl<Min, Max, Underlying> RefUnwindSafe for Ranged<Min, Max, Underlying>
where Underlying: RefUnwindSafe, Min: RefUnwindSafe, Max: RefUnwindSafe,

§

impl<Min, Max, Underlying> Send for Ranged<Min, Max, Underlying>
where Underlying: Send, Min: Send, Max: Send,

§

impl<Min, Max, Underlying> Sync for Ranged<Min, Max, Underlying>
where Underlying: Sync, Min: Sync, Max: Sync,

§

impl<Min, Max, Underlying> Unpin for Ranged<Min, Max, Underlying>
where Underlying: Unpin, Min: Unpin, Max: Unpin,

§

impl<Min, Max, Underlying> UnwindSafe for Ranged<Min, Max, Underlying>
where Underlying: UnwindSafe, Min: UnwindSafe, Max: 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> 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> Same for T

Source§

type Output = T

Should always be Self
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.