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>
impl<Min, Max, Underlying> Ranged<Min, Max, Underlying>
Sourcepub fn new<T>() -> Self
pub fn new<T>() -> Self
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);
Sourcepub fn try_new(u: Underlying) -> Option<Self>
pub fn try_new(u: Underlying) -> Option<Self>
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());
Sourcepub fn wrapped_add(&self, u: Underlying) -> Selfwhere
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>,
pub fn wrapped_add(&self, u: Underlying) -> Selfwhere
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);
Sourcepub fn offset<T>(&self) -> Ranged<Sum<Min, T>, Sum<Max, T>, Underlying>
pub fn offset<T>(&self) -> Ranged<Sum<Min, T>, Sum<Max, T>, 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);
Sourcepub fn value(&self) -> Underlyingwhere
Underlying: Copy,
pub fn value(&self) -> Underlyingwhere
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);
Sourcepub fn max_value(&self) -> Underlyingwhere
Max: CanMake<Underlying>,
pub fn max_value(&self) -> Underlyingwhere
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);
Sourcepub fn min_value(&self) -> Underlyingwhere
Min: CanMake<Underlying>,
pub fn min_value(&self) -> Underlyingwhere
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>
impl<Min1, Max1, Min2, Max2, Underlying> Add<Ranged<Min2, Max2, Underlying>> for Ranged<Min1, Max1, Underlying>
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>,
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>
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§impl<Min1, Max1, Min2, Max2, Underlying> Rem<Ranged<Min2, Max2, Underlying>> for Ranged<Min1, Max1, Underlying>
impl<Min1, Max1, Min2, Max2, Underlying> Rem<Ranged<Min2, Max2, Underlying>> for Ranged<Min1, Max1, Underlying>
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>
impl<Min, Max, Underlying> Send for Ranged<Min, Max, Underlying>
impl<Min, Max, Underlying> Sync for Ranged<Min, Max, Underlying>
impl<Min, Max, Underlying> Unpin for Ranged<Min, Max, Underlying>
impl<Min, Max, Underlying> UnwindSafe for Ranged<Min, Max, Underlying>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more