Trait 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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl Numerated for i8

Source§

type Distance = u8

Source§

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

Source§

type Distance = u16

Source§

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

Source§

type Distance = u32

Source§

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

Source§

type Distance = u64

Source§

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

Source§

type Distance = u128

Source§

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

Source§

type Distance = usize

Source§

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

Source§

type Distance = u8

Source§

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

Source§

type Distance = u16

Source§

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

Source§

type Distance = u32

Source§

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

Source§

type Distance = u64

Source§

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

Source§

type Distance = u128

Source§

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

Source§

type Distance = usize

Source§

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§