Trait numerated::Numerated

source ·
pub trait Numerated: Copy + Sized + Ord + Eq {
    type Distance: PrimInt + Unsigned;
    type Bound: Bound<Self>;

    // Required methods
    fn add_if_enclosed_by(
        self,
        num: Self::Distance,
        other: Self
    ) -> Option<Self>;
    fn sub_if_enclosed_by(
        self,
        num: Self::Distance,
        other: Self
    ) -> Option<Self>;
    fn distance(self, other: Self) -> Self::Distance;

    // Provided methods
    fn inc_if_lt(self, other: Self) -> Option<Self> { ... }
    fn dec_if_gt(self, other: Self) -> Option<Self> { ... }
    fn enclosed_by(self, a: &Self, b: &Self) -> bool { ... }
}
Expand description

Numerated type is a type, which has type for distances between any two values of Self, and provide an interface to add/subtract distance to/from value.

Default implementation is provided for all integer types: i8 u8 i16 u16 i32 u32 i64 u64 i128 u128 isize usize.

Required Associated Types§

source

type Distance: PrimInt + Unsigned

Numerate type: type that describes the distances between two values of Self.

source

type Bound: Bound<Self>

Bound type: type for which any value can be mapped to Self, and also has upper value, which is bigger than any value of Self.

Required Methods§

source

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

Adds num to self, if self + num is enclosed by self and other.

§Guaranties
  • iff self + num is enclosed by self and other, then returns Some(_).
  • iff self.add_if_enclosed_by(num, other) == Some(a), then a.sub_if_enclosed_by(num, self) == Some(self).
source

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

Subtracts num from self, if self - num is enclosed by self and other.

§Guaranties
  • iff self - num is enclosed by self and other, then returns Some(_).
  • iff self.sub_if_enclosed_by(num, other) == Some(a), then a.add_if_enclosed_by(num, self) == Some(self).
source

fn distance(self, other: Self) -> Self::Distance

Returns a distance between self and other

§Guaranties
  • iff self == other, then returns 0.
  • self.distance(other) == other.distance(self).
  • iff self.distance(other) == a and self ≥ other then
    • self.sub_if_enclosed_by(a, other) == Some(other)
    • other.add_if_enclosed_by(a, self) == Some(self)

Provided Methods§

source

fn inc_if_lt(self, other: Self) -> Option<Self>

Increments self, if self < other.

source

fn dec_if_gt(self, other: Self) -> Option<Self>

Decrements self, if self > other.

source

fn enclosed_by(self, a: &Self, b: &Self) -> bool

Returns true, if self is enclosed by a and b.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl Numerated for i8

§

type Distance = u8

§

type Bound = OptionBound<i8>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> u8

source§

impl Numerated for i16

§

type Distance = u16

§

type Bound = OptionBound<i16>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> u16

source§

impl Numerated for i32

§

type Distance = u32

§

type Bound = OptionBound<i32>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> u32

source§

impl Numerated for i64

§

type Distance = u64

§

type Bound = OptionBound<i64>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> u64

source§

impl Numerated for i128

§

type Distance = u128

§

type Bound = OptionBound<i128>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> u128

source§

impl Numerated for isize

§

type Distance = usize

§

type Bound = OptionBound<isize>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> usize

source§

impl Numerated for u8

§

type Distance = u8

§

type Bound = OptionBound<u8>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> u8

source§

impl Numerated for u16

§

type Distance = u16

§

type Bound = OptionBound<u16>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> u16

source§

impl Numerated for u32

§

type Distance = u32

§

type Bound = OptionBound<u32>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> u32

source§

impl Numerated for u64

§

type Distance = u64

§

type Bound = OptionBound<u64>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> u64

source§

impl Numerated for u128

§

type Distance = u128

§

type Bound = OptionBound<u128>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> u128

source§

impl Numerated for usize

§

type Distance = usize

§

type Bound = OptionBound<usize>

source§

fn add_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn sub_if_enclosed_by(self, num: Self::Distance, other: Self) -> Option<Self>

source§

fn distance(self, other: Self) -> usize

Implementors§